coridoc-docx 0.1.6

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.
Files changed (32) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +164 -0
  3. data/Rakefile +8 -0
  4. data/lib/coridoc/docx/transform/context.rb +72 -0
  5. data/lib/coridoc/docx/transform/from_core_model.rb +598 -0
  6. data/lib/coridoc/docx/transform/frontmatter_core_properties.rb +107 -0
  7. data/lib/coridoc/docx/transform/numbering_access.rb +62 -0
  8. data/lib/coridoc/docx/transform/numbering_resolver.rb +127 -0
  9. data/lib/coridoc/docx/transform/ordered_content.rb +107 -0
  10. data/lib/coridoc/docx/transform/rule.rb +57 -0
  11. data/lib/coridoc/docx/transform/rule_registry.rb +60 -0
  12. data/lib/coridoc/docx/transform/rules/bookmark_rule.rb +34 -0
  13. data/lib/coridoc/docx/transform/rules/break_rule.rb +30 -0
  14. data/lib/coridoc/docx/transform/rules/footnote_rule.rb +27 -0
  15. data/lib/coridoc/docx/transform/rules/heading_rule.rb +52 -0
  16. data/lib/coridoc/docx/transform/rules/hyperlink_rule.rb +58 -0
  17. data/lib/coridoc/docx/transform/rules/image_rule.rb +125 -0
  18. data/lib/coridoc/docx/transform/rules/list_item_rule.rb +48 -0
  19. data/lib/coridoc/docx/transform/rules/math_rule.rb +39 -0
  20. data/lib/coridoc/docx/transform/rules/paragraph_rule.rb +65 -0
  21. data/lib/coridoc/docx/transform/rules/proof_error_rule.rb +25 -0
  22. data/lib/coridoc/docx/transform/rules/run_rule.rb +191 -0
  23. data/lib/coridoc/docx/transform/rules/simple_field_rule.rb +87 -0
  24. data/lib/coridoc/docx/transform/rules/structured_document_tag_rule.rb +36 -0
  25. data/lib/coridoc/docx/transform/rules/table_rule.rb +98 -0
  26. data/lib/coridoc/docx/transform/rules/text_rule.rb +25 -0
  27. data/lib/coridoc/docx/transform/style_resolver.rb +252 -0
  28. data/lib/coridoc/docx/transform/to_core_model.rb +382 -0
  29. data/lib/coridoc/docx/transform.rb +40 -0
  30. data/lib/coridoc/docx/version.rb +7 -0
  31. data/lib/coridoc/docx.rb +98 -0
  32. metadata +159 -0
@@ -0,0 +1,598 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uniword'
4
+
5
+ module Coridoc
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
+ # Coridoc::Docx::Transform::FromCoreModel.transform_to_file(core, "output.docx")
16
+ #
17
+ # @example Get Uniword DocumentRoot
18
+ # doc = Coridoc::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 [Coridoc::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 [Coridoc::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 Coridoc::CoreModel::StructuralElement
44
+ transform_structural_element(core)
45
+ when Coridoc::CoreModel::AnnotationBlock
46
+ transform_annotation_block(core)
47
+ when Coridoc::CoreModel::Block
48
+ transform_block(core)
49
+ when Coridoc::CoreModel::ListBlock
50
+ transform_list(core)
51
+ when Coridoc::CoreModel::Table
52
+ transform_table(core)
53
+ when Coridoc::CoreModel::Image
54
+ transform_image(core)
55
+ when Coridoc::CoreModel::InlineElement
56
+ transform_inline(core)
57
+ when Coridoc::CoreModel::FootnoteReference
58
+ build_ooxml_footnote_reference(core)
59
+ when Coridoc::CoreModel::Footnote
60
+ build_ooxml_footnote(core)
61
+ when Coridoc::CoreModel::DefinitionList
62
+ build_ooxml_definition_list(core)
63
+ when Coridoc::CoreModel::Toc
64
+ build_ooxml_toc(core)
65
+ when Coridoc::CoreModel::Term
66
+ build_ooxml_term(core)
67
+ when Coridoc::CoreModel::Abbreviation
68
+ build_ooxml_abbreviation(core)
69
+ when Coridoc::CoreModel::Bibliography
70
+ build_ooxml_bibliography(core)
71
+ when Coridoc::CoreModel::BibliographyEntry
72
+ build_ooxml_bibliography_entry(core)
73
+ when Coridoc::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 Coridoc::CoreModel::Footnote
159
+ footnotes << child
160
+ when Coridoc::CoreModel::StructuralElement
161
+ footnotes.concat(add_section_to_builder(child, builder))
162
+ when Coridoc::CoreModel::AnnotationBlock
163
+ add_annotation_to_builder(child, builder)
164
+ when Coridoc::CoreModel::Block
165
+ add_block_to_builder(child, builder)
166
+ when Coridoc::CoreModel::ListBlock
167
+ add_list_to_builder(child, builder)
168
+ when Coridoc::CoreModel::Table
169
+ add_table_to_builder(child, builder)
170
+ when Coridoc::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?(Coridoc::CoreModel::TextContent) }
204
+ builder.paragraph do |p|
205
+ content.each do |child|
206
+ case child
207
+ when Coridoc::CoreModel::TextContent
208
+ p << child.text
209
+ when String
210
+ p << child
211
+ when Coridoc::CoreModel::InlineElement
212
+ p << build_inline_text(child)
213
+ when Coridoc::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
+ list_method = list_block.marker_type == 'ordered' ? :numbered_list : :bullet_list
226
+
227
+ if list_method == :numbered_list
228
+ builder.numbered_list do |list|
229
+ list_block.items.each do |item|
230
+ list.item(item.flat_text)
231
+ end
232
+ end
233
+ else
234
+ builder.bullet_list do |list|
235
+ list_block.items.each do |item|
236
+ list.item(item.flat_text)
237
+ end
238
+ end
239
+ end
240
+ end
241
+
242
+ def add_table_to_builder(table, builder)
243
+ builder.table do |t|
244
+ table.rows.each do |row|
245
+ t.row do |r|
246
+ # TableRowBuilder#header_row is broken upstream (calls a
247
+ # nonexistent table_header= setter), so set the persisted
248
+ # w:tblHeader property on the row model directly
249
+ if row.header
250
+ r.model.properties = Uniword::Wordprocessingml::TableRowProperties.new(
251
+ tbl_header: Uniword::Wordprocessingml::ValInt.new(value: 1)
252
+ )
253
+ end
254
+ row.cells.each do |cell|
255
+ r.cell(text: cell.flat_text)
256
+ end
257
+ end
258
+ end
259
+ end
260
+ end
261
+
262
+ def add_image_to_builder(image, builder)
263
+ if image.src && File.exist?(image.src)
264
+ builder.image(image.src, alt_text: image.alt || '')
265
+ else
266
+ builder.paragraph("[Image: #{image.alt || image.src}]")
267
+ end
268
+ end
269
+
270
+ # Build OOXML objects directly (low-level)
271
+
272
+ def build_heading(text, level:)
273
+ para = Uniword::Wordprocessingml::Paragraph.new
274
+ para.properties = Uniword::Wordprocessingml::ParagraphProperties.new
275
+ para.properties.style = Uniword::Properties::StyleReference.new(
276
+ value: "Heading#{level}"
277
+ )
278
+ run = Uniword::Wordprocessingml::Run.new
279
+ run.text = Uniword::Wordprocessingml::Text.new(content: text)
280
+ para.runs << run
281
+ para
282
+ end
283
+
284
+ def build_ooxml_paragraph(block)
285
+ para = Uniword::Wordprocessingml::Paragraph.new
286
+
287
+ content = block.renderable_content
288
+ if content.is_a?(Array)
289
+ content.each do |child|
290
+ case child
291
+ when Coridoc::CoreModel::TextContent
292
+ run = Uniword::Wordprocessingml::Run.new
293
+ run.text = Uniword::Wordprocessingml::Text.new(content: child.text)
294
+ para.runs << run
295
+ when Coridoc::CoreModel::InlineElement
296
+ para.runs << build_ooxml_run(child)
297
+ when Coridoc::CoreModel::FootnoteReference
298
+ para.runs << build_ooxml_footnote_reference(child)
299
+ end
300
+ end
301
+ else
302
+ run = Uniword::Wordprocessingml::Run.new
303
+ run.text = Uniword::Wordprocessingml::Text.new(content: content.to_s)
304
+ para.runs << run
305
+ end
306
+
307
+ para.id = block.id if block.id
308
+ para
309
+ end
310
+
311
+ def build_ooxml_run(inline)
312
+ run = Uniword::Wordprocessingml::Run.new
313
+ run.text = Uniword::Wordprocessingml::Text.new(content: inline.content.to_s)
314
+
315
+ props = Uniword::Wordprocessingml::RunProperties.new
316
+
317
+ # resolve_format_type covers typed subclasses (BoldElement etc.)
318
+ # that carry no explicit format_type attribute
319
+ case inline.resolve_format_type
320
+ when 'bold'
321
+ props.bold = Uniword::Properties::Bold.new
322
+ when 'italic'
323
+ props.italic = Uniword::Properties::Italic.new
324
+ when 'underline'
325
+ props.underline = Uniword::Properties::Underline.new
326
+ when 'strikethrough'
327
+ props.strike = Uniword::Properties::Strike.new
328
+ when 'subscript'
329
+ va = Uniword::Properties::VerticalAlign.new
330
+ va.value = 'subscript'
331
+ props.vertical_align = va
332
+ when 'superscript'
333
+ va = Uniword::Properties::VerticalAlign.new
334
+ va.value = 'superscript'
335
+ props.vertical_align = va
336
+ when 'monospace'
337
+ props.fonts = Uniword::Wordprocessingml::RFonts.new(ascii: 'Courier New')
338
+ when 'link'
339
+ # Links need to be at the paragraph level (w:hyperlink)
340
+ # Return the run; caller should wrap in Hyperlink
341
+ return run
342
+ when 'highlight'
343
+ hl = Uniword::Properties::Highlight.new
344
+ hl.value = 'yellow'
345
+ props.highlight = hl
346
+ when 'xref'
347
+ return run
348
+ when 'stem'
349
+ return run
350
+ else
351
+ return run
352
+ end
353
+
354
+ run.properties = props unless plain_properties?(props)
355
+ run
356
+ end
357
+
358
+ def build_ooxml_list(list_block)
359
+ items = list_block.items || []
360
+ items.map do |item|
361
+ para = Uniword::Wordprocessingml::Paragraph.new
362
+ para.properties = Uniword::Wordprocessingml::ParagraphProperties.new
363
+ para.properties.num_id = 1
364
+ para.properties.ilvl = (list_block.marker_level || 1) - 1
365
+
366
+ content = item.flat_text
367
+ run = Uniword::Wordprocessingml::Run.new
368
+ run.text = Uniword::Wordprocessingml::Text.new(content: content)
369
+ para.runs << run
370
+ para
371
+ end
372
+ end
373
+
374
+ def build_ooxml_table(table)
375
+ tbl = Uniword::Wordprocessingml::Table.new
376
+
377
+ table.rows.each do |row|
378
+ tr = Uniword::Wordprocessingml::TableRow.new
379
+ if row.header
380
+ tr.properties = Uniword::Wordprocessingml::TableRowProperties.new(
381
+ tbl_header: Uniword::Wordprocessingml::ValInt.new(value: 1)
382
+ )
383
+ end
384
+ row.cells.each do |cell|
385
+ tc = Uniword::Wordprocessingml::TableCell.new
386
+
387
+ para = Uniword::Wordprocessingml::Paragraph.new
388
+ run = Uniword::Wordprocessingml::Run.new
389
+ run.text = Uniword::Wordprocessingml::Text.new(content: cell.flat_text)
390
+ para.runs << run
391
+ tc.paragraphs << para
392
+
393
+ tc.column_span = cell.colspan if cell.colspan && cell.colspan > 1
394
+ tc.row_span = cell.rowspan if cell.rowspan && cell.rowspan > 1
395
+
396
+ tr.cells << tc
397
+ end
398
+ tbl.rows << tr
399
+ end
400
+
401
+ tbl
402
+ end
403
+
404
+ def build_ooxml_image(image)
405
+ if image.src && File.exist?(image.src)
406
+ run = Uniword::Builder::ImageBuilder.create_run(
407
+ nil, image.src, alt_text: image.alt
408
+ )
409
+ para = Uniword::Wordprocessingml::Paragraph.new
410
+ else
411
+ para = Uniword::Wordprocessingml::Paragraph.new
412
+ run = Uniword::Wordprocessingml::Run.new
413
+ run.text = Uniword::Wordprocessingml::Text.new(
414
+ content: "[Image: #{image.alt || image.src}]"
415
+ )
416
+ end
417
+ para.runs << run
418
+ para
419
+ end
420
+
421
+ def build_page_break
422
+ para = Uniword::Wordprocessingml::Paragraph.new
423
+ run = Uniword::Wordprocessingml::Run.new
424
+ run.break = Uniword::Wordprocessingml::Break.new(type: 'page')
425
+ para.runs << run
426
+ para
427
+ end
428
+
429
+ def build_inline_text(inline)
430
+ text = inline.content.to_s
431
+
432
+ # resolve_format_type covers typed subclasses (BoldElement etc.)
433
+ # that carry no explicit format_type attribute
434
+ case inline.resolve_format_type
435
+ when 'bold' then Uniword::Builder.text(text, bold: true)
436
+ when 'italic' then Uniword::Builder.text(text, italic: true)
437
+ when 'underline' then Uniword::Builder.text(text, underline: true)
438
+ when 'strikethrough' then Uniword::Builder.text(text, strike: true)
439
+ when 'highlight' then Uniword::Builder.text(text, highlight: 'yellow')
440
+ when 'monospace' then Uniword::Builder.text(text, font: 'Courier New')
441
+ when 'subscript', 'superscript'
442
+ run = Uniword::Wordprocessingml::Run.new(text: text)
443
+ props = Uniword::Wordprocessingml::RunProperties.new
444
+ va = Uniword::Properties::VerticalAlign.new
445
+ va.value = inline.resolve_format_type
446
+ props.vertical_align = va
447
+ run.properties = props
448
+ run
449
+ when 'link'
450
+ Uniword::Builder.hyperlink(inline.target.to_s, text)
451
+ else
452
+ text
453
+ end
454
+ end
455
+
456
+ def plain_properties?(props)
457
+ props.bold.nil? &&
458
+ props.italic.nil? &&
459
+ props.underline.nil? &&
460
+ props.strike.nil? &&
461
+ props.vertical_align.nil? &&
462
+ props.highlight.nil?
463
+ end
464
+
465
+ # Build OOXML footnote reference (w:footnoteReference)
466
+ def build_ooxml_footnote_reference(footnote_ref)
467
+ ref = Uniword::Wordprocessingml::FootnoteReference.new
468
+ ref.id = footnote_ref.id.to_s
469
+
470
+ run = Uniword::Wordprocessingml::Run.new
471
+ run.footnote_reference = ref
472
+ run
473
+ end
474
+
475
+ # Write CoreModel::Footnote definitions as a w:footnotes part on
476
+ # the DocumentRoot. Reference runs carry the core ids verbatim,
477
+ # keeping w:footnoteReference/@w:id aligned with w:footnote/@w:id.
478
+ def attach_footnotes(footnotes, builder)
479
+ return if Array(footnotes).empty?
480
+
481
+ part = Uniword::Wordprocessingml::Footnotes.new
482
+ footnotes.each do |fn|
483
+ entry = Uniword::Wordprocessingml::Footnote.new(id: fn.id.to_s)
484
+ entry.paragraphs << footnote_content_paragraph(fn)
485
+ part.footnote_entries << entry
486
+ end
487
+ builder.model.footnotes = part
488
+ end
489
+
490
+ def footnote_content_paragraph(fn)
491
+ para = Uniword::Wordprocessingml::Paragraph.new
492
+ run = Uniword::Wordprocessingml::Run.new
493
+ run.text = Uniword::Wordprocessingml::Text.new(content: fn.content.to_s)
494
+ para.runs << run
495
+ para
496
+ end
497
+
498
+ # Build OOXML footnote content as a paragraph placeholder
499
+ def build_ooxml_footnote(footnote)
500
+ para = Uniword::Wordprocessingml::Paragraph.new
501
+ run = Uniword::Wordprocessingml::Run.new
502
+ run.text = Uniword::Wordprocessingml::Text.new(content: footnote.content.to_s)
503
+ para.runs << run
504
+ para
505
+ end
506
+
507
+ # Build OOXML definition list as a two-column table
508
+ def build_ooxml_definition_list(dl)
509
+ tbl = Uniword::Wordprocessingml::Table.new
510
+
511
+ Array(dl.items).each do |item|
512
+ row = Uniword::Wordprocessingml::TableRow.new
513
+
514
+ term_cell = Uniword::Wordprocessingml::TableCell.new
515
+ term_para = Uniword::Wordprocessingml::Paragraph.new
516
+ term_run = Uniword::Wordprocessingml::Run.new
517
+ term_run.text = Uniword::Wordprocessingml::Text.new(content: item.term.to_s)
518
+ term_props = Uniword::Wordprocessingml::RunProperties.new
519
+ term_props.bold = Uniword::Properties::Bold.new
520
+ term_run.properties = term_props
521
+ term_para.runs << term_run
522
+ term_cell.paragraphs << term_para
523
+
524
+ def_cell = Uniword::Wordprocessingml::TableCell.new
525
+ def_text = Array(item.definitions).map(&:to_s).join('; ')
526
+ def_para = Uniword::Wordprocessingml::Paragraph.new
527
+ def_run = Uniword::Wordprocessingml::Run.new
528
+ def_run.text = Uniword::Wordprocessingml::Text.new(content: def_text)
529
+ def_para.runs << def_run
530
+ def_cell.paragraphs << def_para
531
+
532
+ row.cells << term_cell
533
+ row.cells << def_cell
534
+ tbl.rows << row
535
+ end
536
+
537
+ tbl
538
+ end
539
+
540
+ # Build OOXML TOC as a text placeholder
541
+ def build_ooxml_toc(_toc)
542
+ para = Uniword::Wordprocessingml::Paragraph.new
543
+ run = Uniword::Wordprocessingml::Run.new
544
+ run.text = Uniword::Wordprocessingml::Text.new(content: '[Table of Contents]')
545
+ para.runs << run
546
+ para
547
+ end
548
+
549
+ # Build OOXML term as a bold paragraph
550
+ def build_ooxml_term(term)
551
+ para = Uniword::Wordprocessingml::Paragraph.new
552
+ run = Uniword::Wordprocessingml::Run.new
553
+ run.text = Uniword::Wordprocessingml::Text.new(content: term.text.to_s)
554
+ run_props = Uniword::Wordprocessingml::RunProperties.new
555
+ run_props.bold = Uniword::Properties::Bold.new
556
+ run.properties = run_props
557
+ para.runs << run
558
+ para
559
+ end
560
+
561
+ def build_ooxml_abbreviation(abbr)
562
+ para = Uniword::Wordprocessingml::Paragraph.new
563
+ run = Uniword::Wordprocessingml::Run.new
564
+ text = abbr.term.to_s
565
+ text += " (#{abbr.definition})" if abbr.definition
566
+ run.text = Uniword::Wordprocessingml::Text.new(content: text)
567
+ para.runs << run
568
+ para
569
+ end
570
+
571
+ def build_ooxml_bibliography(bib)
572
+ entries = Array(bib.entries).map { |e| build_ooxml_bibliography_entry(e) }
573
+
574
+ result = []
575
+ result << build_heading(bib.title, level: bib.level || 2) if bib.title
576
+ result.concat(entries)
577
+ result
578
+ end
579
+
580
+ def build_ooxml_bibliography_entry(entry)
581
+ para = Uniword::Wordprocessingml::Paragraph.new
582
+ run = Uniword::Wordprocessingml::Run.new
583
+ run.text = Uniword::Wordprocessingml::Text.new(content: entry.display_text)
584
+ para.runs << run
585
+ para
586
+ end
587
+
588
+ def build_ooxml_toc_entry(entry)
589
+ para = Uniword::Wordprocessingml::Paragraph.new
590
+ run = Uniword::Wordprocessingml::Run.new
591
+ run.text = Uniword::Wordprocessingml::Text.new(content: entry.title.to_s)
592
+ para.runs << run
593
+ para
594
+ end
595
+ end
596
+ end
597
+ end
598
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Docx
5
+ module Transform
6
+ # Single source of truth for FrontmatterBlock -> OOXML core
7
+ # properties mapping. (MECE: DOCX-specific concerns live in the
8
+ # DOCX gem; CoreModel has no knowledge of OOXML.)
9
+ #
10
+ # Uniword's DocumentBuilder exposes setter methods (title, author,
11
+ # subject, keywords, description, date_field) that write the
12
+ # docProps/core.xml Dublin Core elements. This module reads
13
+ # FrontmatterBlock entries and invokes the right setters, so the
14
+ # mapping table lives in exactly one place.
15
+ #
16
+ # Mapping table (single source of truth):
17
+ #
18
+ # | Frontmatter key | OOXML element | Builder method |
19
+ # |-----------------------|----------------------|----------------|
20
+ # | title | dc:title | title |
21
+ # | author | dc:creator | author |
22
+ # | date | dc:date | created |
23
+ # | description, excerpt | dc:description | description |
24
+ # | subject | dc:subject | subject |
25
+ # | tags, categories | cp:keywords | keywords |
26
+ module FrontmatterCoreProperties
27
+ DESCRIPTION_KEYS = %w[description excerpt].freeze
28
+ KEYWORDS_KEYS = %w[tags categories].freeze
29
+
30
+ class << self
31
+ # Apply FrontmatterBlock data to a Uniword DocumentBuilder.
32
+ # Skips nil/empty values. Does not overwrite builder values
33
+ # that the caller has set explicitly (caller invokes this
34
+ # AFTER setting document.title, for example).
35
+ #
36
+ # @param builder [Uniword::Builder::DocumentBuilder]
37
+ # @param block [Coridoc::CoreModel::FrontmatterBlock, nil]
38
+ # @return [void]
39
+ def apply(builder, block)
40
+ return unless block.is_a?(Coridoc::CoreModel::FrontmatterBlock)
41
+
42
+ data = Coridoc::CoreModel::FrontmatterBlock::Codec.to_hash(block)
43
+
44
+ if (v = find_first_scalar(data, KEYWORDS_KEYS))
45
+ builder.keywords(v)
46
+ end
47
+ if (v = find_first_scalar(data, %w[author]))
48
+ builder.author(v)
49
+ end
50
+ if (v = find_first_scalar(data, %w[subject]))
51
+ builder.subject(v)
52
+ end
53
+ if (v = find_first_scalar(data, DESCRIPTION_KEYS))
54
+ builder.description(v)
55
+ end
56
+ if (v = find_first_scalar(data, %w[date]))
57
+ builder.created(v)
58
+ end
59
+ end
60
+
61
+ # Locate the FrontmatterBlock in a DocumentElement's children,
62
+ # if any.
63
+ #
64
+ # @param document [Coridoc::CoreModel::DocumentElement]
65
+ # @return [Coridoc::CoreModel::FrontmatterBlock, nil]
66
+ def extract(document)
67
+ return nil unless document.is_a?(Coridoc::CoreModel::DocumentElement)
68
+
69
+ Array(document.children).find { |c| c.is_a?(Coridoc::CoreModel::FrontmatterBlock) }
70
+ end
71
+
72
+ private
73
+
74
+ def find_first_scalar(data, keys)
75
+ keys.each do |k|
76
+ value = data[k.to_s]
77
+ next if value.nil?
78
+
79
+ str = scalar_value(value)
80
+ return str if str && !str.empty?
81
+ end
82
+ nil
83
+ end
84
+
85
+ def scalar_value(value)
86
+ return nil if value.nil?
87
+
88
+ case value
89
+ when String then value
90
+ when Integer, Float, TrueClass, FalseClass then value.to_s
91
+ when Date, Time, DateTime then value.iso8601
92
+ when Symbol then value.to_s
93
+ when Array then array_to_csv(value)
94
+ when Hash then nil
95
+ else value.to_s
96
+ end
97
+ end
98
+
99
+ def array_to_csv(value)
100
+ strings = value.map { |i| scalar_value(i) }.compact
101
+ strings.any? ? strings.join(', ') : nil
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end