caracal 1.0.1 → 1.2.0
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.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/CHANGELOG.md +95 -5
- data/README.md +171 -35
- data/caracal.gemspec +4 -4
- data/lib/caracal/core/custom_properties.rb +49 -0
- data/lib/caracal/core/iframes.rb +42 -0
- data/lib/caracal/core/ignorables.rb +47 -0
- data/lib/caracal/core/models/custom_property_model.rb +60 -0
- data/lib/caracal/core/models/iframe_model.rb +148 -0
- data/lib/caracal/core/models/namespace_model.rb +65 -0
- data/lib/caracal/core/models/page_break_model.rb +50 -4
- data/lib/caracal/core/models/page_number_model.rb +50 -24
- data/lib/caracal/core/models/page_size_model.rb +34 -25
- data/lib/caracal/core/models/paragraph_model.rb +11 -4
- data/lib/caracal/core/models/style_model.rb +44 -15
- data/lib/caracal/core/models/table_cell_model.rb +61 -50
- data/lib/caracal/core/models/table_model.rb +1 -1
- data/lib/caracal/core/models/text_model.rb +42 -20
- data/lib/caracal/core/namespaces.rb +89 -0
- data/lib/caracal/core/page_numbers.rb +22 -16
- data/lib/caracal/core/page_settings.rb +18 -16
- data/lib/caracal/document.rb +75 -58
- data/lib/caracal/renderers/content_types_renderer.rb +18 -17
- data/lib/caracal/renderers/core_renderer.rb +1 -1
- data/lib/caracal/renderers/custom_renderer.rb +64 -0
- data/lib/caracal/renderers/document_renderer.rb +205 -118
- data/lib/caracal/renderers/fonts_renderer.rb +2 -2
- data/lib/caracal/renderers/footer_renderer.rb +41 -20
- data/lib/caracal/renderers/numbering_renderer.rb +27 -26
- data/lib/caracal/renderers/package_relationships_renderer.rb +14 -13
- data/lib/caracal/renderers/settings_renderer.rb +5 -5
- data/lib/caracal/renderers/styles_renderer.rb +88 -73
- data/lib/caracal/renderers/xml_renderer.rb +20 -20
- data/lib/caracal/version.rb +1 -1
- data/lib/caracal.rb +11 -2
- data/spec/lib/caracal/core/iframes_spec.rb +29 -0
- data/spec/lib/caracal/core/ignorables_spec.rb +79 -0
- data/spec/lib/caracal/core/models/iframe_model_spec.rb +83 -0
- data/spec/lib/caracal/core/models/image_model_spec.rb +60 -62
- data/spec/lib/caracal/core/models/link_model_spec.rb +12 -11
- data/spec/lib/caracal/core/models/namespace_model_spec.rb +107 -0
- data/spec/lib/caracal/core/models/page_number_model_spec.rb +66 -31
- data/spec/lib/caracal/core/models/page_size_model_spec.rb +42 -32
- data/spec/lib/caracal/core/models/style_model_spec.rb +32 -1
- data/spec/lib/caracal/core/models/table_cell_model_spec.rb +10 -1
- data/spec/lib/caracal/core/models/text_model_spec.rb +18 -10
- data/spec/lib/caracal/core/namespaces_spec.rb +116 -0
- data/spec/lib/caracal/core/page_numbers_spec.rb +32 -23
- data/spec/lib/caracal/core/page_settings_spec.rb +33 -25
- data/spec/lib/caracal/core/relationships_spec.rb +47 -47
- data/spec/support/_fixtures/snippet.docx +0 -0
- metadata +28 -8
|
@@ -5,32 +5,33 @@ require 'caracal/errors'
|
|
|
5
5
|
|
|
6
6
|
module Caracal
|
|
7
7
|
module Core
|
|
8
|
-
|
|
9
|
-
# This module encapsulates all the functionality related to setting the
|
|
8
|
+
|
|
9
|
+
# This module encapsulates all the functionality related to setting the
|
|
10
10
|
# document's size and margins.
|
|
11
11
|
#
|
|
12
12
|
module PageSettings
|
|
13
13
|
def self.included(base)
|
|
14
14
|
base.class_eval do
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
#-------------------------------------------------------------
|
|
17
17
|
# Configuration
|
|
18
18
|
#-------------------------------------------------------------
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
# accessors
|
|
21
21
|
attr_reader :page_width
|
|
22
|
+
attr_reader :page_orientation
|
|
22
23
|
attr_reader :page_height
|
|
23
24
|
attr_reader :page_margin_top
|
|
24
25
|
attr_reader :page_margin_bottom
|
|
25
26
|
attr_reader :page_margin_left
|
|
26
27
|
attr_reader :page_margin_right
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
|
|
29
30
|
#-------------------------------------------------------------
|
|
30
31
|
# Public Methods
|
|
31
32
|
#-------------------------------------------------------------
|
|
32
|
-
|
|
33
|
-
# This method controls the physical margins of the printed page. Defaults
|
|
33
|
+
|
|
34
|
+
# This method controls the physical margins of the printed page. Defaults
|
|
34
35
|
# to 1in on each side.
|
|
35
36
|
#
|
|
36
37
|
def page_margins(options={}, &block)
|
|
@@ -49,24 +50,25 @@ module Caracal
|
|
|
49
50
|
raise Caracal::Errors::InvalidModelError, 'page_margins method requires non-zero :top, :bottom, :left, and :right options.'
|
|
50
51
|
end
|
|
51
52
|
end
|
|
52
|
-
|
|
53
|
-
# This method controls the physical width and height of the printed page. Defaults
|
|
53
|
+
|
|
54
|
+
# This method controls the physical width and height of the printed page. Defaults
|
|
54
55
|
# to US standard A4 portrait size.
|
|
55
56
|
#
|
|
56
57
|
def page_size(options={}, &block)
|
|
57
58
|
model = Caracal::Core::Models::PageSizeModel.new(options, &block)
|
|
58
|
-
|
|
59
|
+
|
|
59
60
|
if model.valid?
|
|
60
|
-
@page_width
|
|
61
|
-
@page_height
|
|
61
|
+
@page_width = model.page_width
|
|
62
|
+
@page_height = model.page_height
|
|
63
|
+
@page_orientation = model.page_orientation
|
|
62
64
|
else
|
|
63
65
|
raise Caracal::Errors::InvalidModelError, 'page_size method requires non-zero :width and :height options.'
|
|
64
66
|
end
|
|
65
67
|
end
|
|
66
|
-
|
|
68
|
+
|
|
67
69
|
end
|
|
68
70
|
end
|
|
69
71
|
end
|
|
70
|
-
|
|
72
|
+
|
|
71
73
|
end
|
|
72
|
-
end
|
|
74
|
+
end
|
data/lib/caracal/document.rb
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
require 'open-uri'
|
|
2
2
|
require 'zip'
|
|
3
3
|
|
|
4
|
+
require 'caracal/core/custom_properties'
|
|
4
5
|
require 'caracal/core/file_name'
|
|
5
6
|
require 'caracal/core/fonts'
|
|
7
|
+
require 'caracal/core/iframes'
|
|
8
|
+
require 'caracal/core/ignorables'
|
|
6
9
|
require 'caracal/core/images'
|
|
7
10
|
require 'caracal/core/list_styles'
|
|
8
11
|
require 'caracal/core/lists'
|
|
12
|
+
require 'caracal/core/namespaces'
|
|
9
13
|
require 'caracal/core/page_breaks'
|
|
10
14
|
require 'caracal/core/page_numbers'
|
|
11
15
|
require 'caracal/core/page_settings'
|
|
@@ -18,6 +22,7 @@ require 'caracal/core/text'
|
|
|
18
22
|
require 'caracal/renderers/app_renderer'
|
|
19
23
|
require 'caracal/renderers/content_types_renderer'
|
|
20
24
|
require 'caracal/renderers/core_renderer'
|
|
25
|
+
require 'caracal/renderers/custom_renderer'
|
|
21
26
|
require 'caracal/renderers/document_renderer'
|
|
22
27
|
require 'caracal/renderers/fonts_renderer'
|
|
23
28
|
require 'caracal/renderers/footer_renderer'
|
|
@@ -30,97 +35,101 @@ require 'caracal/renderers/styles_renderer'
|
|
|
30
35
|
|
|
31
36
|
module Caracal
|
|
32
37
|
class Document
|
|
33
|
-
|
|
38
|
+
|
|
34
39
|
#-------------------------------------------------------------
|
|
35
40
|
# Configuration
|
|
36
41
|
#-------------------------------------------------------------
|
|
37
|
-
|
|
42
|
+
|
|
38
43
|
# mixins (order is important)
|
|
44
|
+
include Caracal::Core::CustomProperties
|
|
39
45
|
include Caracal::Core::FileName
|
|
40
|
-
|
|
46
|
+
include Caracal::Core::Ignorables
|
|
47
|
+
include Caracal::Core::Namespaces
|
|
41
48
|
include Caracal::Core::Relationships
|
|
49
|
+
|
|
42
50
|
include Caracal::Core::Fonts
|
|
43
51
|
include Caracal::Core::PageSettings
|
|
44
52
|
include Caracal::Core::PageNumbers
|
|
45
53
|
include Caracal::Core::Styles
|
|
46
54
|
include Caracal::Core::ListStyles
|
|
47
|
-
|
|
55
|
+
|
|
56
|
+
include Caracal::Core::IFrames
|
|
48
57
|
include Caracal::Core::Images
|
|
49
58
|
include Caracal::Core::Lists
|
|
50
59
|
include Caracal::Core::PageBreaks
|
|
51
60
|
include Caracal::Core::Rules
|
|
52
61
|
include Caracal::Core::Tables
|
|
53
62
|
include Caracal::Core::Text
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
|
|
64
|
+
|
|
56
65
|
#-------------------------------------------------------------
|
|
57
66
|
# Public Class Methods
|
|
58
67
|
#-------------------------------------------------------------
|
|
59
|
-
|
|
68
|
+
|
|
60
69
|
#============ OUTPUT ====================================
|
|
61
|
-
|
|
70
|
+
|
|
62
71
|
# This method renders a new Word document and returns it as a
|
|
63
72
|
# a string.
|
|
64
73
|
#
|
|
65
74
|
def self.render(f_name = nil, &block)
|
|
66
75
|
docx = new(f_name, &block)
|
|
67
76
|
buffer = docx.render
|
|
68
|
-
|
|
77
|
+
|
|
69
78
|
buffer.rewind
|
|
70
79
|
buffer.sysread
|
|
71
80
|
end
|
|
72
|
-
|
|
81
|
+
|
|
73
82
|
# This method renders a new Word document and saves it to the
|
|
74
83
|
# file system.
|
|
75
84
|
#
|
|
76
85
|
def self.save(f_name = nil, &block)
|
|
77
86
|
docx = new(f_name, &block)
|
|
78
87
|
buffer = docx.render
|
|
79
|
-
|
|
80
|
-
File.open("./#{ docx.name }", '
|
|
88
|
+
|
|
89
|
+
File.open("./#{ docx.name }", 'wb') { |f| f.write(buffer.string) }
|
|
81
90
|
end
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
85
94
|
#-------------------------------------------------------------
|
|
86
95
|
# Public Instance Methods
|
|
87
96
|
#-------------------------------------------------------------
|
|
88
|
-
|
|
97
|
+
|
|
89
98
|
# This method instantiates a new word document.
|
|
90
99
|
#
|
|
91
100
|
def initialize(name = nil, &block)
|
|
92
101
|
file_name(name)
|
|
93
|
-
|
|
94
|
-
page_size
|
|
102
|
+
|
|
103
|
+
page_size
|
|
95
104
|
page_margins top: 1440, bottom: 1440, left: 1440, right: 1440
|
|
96
105
|
page_numbers
|
|
97
|
-
|
|
98
|
-
[:
|
|
106
|
+
|
|
107
|
+
[:font, :list_style, :namespace, :relationship, :style].each do |method|
|
|
99
108
|
collection = self.class.send("default_#{ method }s")
|
|
100
109
|
collection.each do |item|
|
|
101
110
|
send(method, item)
|
|
102
111
|
end
|
|
103
112
|
end
|
|
104
|
-
|
|
113
|
+
|
|
105
114
|
if block_given?
|
|
106
115
|
(block.arity < 1) ? instance_eval(&block) : block[self]
|
|
107
116
|
end
|
|
108
117
|
end
|
|
109
|
-
|
|
110
|
-
|
|
118
|
+
|
|
119
|
+
|
|
111
120
|
#============ GETTERS ===================================
|
|
112
|
-
|
|
113
|
-
# This method returns an array of models which constitute the
|
|
121
|
+
|
|
122
|
+
# This method returns an array of models which constitute the
|
|
114
123
|
# set of instructions for producing the document content.
|
|
115
124
|
#
|
|
116
125
|
def contents
|
|
117
126
|
@contents ||= []
|
|
118
127
|
end
|
|
119
|
-
|
|
120
|
-
|
|
128
|
+
|
|
129
|
+
|
|
121
130
|
#============ RENDERING =================================
|
|
122
|
-
|
|
123
|
-
# This method renders the word document instance into
|
|
131
|
+
|
|
132
|
+
# This method renders the word document instance into
|
|
124
133
|
# a string buffer. Order is important!
|
|
125
134
|
#
|
|
126
135
|
def render
|
|
@@ -129,6 +138,7 @@ module Caracal
|
|
|
129
138
|
render_content_types(zip)
|
|
130
139
|
render_app(zip)
|
|
131
140
|
render_core(zip)
|
|
141
|
+
render_custom(zip)
|
|
132
142
|
render_fonts(zip)
|
|
133
143
|
render_footer(zip)
|
|
134
144
|
render_settings(zip)
|
|
@@ -139,58 +149,65 @@ module Caracal
|
|
|
139
149
|
render_numbering(zip) # Must go here: Depends on document renderer
|
|
140
150
|
end
|
|
141
151
|
end
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
145
155
|
#-------------------------------------------------------------
|
|
146
156
|
# Private Instance Methods
|
|
147
157
|
#-------------------------------------------------------------
|
|
148
158
|
private
|
|
149
|
-
|
|
159
|
+
|
|
150
160
|
#============ RENDERERS =====================================
|
|
151
|
-
|
|
161
|
+
|
|
152
162
|
def render_app(zip)
|
|
153
163
|
content = ::Caracal::Renderers::AppRenderer.render(self)
|
|
154
|
-
|
|
164
|
+
|
|
155
165
|
zip.put_next_entry('docProps/app.xml')
|
|
156
166
|
zip.write(content)
|
|
157
167
|
end
|
|
158
|
-
|
|
168
|
+
|
|
159
169
|
def render_content_types(zip)
|
|
160
170
|
content = ::Caracal::Renderers::ContentTypesRenderer.render(self)
|
|
161
|
-
|
|
171
|
+
|
|
162
172
|
zip.put_next_entry('[Content_Types].xml')
|
|
163
173
|
zip.write(content)
|
|
164
174
|
end
|
|
165
|
-
|
|
175
|
+
|
|
166
176
|
def render_core(zip)
|
|
167
177
|
content = ::Caracal::Renderers::CoreRenderer.render(self)
|
|
168
|
-
|
|
178
|
+
|
|
169
179
|
zip.put_next_entry('docProps/core.xml')
|
|
170
180
|
zip.write(content)
|
|
171
181
|
end
|
|
172
|
-
|
|
182
|
+
|
|
183
|
+
def render_custom(zip)
|
|
184
|
+
content = ::Caracal::Renderers::CustomRenderer.render(self)
|
|
185
|
+
|
|
186
|
+
zip.put_next_entry('docProps/custom.xml')
|
|
187
|
+
zip.write(content)
|
|
188
|
+
end
|
|
189
|
+
|
|
173
190
|
def render_document(zip)
|
|
174
191
|
content = ::Caracal::Renderers::DocumentRenderer.render(self)
|
|
175
|
-
|
|
192
|
+
|
|
176
193
|
zip.put_next_entry('word/document.xml')
|
|
177
194
|
zip.write(content)
|
|
178
195
|
end
|
|
179
|
-
|
|
196
|
+
|
|
180
197
|
def render_fonts(zip)
|
|
181
198
|
content = ::Caracal::Renderers::FontsRenderer.render(self)
|
|
182
|
-
|
|
199
|
+
|
|
183
200
|
zip.put_next_entry('word/fontTable.xml')
|
|
184
201
|
zip.write(content)
|
|
185
202
|
end
|
|
186
|
-
|
|
203
|
+
|
|
187
204
|
def render_footer(zip)
|
|
188
205
|
content = ::Caracal::Renderers::FooterRenderer.render(self)
|
|
189
|
-
|
|
206
|
+
|
|
190
207
|
zip.put_next_entry('word/footer1.xml')
|
|
191
208
|
zip.write(content)
|
|
192
209
|
end
|
|
193
|
-
|
|
210
|
+
|
|
194
211
|
def render_media(zip)
|
|
195
212
|
images = relationships.select { |r| r.relationship_type == :image }
|
|
196
213
|
images.each do |rel|
|
|
@@ -199,46 +216,46 @@ module Caracal
|
|
|
199
216
|
else
|
|
200
217
|
content = open(rel.relationship_target).read
|
|
201
218
|
end
|
|
202
|
-
|
|
219
|
+
|
|
203
220
|
zip.put_next_entry("word/#{ rel.formatted_target }")
|
|
204
221
|
zip.write(content)
|
|
205
222
|
end
|
|
206
223
|
end
|
|
207
|
-
|
|
224
|
+
|
|
208
225
|
def render_numbering(zip)
|
|
209
226
|
content = ::Caracal::Renderers::NumberingRenderer.render(self)
|
|
210
|
-
|
|
227
|
+
|
|
211
228
|
zip.put_next_entry('word/numbering.xml')
|
|
212
229
|
zip.write(content)
|
|
213
230
|
end
|
|
214
|
-
|
|
231
|
+
|
|
215
232
|
def render_package_relationships(zip)
|
|
216
233
|
content = ::Caracal::Renderers::PackageRelationshipsRenderer.render(self)
|
|
217
|
-
|
|
234
|
+
|
|
218
235
|
zip.put_next_entry('_rels/.rels')
|
|
219
236
|
zip.write(content)
|
|
220
237
|
end
|
|
221
|
-
|
|
238
|
+
|
|
222
239
|
def render_relationships(zip)
|
|
223
240
|
content = ::Caracal::Renderers::RelationshipsRenderer.render(self)
|
|
224
|
-
|
|
241
|
+
|
|
225
242
|
zip.put_next_entry('word/_rels/document.xml.rels')
|
|
226
243
|
zip.write(content)
|
|
227
244
|
end
|
|
228
|
-
|
|
245
|
+
|
|
229
246
|
def render_settings(zip)
|
|
230
247
|
content = ::Caracal::Renderers::SettingsRenderer.render(self)
|
|
231
|
-
|
|
248
|
+
|
|
232
249
|
zip.put_next_entry('word/settings.xml')
|
|
233
250
|
zip.write(content)
|
|
234
251
|
end
|
|
235
|
-
|
|
252
|
+
|
|
236
253
|
def render_styles(zip)
|
|
237
254
|
content = ::Caracal::Renderers::StylesRenderer.render(self)
|
|
238
|
-
|
|
255
|
+
|
|
239
256
|
zip.put_next_entry('word/styles.xml')
|
|
240
257
|
zip.write(content)
|
|
241
258
|
end
|
|
242
|
-
|
|
259
|
+
|
|
243
260
|
end
|
|
244
261
|
end
|
|
@@ -6,12 +6,12 @@ require 'caracal/renderers/xml_renderer'
|
|
|
6
6
|
module Caracal
|
|
7
7
|
module Renderers
|
|
8
8
|
class ContentTypesRenderer < XmlRenderer
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
#-------------------------------------------------------------
|
|
11
11
|
# Public Methods
|
|
12
12
|
#-------------------------------------------------------------
|
|
13
|
-
|
|
14
|
-
# This method produces the xml required for the `_[ContentTypes].xml`
|
|
13
|
+
|
|
14
|
+
# This method produces the xml required for the `_[ContentTypes].xml`
|
|
15
15
|
# sub-document.
|
|
16
16
|
#
|
|
17
17
|
def to_xml
|
|
@@ -23,31 +23,32 @@ module Caracal
|
|
|
23
23
|
xml.send 'Default', { 'Extension' => 'png', 'ContentType' => 'image/png' }
|
|
24
24
|
xml.send 'Default', { 'Extension' => 'rels', 'ContentType' => 'application/vnd.openxmlformats-package.relationships+xml' }
|
|
25
25
|
xml.send 'Default', { 'Extension' => 'xml', 'ContentType' => 'application/xml' }
|
|
26
|
-
xml.send 'Override', { 'PartName' => '/docProps/app.xml',
|
|
27
|
-
xml.send 'Override', { 'PartName' => '/docProps/core.xml',
|
|
28
|
-
xml.send 'Override', { 'PartName' => '/
|
|
29
|
-
xml.send 'Override', { 'PartName' => '/word/
|
|
30
|
-
xml.send 'Override', { 'PartName' => '/word/
|
|
31
|
-
xml.send 'Override', { 'PartName' => '/word/
|
|
32
|
-
xml.send 'Override', { 'PartName' => '/word/
|
|
33
|
-
xml.send 'Override', { 'PartName' => '/word/
|
|
26
|
+
xml.send 'Override', { 'PartName' => '/docProps/app.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.extended-properties+xml' }
|
|
27
|
+
xml.send 'Override', { 'PartName' => '/docProps/core.xml', 'ContentType' => 'application/vnd.openxmlformats-package.core-properties+xml' }
|
|
28
|
+
xml.send 'Override', { 'PartName' => '/docProps/custom.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.custom-properties+xml' }
|
|
29
|
+
xml.send 'Override', { 'PartName' => '/word/document.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' }
|
|
30
|
+
xml.send 'Override', { 'PartName' => '/word/footer1.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' }
|
|
31
|
+
xml.send 'Override', { 'PartName' => '/word/fontTable.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' }
|
|
32
|
+
xml.send 'Override', { 'PartName' => '/word/numbering.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' }
|
|
33
|
+
xml.send 'Override', { 'PartName' => '/word/settings.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' }
|
|
34
|
+
xml.send 'Override', { 'PartName' => '/word/styles.xml', 'ContentType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' }
|
|
34
35
|
end
|
|
35
36
|
end
|
|
36
37
|
builder.to_xml(save_options)
|
|
37
38
|
end
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
|
|
40
|
+
|
|
40
41
|
#-------------------------------------------------------------
|
|
41
42
|
# Private Methods
|
|
42
|
-
#-------------------------------------------------------------
|
|
43
|
+
#-------------------------------------------------------------
|
|
43
44
|
private
|
|
44
|
-
|
|
45
|
+
|
|
45
46
|
def root_options
|
|
46
47
|
{
|
|
47
48
|
'xmlns' => 'http://schemas.openxmlformats.org/package/2006/content-types'
|
|
48
49
|
}
|
|
49
50
|
end
|
|
50
|
-
|
|
51
|
+
|
|
51
52
|
end
|
|
52
53
|
end
|
|
53
|
-
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'nokogiri'
|
|
2
|
+
require 'date'
|
|
3
|
+
|
|
4
|
+
require 'caracal/renderers/xml_renderer'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
module Caracal
|
|
8
|
+
module Renderers
|
|
9
|
+
class CustomRenderer < XmlRenderer
|
|
10
|
+
|
|
11
|
+
#-------------------------------------------------------------
|
|
12
|
+
# Public Methods
|
|
13
|
+
#-------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
# This method produces the xml required for the `docProps/custom.xml`
|
|
16
|
+
# sub-document.
|
|
17
|
+
#
|
|
18
|
+
def to_xml
|
|
19
|
+
builder = ::Nokogiri::XML::Builder.with(declaration_xml) do |xml|
|
|
20
|
+
xml.send 'Properties', root_options do
|
|
21
|
+
document.custom_props.each_with_index do |property, index|
|
|
22
|
+
xml.send 'property',
|
|
23
|
+
{ fmtid: '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}', pid: index + 2, name: property.custom_property_name } do
|
|
24
|
+
case property.custom_property_type.downcase
|
|
25
|
+
when 'text'
|
|
26
|
+
xml['vt'].lpwstr property.custom_property_value
|
|
27
|
+
when 'date'
|
|
28
|
+
xml['vt'].filetime property.custom_property_value.to_date
|
|
29
|
+
when 'number'
|
|
30
|
+
xml['vt'].i4 property.custom_property_value.to_f
|
|
31
|
+
when 'boolean'
|
|
32
|
+
if property.custom_property_value == 'true' || property.custom_property_value == 'false'
|
|
33
|
+
xml['vt'].bool property.custom_property_value
|
|
34
|
+
else
|
|
35
|
+
# Not a boolean sent, so reverting to string so docx will open
|
|
36
|
+
xml['vt'].lpwstr property.custom_property_value
|
|
37
|
+
end
|
|
38
|
+
else
|
|
39
|
+
# Fail to string type
|
|
40
|
+
xml['vt'].lpwstr property.custom_property_value
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
builder.to_xml(save_options)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
#-------------------------------------------------------------
|
|
51
|
+
# Private Methods
|
|
52
|
+
#-------------------------------------------------------------
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def root_options
|
|
56
|
+
{
|
|
57
|
+
'xmlns' => "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
|
|
58
|
+
'xmlns:vt' => "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|