docx_manipulator 0.0.6 → 0.0.7
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/lib/docx_manipulator.rb +8 -4
- data/lib/docx_manipulator/content.rb +5 -6
- data/lib/docx_manipulator/version.rb +1 -1
- data/spec/docx_manipulator_spec.rb +12 -0
- data/spec/files/footer.xslt +13 -0
- metadata +4 -2
data/lib/docx_manipulator.rb
CHANGED
@@ -12,12 +12,16 @@ class DocxManipulator
|
|
12
12
|
@source = source
|
13
13
|
@target = target
|
14
14
|
|
15
|
-
@
|
15
|
+
@changed_files = []
|
16
16
|
@relationships = Relationships.new(source)
|
17
17
|
end
|
18
18
|
|
19
19
|
def content(new_content, options = {})
|
20
|
-
@
|
20
|
+
@changed_files << Content.new('word/document.xml', new_content, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_file(path, data, options = {})
|
24
|
+
@changed_files << Content.new(path, data, options)
|
21
25
|
end
|
22
26
|
|
23
27
|
def add_image(id, path)
|
@@ -29,7 +33,7 @@ class DocxManipulator
|
|
29
33
|
end
|
30
34
|
|
31
35
|
def process
|
32
|
-
files_to_be_written = @
|
36
|
+
files_to_be_written = @changed_files.map(&:writes_to_file).flatten + @relationships.writes_to_files
|
33
37
|
|
34
38
|
Zip::ZipOutputStream.open(target) do |os|
|
35
39
|
Zip::ZipFile.foreach(source) do |entry|
|
@@ -39,7 +43,7 @@ class DocxManipulator
|
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
42
|
-
@
|
46
|
+
@changed_files.each {|f| f.process(os)}
|
43
47
|
@relationships.process(os)
|
44
48
|
end
|
45
49
|
end
|
@@ -2,7 +2,10 @@ require 'nokogiri'
|
|
2
2
|
|
3
3
|
class DocxManipulator
|
4
4
|
class Content
|
5
|
-
|
5
|
+
attr_reader :writes_to_file
|
6
|
+
|
7
|
+
def initialize(path, new_content, options = {})
|
8
|
+
@writes_to_file = path
|
6
9
|
@new_content = if new_content.kind_of?(File)
|
7
10
|
new_content.read
|
8
11
|
else
|
@@ -15,12 +18,8 @@ class DocxManipulator
|
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
18
|
-
def writes_to_files
|
19
|
-
['word/document.xml']
|
20
|
-
end
|
21
|
-
|
22
21
|
def process(output)
|
23
|
-
output.put_next_entry
|
22
|
+
output.put_next_entry @writes_to_file
|
24
23
|
output.write @new_content
|
25
24
|
end
|
26
25
|
end
|
@@ -80,6 +80,18 @@ EOF
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
+
context 'add a custom file' do
|
84
|
+
let(:data) { '<translations><page>Seite</page></translations>' }
|
85
|
+
it 'transforms the data file with an xslt template' do
|
86
|
+
subject.add_file 'word/footer.xml', data, :xslt => File.new('spec/files/footer.xslt')
|
87
|
+
subject.process
|
88
|
+
Zip::ZipFile.open('spec/files/result.docx') do |file|
|
89
|
+
data = file.get_input_stream('word/footer.xml').read
|
90
|
+
data.should =~ /<w:t xml:space="preserve">Seite<\/w:t>/
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
83
95
|
context 'with an image' do
|
84
96
|
it 'adds an image' do
|
85
97
|
subject.add_image 'rId19', 'spec/files/duck.jpeg'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
2
|
+
<xsl:template match="/">
|
3
|
+
<w:ftr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
|
4
|
+
<w:p w:rsidR="00945BE5" w:rsidRPr="00AE7E2C" w:rsidRDefault="00945BE5">
|
5
|
+
<w:pPr><w:jc w:val="right"/><w:rPr><w:sz w:val="18"/><w:szCs w:val="18"/></w:rPr></w:pPr>
|
6
|
+
<w:r w:rsidRPr="00AE7E2C">
|
7
|
+
<w:rPr><w:snapToGrid w:val="0"/><w:sz w:val="18"/><w:szCs w:val="18"/></w:rPr>
|
8
|
+
<w:t xml:space="preserve"><xsl:value-of select="/translations/page"/></w:t>
|
9
|
+
</w:r>
|
10
|
+
</w:p>
|
11
|
+
</w:ftr>
|
12
|
+
</xsl:template>
|
13
|
+
</xsl:stylesheet>
|
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.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Michael St\xC3\xA4mpfli"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-12-12 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -81,6 +81,7 @@ files:
|
|
81
81
|
- spec/files/data.xml
|
82
82
|
- spec/files/document.xslt
|
83
83
|
- spec/files/duck.jpeg
|
84
|
+
- spec/files/footer.xslt
|
84
85
|
- spec/files/movies.docx
|
85
86
|
homepage: ""
|
86
87
|
licenses: []
|
@@ -115,4 +116,5 @@ test_files:
|
|
115
116
|
- spec/files/data.xml
|
116
117
|
- spec/files/document.xslt
|
117
118
|
- spec/files/duck.jpeg
|
119
|
+
- spec/files/footer.xslt
|
118
120
|
- spec/files/movies.docx
|