docx_manipulator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- metadata +3 -16
- data/.gitignore +0 -5
- data/.rvmrc +0 -1
- data/Gemfile +0 -4
- data/README.rdoc +0 -3
- data/Rakefile +0 -1
- data/docx_manipulator.gemspec +0 -25
- data/lib/docx_manipulator/version.rb +0 -3
- data/lib/docx_manipulator.rb +0 -77
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.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Michael St\xC3\xA4mpfli"
|
@@ -55,14 +55,6 @@ 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
|
66
58
|
- spec/docx_manipulator_spec.rb
|
67
59
|
- spec/files/content.txt
|
68
60
|
- spec/files/data.xml
|
@@ -96,10 +88,5 @@ rubygems_version: 1.8.11
|
|
96
88
|
signing_key:
|
97
89
|
specification_version: 3
|
98
90
|
summary: Enables the modification of docx files.
|
99
|
-
test_files:
|
100
|
-
|
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
|
91
|
+
test_files: []
|
92
|
+
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm --create ruby-1.9.2-p180@docx_manipulator
|
data/Gemfile
DELETED
data/README.rdoc
DELETED
data/Rakefile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
data/docx_manipulator.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
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 -- spec/*`.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_runtime_dependency "rubyzip"
|
24
|
-
s.add_runtime_dependency "nokogiri"
|
25
|
-
end
|
data/lib/docx_manipulator.rb
DELETED
@@ -1,77 +0,0 @@
|
|
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
|