ceradoc-docx 0.1.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.
- checksums.yaml +7 -0
- data/README.adoc +164 -0
- data/Rakefile +8 -0
- data/lib/ceradoc/docx/transform/context.rb +72 -0
- data/lib/ceradoc/docx/transform/from_core_model.rb +608 -0
- data/lib/ceradoc/docx/transform/frontmatter_core_properties.rb +107 -0
- data/lib/ceradoc/docx/transform/numbering_access.rb +62 -0
- data/lib/ceradoc/docx/transform/numbering_resolver.rb +127 -0
- data/lib/ceradoc/docx/transform/ordered_content.rb +107 -0
- data/lib/ceradoc/docx/transform/rule.rb +57 -0
- data/lib/ceradoc/docx/transform/rule_registry.rb +60 -0
- data/lib/ceradoc/docx/transform/rules/bookmark_rule.rb +34 -0
- data/lib/ceradoc/docx/transform/rules/break_rule.rb +30 -0
- data/lib/ceradoc/docx/transform/rules/footnote_rule.rb +27 -0
- data/lib/ceradoc/docx/transform/rules/heading_rule.rb +52 -0
- data/lib/ceradoc/docx/transform/rules/hyperlink_rule.rb +58 -0
- data/lib/ceradoc/docx/transform/rules/image_rule.rb +125 -0
- data/lib/ceradoc/docx/transform/rules/list_item_rule.rb +48 -0
- data/lib/ceradoc/docx/transform/rules/math_rule.rb +39 -0
- data/lib/ceradoc/docx/transform/rules/paragraph_rule.rb +65 -0
- data/lib/ceradoc/docx/transform/rules/proof_error_rule.rb +25 -0
- data/lib/ceradoc/docx/transform/rules/run_rule.rb +191 -0
- data/lib/ceradoc/docx/transform/rules/simple_field_rule.rb +87 -0
- data/lib/ceradoc/docx/transform/rules/structured_document_tag_rule.rb +36 -0
- data/lib/ceradoc/docx/transform/rules/table_rule.rb +98 -0
- data/lib/ceradoc/docx/transform/rules/text_rule.rb +25 -0
- data/lib/ceradoc/docx/transform/style_resolver.rb +252 -0
- data/lib/ceradoc/docx/transform/to_core_model.rb +382 -0
- data/lib/ceradoc/docx/transform.rb +40 -0
- data/lib/ceradoc/docx/version.rb +7 -0
- data/lib/ceradoc/docx.rb +98 -0
- metadata +159 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'uniword'
|
|
4
|
+
|
|
5
|
+
module Ceradoc
|
|
6
|
+
module Docx
|
|
7
|
+
module Transform
|
|
8
|
+
# Transforms CoreModel to OOXML document via Uniword Builder.
|
|
9
|
+
#
|
|
10
|
+
# Follows the hub-and-spoke architecture: CoreModel elements are
|
|
11
|
+
# dispatched to handler methods that produce Uniword OOXML objects.
|
|
12
|
+
# The resulting DocumentRoot is serialized to .docx format.
|
|
13
|
+
#
|
|
14
|
+
# @example Convert CoreModel to DOCX file
|
|
15
|
+
# Ceradoc::Docx::Transform::FromCoreModel.transform_to_file(core, "output.docx")
|
|
16
|
+
#
|
|
17
|
+
# @example Get Uniword DocumentRoot
|
|
18
|
+
# doc = Ceradoc::Docx::Transform::FromCoreModel.transform(core)
|
|
19
|
+
# doc.save("output.docx")
|
|
20
|
+
class FromCoreModel
|
|
21
|
+
class << self
|
|
22
|
+
# Transform a CoreModel document to a Uniword DocumentRoot
|
|
23
|
+
#
|
|
24
|
+
# @param core [Ceradoc::CoreModel::Base] CoreModel document
|
|
25
|
+
# @return [Uniword::Wordprocessingml::DocumentRoot] OOXML document
|
|
26
|
+
def transform(core)
|
|
27
|
+
new.transform(core)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Transform a CoreModel document and save to .docx file
|
|
31
|
+
#
|
|
32
|
+
# @param core [Ceradoc::CoreModel::Base] CoreModel document
|
|
33
|
+
# @param path [String] output file path
|
|
34
|
+
# @return [void]
|
|
35
|
+
def transform_to_file(core, path)
|
|
36
|
+
doc = transform(core)
|
|
37
|
+
doc.save(path)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def transform(core)
|
|
42
|
+
case core
|
|
43
|
+
when Ceradoc::CoreModel::StructuralElement
|
|
44
|
+
transform_structural_element(core)
|
|
45
|
+
when Ceradoc::CoreModel::AnnotationBlock
|
|
46
|
+
transform_annotation_block(core)
|
|
47
|
+
when Ceradoc::CoreModel::Block
|
|
48
|
+
transform_block(core)
|
|
49
|
+
when Ceradoc::CoreModel::ListBlock
|
|
50
|
+
transform_list(core)
|
|
51
|
+
when Ceradoc::CoreModel::Table
|
|
52
|
+
transform_table(core)
|
|
53
|
+
when Ceradoc::CoreModel::Image
|
|
54
|
+
transform_image(core)
|
|
55
|
+
when Ceradoc::CoreModel::InlineElement
|
|
56
|
+
transform_inline(core)
|
|
57
|
+
when Ceradoc::CoreModel::FootnoteReference
|
|
58
|
+
build_ooxml_footnote_reference(core)
|
|
59
|
+
when Ceradoc::CoreModel::Footnote
|
|
60
|
+
build_ooxml_footnote(core)
|
|
61
|
+
when Ceradoc::CoreModel::DefinitionList
|
|
62
|
+
build_ooxml_definition_list(core)
|
|
63
|
+
when Ceradoc::CoreModel::Toc
|
|
64
|
+
build_ooxml_toc(core)
|
|
65
|
+
when Ceradoc::CoreModel::Term
|
|
66
|
+
build_ooxml_term(core)
|
|
67
|
+
when Ceradoc::CoreModel::Abbreviation
|
|
68
|
+
build_ooxml_abbreviation(core)
|
|
69
|
+
when Ceradoc::CoreModel::Bibliography
|
|
70
|
+
build_ooxml_bibliography(core)
|
|
71
|
+
when Ceradoc::CoreModel::BibliographyEntry
|
|
72
|
+
build_ooxml_bibliography_entry(core)
|
|
73
|
+
when Ceradoc::CoreModel::TocEntry
|
|
74
|
+
build_ooxml_toc_entry(core)
|
|
75
|
+
when Array
|
|
76
|
+
transform_array(core)
|
|
77
|
+
else
|
|
78
|
+
core
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def transform_structural_element(element)
|
|
85
|
+
transform_document(element)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def transform_document(element)
|
|
89
|
+
doc = Uniword::Builder::DocumentBuilder.new
|
|
90
|
+
doc.title(element.title) if element.title
|
|
91
|
+
|
|
92
|
+
FrontmatterCoreProperties.apply(doc, FrontmatterCoreProperties.extract(element))
|
|
93
|
+
|
|
94
|
+
footnotes = transform_children(element.children, doc)
|
|
95
|
+
attach_footnotes(footnotes, doc)
|
|
96
|
+
|
|
97
|
+
doc.build
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def transform_block(block)
|
|
101
|
+
semantic = block.resolve_semantic_type
|
|
102
|
+
case semantic
|
|
103
|
+
when :page_break
|
|
104
|
+
build_page_break
|
|
105
|
+
when :comment
|
|
106
|
+
nil
|
|
107
|
+
else
|
|
108
|
+
build_ooxml_paragraph(block)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def transform_annotation_block(annotation)
|
|
113
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
114
|
+
type_run = Uniword::Wordprocessingml::Run.new
|
|
115
|
+
type_run.text = Uniword::Wordprocessingml::Text.new(
|
|
116
|
+
content: "#{annotation.annotation_type}: "
|
|
117
|
+
)
|
|
118
|
+
type_run.properties = Uniword::Wordprocessingml::RunProperties.new
|
|
119
|
+
type_run.properties.bold = Uniword::Properties::Bold.new
|
|
120
|
+
|
|
121
|
+
text_run = Uniword::Wordprocessingml::Run.new
|
|
122
|
+
text_run.text = Uniword::Wordprocessingml::Text.new(content: annotation.flat_text)
|
|
123
|
+
|
|
124
|
+
para.runs << type_run
|
|
125
|
+
para.runs << text_run
|
|
126
|
+
para
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def transform_list(list_block)
|
|
130
|
+
build_ooxml_list(list_block)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def transform_table(table)
|
|
134
|
+
build_ooxml_table(table)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def transform_image(image)
|
|
138
|
+
build_ooxml_image(image)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def transform_inline(inline)
|
|
142
|
+
build_ooxml_run(inline)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def transform_array(elements)
|
|
146
|
+
elements.map { |e| transform(e) }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Helper to transform children into builder calls. Returns the
|
|
150
|
+
# CoreModel::Footnote definitions encountered (including ones
|
|
151
|
+
# nested in sections) so the caller can attach a footnotes part.
|
|
152
|
+
def transform_children(children, builder)
|
|
153
|
+
return [] unless children
|
|
154
|
+
|
|
155
|
+
footnotes = []
|
|
156
|
+
children.each do |child|
|
|
157
|
+
case child
|
|
158
|
+
when Ceradoc::CoreModel::Footnote
|
|
159
|
+
footnotes << child
|
|
160
|
+
when Ceradoc::CoreModel::StructuralElement
|
|
161
|
+
footnotes.concat(add_section_to_builder(child, builder))
|
|
162
|
+
when Ceradoc::CoreModel::AnnotationBlock
|
|
163
|
+
add_annotation_to_builder(child, builder)
|
|
164
|
+
when Ceradoc::CoreModel::Block
|
|
165
|
+
add_block_to_builder(child, builder)
|
|
166
|
+
when Ceradoc::CoreModel::ListBlock
|
|
167
|
+
add_list_to_builder(child, builder)
|
|
168
|
+
when Ceradoc::CoreModel::Table
|
|
169
|
+
add_table_to_builder(child, builder)
|
|
170
|
+
when Ceradoc::CoreModel::Image
|
|
171
|
+
add_image_to_builder(child, builder)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
footnotes
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def add_section_to_builder(element, builder)
|
|
178
|
+
level = element.heading_level
|
|
179
|
+
builder.heading(element.title, level: level) if element.title
|
|
180
|
+
|
|
181
|
+
transform_children(element.children, builder)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def add_block_to_builder(block, builder)
|
|
185
|
+
case block.resolve_semantic_type
|
|
186
|
+
when :page_break
|
|
187
|
+
builder.page_break
|
|
188
|
+
else
|
|
189
|
+
add_paragraph_to_builder(block, builder)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def add_annotation_to_builder(annotation, builder)
|
|
194
|
+
text = "#{annotation.annotation_type}: #{annotation.flat_text}"
|
|
195
|
+
builder.paragraph do |p|
|
|
196
|
+
p << Uniword::Builder.text(text, bold: true)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def add_paragraph_to_builder(block, builder)
|
|
201
|
+
content = block.renderable_content
|
|
202
|
+
|
|
203
|
+
if content.is_a?(Array) && content.any? { |c| !c.is_a?(Ceradoc::CoreModel::TextContent) }
|
|
204
|
+
builder.paragraph do |p|
|
|
205
|
+
content.each do |child|
|
|
206
|
+
case child
|
|
207
|
+
when Ceradoc::CoreModel::TextContent
|
|
208
|
+
p << child.text
|
|
209
|
+
when String
|
|
210
|
+
p << child
|
|
211
|
+
when Ceradoc::CoreModel::InlineElement
|
|
212
|
+
p << build_inline_text(child)
|
|
213
|
+
when Ceradoc::CoreModel::FootnoteReference
|
|
214
|
+
p << build_ooxml_footnote_reference(child)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
else
|
|
219
|
+
text = content.is_a?(Array) ? content.join : content.to_s
|
|
220
|
+
builder.paragraph(text)
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def add_list_to_builder(list_block, builder)
|
|
225
|
+
if list_block.marker_type == 'ordered'
|
|
226
|
+
builder.numbered_list do |list|
|
|
227
|
+
list_block.items.each { |item| add_list_item_to_builder(item, list) }
|
|
228
|
+
end
|
|
229
|
+
else
|
|
230
|
+
builder.bullet_list do |list|
|
|
231
|
+
list_block.items.each { |item| add_list_item_to_builder(item, list) }
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# List items carry the same inline children paragraphs do — push
|
|
237
|
+
# styled runs (bold/italic/links) rather than the flattened text
|
|
238
|
+
# (#321).
|
|
239
|
+
def add_list_item_to_builder(item, list)
|
|
240
|
+
list.item do |para|
|
|
241
|
+
content_runs(item.renderable_content).each { |run| para << run }
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def add_table_to_builder(table, builder)
|
|
246
|
+
builder.table do |t|
|
|
247
|
+
table.rows.each do |row|
|
|
248
|
+
t.row do |r|
|
|
249
|
+
# TableRowBuilder#header_row is broken upstream (calls a
|
|
250
|
+
# nonexistent table_header= setter), so set the persisted
|
|
251
|
+
# w:tblHeader property on the row model directly
|
|
252
|
+
if row.header
|
|
253
|
+
r.model.properties = Uniword::Wordprocessingml::TableRowProperties.new(
|
|
254
|
+
tbl_header: Uniword::Wordprocessingml::ValInt.new(value: 1)
|
|
255
|
+
)
|
|
256
|
+
end
|
|
257
|
+
row.cells.each do |cell|
|
|
258
|
+
r.cell(text: cell.flat_text)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def add_image_to_builder(image, builder)
|
|
266
|
+
if image.src && File.exist?(image.src)
|
|
267
|
+
builder.image(image.src, alt_text: image.alt || '')
|
|
268
|
+
else
|
|
269
|
+
builder.paragraph("[Image: #{image.alt || image.src}]")
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Build OOXML objects directly (low-level)
|
|
274
|
+
|
|
275
|
+
def build_heading(text, level:)
|
|
276
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
277
|
+
para.properties = Uniword::Wordprocessingml::ParagraphProperties.new
|
|
278
|
+
para.properties.style = Uniword::Properties::StyleReference.new(
|
|
279
|
+
value: "Heading#{level}"
|
|
280
|
+
)
|
|
281
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
282
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: text)
|
|
283
|
+
para.runs << run
|
|
284
|
+
para
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def build_ooxml_paragraph(block)
|
|
288
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
289
|
+
para.runs.concat(content_runs(block.renderable_content))
|
|
290
|
+
para.id = block.id if block.id
|
|
291
|
+
para
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Convert CoreModel inline content to OOXML runs. Shared by the
|
|
295
|
+
# paragraph and list-item paths so styling survives both.
|
|
296
|
+
def content_runs(content)
|
|
297
|
+
runs = []
|
|
298
|
+
if content.is_a?(Array)
|
|
299
|
+
content.each do |child|
|
|
300
|
+
case child
|
|
301
|
+
when Ceradoc::CoreModel::TextContent
|
|
302
|
+
runs << plain_run(child.text)
|
|
303
|
+
when Ceradoc::CoreModel::InlineElement
|
|
304
|
+
runs << build_ooxml_run(child)
|
|
305
|
+
when Ceradoc::CoreModel::FootnoteReference
|
|
306
|
+
runs << build_ooxml_footnote_reference(child)
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
else
|
|
310
|
+
runs << plain_run(content.to_s)
|
|
311
|
+
end
|
|
312
|
+
runs
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
def plain_run(text)
|
|
316
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
317
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: text)
|
|
318
|
+
run
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def build_ooxml_run(inline)
|
|
322
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
323
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: inline.content.to_s)
|
|
324
|
+
|
|
325
|
+
props = Uniword::Wordprocessingml::RunProperties.new
|
|
326
|
+
|
|
327
|
+
# resolve_format_type covers typed subclasses (BoldElement etc.)
|
|
328
|
+
# that carry no explicit format_type attribute
|
|
329
|
+
case inline.resolve_format_type
|
|
330
|
+
when 'bold'
|
|
331
|
+
props.bold = Uniword::Properties::Bold.new
|
|
332
|
+
when 'italic'
|
|
333
|
+
props.italic = Uniword::Properties::Italic.new
|
|
334
|
+
when 'underline'
|
|
335
|
+
props.underline = Uniword::Properties::Underline.new
|
|
336
|
+
when 'strikethrough'
|
|
337
|
+
props.strike = Uniword::Properties::Strike.new
|
|
338
|
+
when 'subscript'
|
|
339
|
+
va = Uniword::Properties::VerticalAlign.new
|
|
340
|
+
va.value = 'subscript'
|
|
341
|
+
props.vertical_align = va
|
|
342
|
+
when 'superscript'
|
|
343
|
+
va = Uniword::Properties::VerticalAlign.new
|
|
344
|
+
va.value = 'superscript'
|
|
345
|
+
props.vertical_align = va
|
|
346
|
+
when 'monospace'
|
|
347
|
+
props.fonts = Uniword::Wordprocessingml::RFonts.new(ascii: 'Courier New')
|
|
348
|
+
when 'link'
|
|
349
|
+
# Links need to be at the paragraph level (w:hyperlink)
|
|
350
|
+
# Return the run; caller should wrap in Hyperlink
|
|
351
|
+
return run
|
|
352
|
+
when 'highlight'
|
|
353
|
+
hl = Uniword::Properties::Highlight.new
|
|
354
|
+
hl.value = 'yellow'
|
|
355
|
+
props.highlight = hl
|
|
356
|
+
when 'xref'
|
|
357
|
+
return run
|
|
358
|
+
when 'stem'
|
|
359
|
+
return run
|
|
360
|
+
else
|
|
361
|
+
return run
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
run.properties = props unless plain_properties?(props)
|
|
365
|
+
run
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def build_ooxml_list(list_block)
|
|
369
|
+
items = list_block.items || []
|
|
370
|
+
items.map do |item|
|
|
371
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
372
|
+
para.properties = Uniword::Wordprocessingml::ParagraphProperties.new
|
|
373
|
+
para.properties.num_id = 1
|
|
374
|
+
para.properties.ilvl = (list_block.marker_level || 1) - 1
|
|
375
|
+
|
|
376
|
+
content = item.flat_text
|
|
377
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
378
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: content)
|
|
379
|
+
para.runs << run
|
|
380
|
+
para
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def build_ooxml_table(table)
|
|
385
|
+
tbl = Uniword::Wordprocessingml::Table.new
|
|
386
|
+
|
|
387
|
+
table.rows.each do |row|
|
|
388
|
+
tr = Uniword::Wordprocessingml::TableRow.new
|
|
389
|
+
if row.header
|
|
390
|
+
tr.properties = Uniword::Wordprocessingml::TableRowProperties.new(
|
|
391
|
+
tbl_header: Uniword::Wordprocessingml::ValInt.new(value: 1)
|
|
392
|
+
)
|
|
393
|
+
end
|
|
394
|
+
row.cells.each do |cell|
|
|
395
|
+
tc = Uniword::Wordprocessingml::TableCell.new
|
|
396
|
+
|
|
397
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
398
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
399
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: cell.flat_text)
|
|
400
|
+
para.runs << run
|
|
401
|
+
tc.paragraphs << para
|
|
402
|
+
|
|
403
|
+
tc.column_span = cell.colspan if cell.colspan && cell.colspan > 1
|
|
404
|
+
tc.row_span = cell.rowspan if cell.rowspan && cell.rowspan > 1
|
|
405
|
+
|
|
406
|
+
tr.cells << tc
|
|
407
|
+
end
|
|
408
|
+
tbl.rows << tr
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
tbl
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def build_ooxml_image(image)
|
|
415
|
+
if image.src && File.exist?(image.src)
|
|
416
|
+
run = Uniword::Builder::ImageBuilder.create_run(
|
|
417
|
+
nil, image.src, alt_text: image.alt
|
|
418
|
+
)
|
|
419
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
420
|
+
else
|
|
421
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
422
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
423
|
+
run.text = Uniword::Wordprocessingml::Text.new(
|
|
424
|
+
content: "[Image: #{image.alt || image.src}]"
|
|
425
|
+
)
|
|
426
|
+
end
|
|
427
|
+
para.runs << run
|
|
428
|
+
para
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
def build_page_break
|
|
432
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
433
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
434
|
+
run.break = Uniword::Wordprocessingml::Break.new(type: 'page')
|
|
435
|
+
para.runs << run
|
|
436
|
+
para
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
def build_inline_text(inline)
|
|
440
|
+
text = inline.content.to_s
|
|
441
|
+
|
|
442
|
+
# resolve_format_type covers typed subclasses (BoldElement etc.)
|
|
443
|
+
# that carry no explicit format_type attribute
|
|
444
|
+
case inline.resolve_format_type
|
|
445
|
+
when 'bold' then Uniword::Builder.text(text, bold: true)
|
|
446
|
+
when 'italic' then Uniword::Builder.text(text, italic: true)
|
|
447
|
+
when 'underline' then Uniword::Builder.text(text, underline: true)
|
|
448
|
+
when 'strikethrough' then Uniword::Builder.text(text, strike: true)
|
|
449
|
+
when 'highlight' then Uniword::Builder.text(text, highlight: 'yellow')
|
|
450
|
+
when 'monospace' then Uniword::Builder.text(text, font: 'Courier New')
|
|
451
|
+
when 'subscript', 'superscript'
|
|
452
|
+
run = Uniword::Wordprocessingml::Run.new(text: text)
|
|
453
|
+
props = Uniword::Wordprocessingml::RunProperties.new
|
|
454
|
+
va = Uniword::Properties::VerticalAlign.new
|
|
455
|
+
va.value = inline.resolve_format_type
|
|
456
|
+
props.vertical_align = va
|
|
457
|
+
run.properties = props
|
|
458
|
+
run
|
|
459
|
+
when 'link'
|
|
460
|
+
Uniword::Builder.hyperlink(inline.target.to_s, text)
|
|
461
|
+
else
|
|
462
|
+
text
|
|
463
|
+
end
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
def plain_properties?(props)
|
|
467
|
+
props.bold.nil? &&
|
|
468
|
+
props.italic.nil? &&
|
|
469
|
+
props.underline.nil? &&
|
|
470
|
+
props.strike.nil? &&
|
|
471
|
+
props.vertical_align.nil? &&
|
|
472
|
+
props.highlight.nil?
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
# Build OOXML footnote reference (w:footnoteReference)
|
|
476
|
+
def build_ooxml_footnote_reference(footnote_ref)
|
|
477
|
+
ref = Uniword::Wordprocessingml::FootnoteReference.new
|
|
478
|
+
ref.id = footnote_ref.id.to_s
|
|
479
|
+
|
|
480
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
481
|
+
run.footnote_reference = ref
|
|
482
|
+
run
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
# Write CoreModel::Footnote definitions as a w:footnotes part on
|
|
486
|
+
# the DocumentRoot. Reference runs carry the core ids verbatim,
|
|
487
|
+
# keeping w:footnoteReference/@w:id aligned with w:footnote/@w:id.
|
|
488
|
+
def attach_footnotes(footnotes, builder)
|
|
489
|
+
return if Array(footnotes).empty?
|
|
490
|
+
|
|
491
|
+
part = Uniword::Wordprocessingml::Footnotes.new
|
|
492
|
+
footnotes.each do |fn|
|
|
493
|
+
entry = Uniword::Wordprocessingml::Footnote.new(id: fn.id.to_s)
|
|
494
|
+
entry.paragraphs << footnote_content_paragraph(fn)
|
|
495
|
+
part.footnote_entries << entry
|
|
496
|
+
end
|
|
497
|
+
builder.model.footnotes = part
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
def footnote_content_paragraph(fn)
|
|
501
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
502
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
503
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: fn.content.to_s)
|
|
504
|
+
para.runs << run
|
|
505
|
+
para
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
# Build OOXML footnote content as a paragraph placeholder
|
|
509
|
+
def build_ooxml_footnote(footnote)
|
|
510
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
511
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
512
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: footnote.content.to_s)
|
|
513
|
+
para.runs << run
|
|
514
|
+
para
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
# Build OOXML definition list as a two-column table
|
|
518
|
+
def build_ooxml_definition_list(dl)
|
|
519
|
+
tbl = Uniword::Wordprocessingml::Table.new
|
|
520
|
+
|
|
521
|
+
Array(dl.items).each do |item|
|
|
522
|
+
row = Uniword::Wordprocessingml::TableRow.new
|
|
523
|
+
|
|
524
|
+
term_cell = Uniword::Wordprocessingml::TableCell.new
|
|
525
|
+
term_para = Uniword::Wordprocessingml::Paragraph.new
|
|
526
|
+
term_run = Uniword::Wordprocessingml::Run.new
|
|
527
|
+
term_run.text = Uniword::Wordprocessingml::Text.new(content: item.term.to_s)
|
|
528
|
+
term_props = Uniword::Wordprocessingml::RunProperties.new
|
|
529
|
+
term_props.bold = Uniword::Properties::Bold.new
|
|
530
|
+
term_run.properties = term_props
|
|
531
|
+
term_para.runs << term_run
|
|
532
|
+
term_cell.paragraphs << term_para
|
|
533
|
+
|
|
534
|
+
def_cell = Uniword::Wordprocessingml::TableCell.new
|
|
535
|
+
def_text = Array(item.definitions).map(&:to_s).join('; ')
|
|
536
|
+
def_para = Uniword::Wordprocessingml::Paragraph.new
|
|
537
|
+
def_run = Uniword::Wordprocessingml::Run.new
|
|
538
|
+
def_run.text = Uniword::Wordprocessingml::Text.new(content: def_text)
|
|
539
|
+
def_para.runs << def_run
|
|
540
|
+
def_cell.paragraphs << def_para
|
|
541
|
+
|
|
542
|
+
row.cells << term_cell
|
|
543
|
+
row.cells << def_cell
|
|
544
|
+
tbl.rows << row
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
tbl
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# Build OOXML TOC as a text placeholder
|
|
551
|
+
def build_ooxml_toc(_toc)
|
|
552
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
553
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
554
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: '[Table of Contents]')
|
|
555
|
+
para.runs << run
|
|
556
|
+
para
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
# Build OOXML term as a bold paragraph
|
|
560
|
+
def build_ooxml_term(term)
|
|
561
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
562
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
563
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: term.text.to_s)
|
|
564
|
+
run_props = Uniword::Wordprocessingml::RunProperties.new
|
|
565
|
+
run_props.bold = Uniword::Properties::Bold.new
|
|
566
|
+
run.properties = run_props
|
|
567
|
+
para.runs << run
|
|
568
|
+
para
|
|
569
|
+
end
|
|
570
|
+
|
|
571
|
+
def build_ooxml_abbreviation(abbr)
|
|
572
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
573
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
574
|
+
text = abbr.term.to_s
|
|
575
|
+
text += " (#{abbr.definition})" if abbr.definition
|
|
576
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: text)
|
|
577
|
+
para.runs << run
|
|
578
|
+
para
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
def build_ooxml_bibliography(bib)
|
|
582
|
+
entries = Array(bib.entries).map { |e| build_ooxml_bibliography_entry(e) }
|
|
583
|
+
|
|
584
|
+
result = []
|
|
585
|
+
result << build_heading(bib.title, level: bib.level || 2) if bib.title
|
|
586
|
+
result.concat(entries)
|
|
587
|
+
result
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
def build_ooxml_bibliography_entry(entry)
|
|
591
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
592
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
593
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: entry.display_text)
|
|
594
|
+
para.runs << run
|
|
595
|
+
para
|
|
596
|
+
end
|
|
597
|
+
|
|
598
|
+
def build_ooxml_toc_entry(entry)
|
|
599
|
+
para = Uniword::Wordprocessingml::Paragraph.new
|
|
600
|
+
run = Uniword::Wordprocessingml::Run.new
|
|
601
|
+
run.text = Uniword::Wordprocessingml::Text.new(content: entry.title.to_s)
|
|
602
|
+
para.runs << run
|
|
603
|
+
para
|
|
604
|
+
end
|
|
605
|
+
end
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
end
|