docx_manipulator 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README.rdoc +3 -0
- data/Rakefile +1 -0
- data/docx_manipulator.gemspec +25 -0
- data/lib/docx_manipulator.rb +77 -0
- data/lib/docx_manipulator/version.rb +3 -0
- metadata +16 -3
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create ruby-1.9.2-p180@docx_manipulator
|
data/Gemfile
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "docx_manipulator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "docx_manipulator"
|
7
|
+
s.version = DocxManipulator::VERSION
|
8
|
+
s.authors = ["Michael Stämpfli"]
|
9
|
+
s.email = ["michael.staempfli@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Enables the modification of docx files.}
|
12
|
+
s.description = %q{This Gem enables you to modify the contents of docx files.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "docx_manipulator"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_dependency "rubyzip"
|
24
|
+
s.add_dependency "nokogiri"
|
25
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'docx_manipulator/version'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'zip/zip'
|
4
|
+
|
5
|
+
class DocxManipulator
|
6
|
+
|
7
|
+
attr_reader :source, :target, :new_content, :new_relationships
|
8
|
+
|
9
|
+
def initialize(source, target)
|
10
|
+
@source = source
|
11
|
+
@target = target
|
12
|
+
@new_relationships = source_relationships
|
13
|
+
@images = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def source_content
|
17
|
+
content = ''
|
18
|
+
Zip::ZipFile.open(source) do |file|
|
19
|
+
content = file.read('word/document.xml')
|
20
|
+
end
|
21
|
+
content
|
22
|
+
end
|
23
|
+
|
24
|
+
def source_relationships
|
25
|
+
content = ''
|
26
|
+
Zip::ZipFile.open(source) do |file|
|
27
|
+
content = file.read('word/_rels/document.xml.rels')
|
28
|
+
end
|
29
|
+
Nokogiri::XML.parse(content)
|
30
|
+
end
|
31
|
+
|
32
|
+
def content(new_content, options = {})
|
33
|
+
new_content_string = case new_content
|
34
|
+
when File then new_content.read
|
35
|
+
else new_content
|
36
|
+
end
|
37
|
+
if options.include?(:xslt)
|
38
|
+
xslt = Nokogiri::XSLT.parse(options[:xslt])
|
39
|
+
data = Nokogiri::XML.parse(new_content_string)
|
40
|
+
@new_content = xslt.transform(data).to_s
|
41
|
+
else
|
42
|
+
@new_content = new_content_string
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def add_image(id, path)
|
47
|
+
@images[id] = path
|
48
|
+
image_node = Nokogiri::XML::Node.new('Relationship', new_relationships)
|
49
|
+
image_node['Id'] = id
|
50
|
+
image_node['Type'] = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image'
|
51
|
+
image_node['Target'] = "media/#{File.basename(path)}"
|
52
|
+
new_relationships.root << image_node
|
53
|
+
end
|
54
|
+
|
55
|
+
def process
|
56
|
+
Zip::ZipOutputStream.open(target) do |os|
|
57
|
+
Zip::ZipFile.foreach(source) do |entry|
|
58
|
+
os.put_next_entry entry.name
|
59
|
+
if entry.name == 'word/document.xml'
|
60
|
+
os.write @new_content
|
61
|
+
elsif entry.name == 'word/_rels/document.xml.rels'
|
62
|
+
os.write new_relationships.to_s
|
63
|
+
elsif entry.file?
|
64
|
+
os.write entry.get_input_stream.read
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
@images.each do |id, path|
|
69
|
+
os.put_next_entry "word/media/#{File.basename(path)}"
|
70
|
+
File.open(path) do |file|
|
71
|
+
IO.copy_stream file, os
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: docx_manipulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Michael St\xC3\xA4mpfli"
|
@@ -55,6 +55,14 @@ extensions: []
|
|
55
55
|
extra_rdoc_files: []
|
56
56
|
|
57
57
|
files:
|
58
|
+
- .gitignore
|
59
|
+
- .rvmrc
|
60
|
+
- Gemfile
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- docx_manipulator.gemspec
|
64
|
+
- lib/docx_manipulator.rb
|
65
|
+
- lib/docx_manipulator/version.rb
|
58
66
|
- spec/docx_manipulator_spec.rb
|
59
67
|
- spec/files/content.txt
|
60
68
|
- spec/files/data.xml
|
@@ -88,5 +96,10 @@ rubygems_version: 1.8.11
|
|
88
96
|
signing_key:
|
89
97
|
specification_version: 3
|
90
98
|
summary: Enables the modification of docx files.
|
91
|
-
test_files:
|
92
|
-
|
99
|
+
test_files:
|
100
|
+
- spec/docx_manipulator_spec.rb
|
101
|
+
- spec/files/content.txt
|
102
|
+
- spec/files/data.xml
|
103
|
+
- spec/files/document.xslt
|
104
|
+
- spec/files/duck.jpeg
|
105
|
+
- spec/files/movies.docx
|