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.
- checksums.yaml +7 -0
- data/README.adoc +164 -0
- data/Rakefile +8 -0
- data/lib/coridoc/docx/transform/context.rb +72 -0
- data/lib/coridoc/docx/transform/from_core_model.rb +598 -0
- data/lib/coridoc/docx/transform/frontmatter_core_properties.rb +107 -0
- data/lib/coridoc/docx/transform/numbering_access.rb +62 -0
- data/lib/coridoc/docx/transform/numbering_resolver.rb +127 -0
- data/lib/coridoc/docx/transform/ordered_content.rb +107 -0
- data/lib/coridoc/docx/transform/rule.rb +57 -0
- data/lib/coridoc/docx/transform/rule_registry.rb +60 -0
- data/lib/coridoc/docx/transform/rules/bookmark_rule.rb +34 -0
- data/lib/coridoc/docx/transform/rules/break_rule.rb +30 -0
- data/lib/coridoc/docx/transform/rules/footnote_rule.rb +27 -0
- data/lib/coridoc/docx/transform/rules/heading_rule.rb +52 -0
- data/lib/coridoc/docx/transform/rules/hyperlink_rule.rb +58 -0
- data/lib/coridoc/docx/transform/rules/image_rule.rb +125 -0
- data/lib/coridoc/docx/transform/rules/list_item_rule.rb +48 -0
- data/lib/coridoc/docx/transform/rules/math_rule.rb +39 -0
- data/lib/coridoc/docx/transform/rules/paragraph_rule.rb +65 -0
- data/lib/coridoc/docx/transform/rules/proof_error_rule.rb +25 -0
- data/lib/coridoc/docx/transform/rules/run_rule.rb +191 -0
- data/lib/coridoc/docx/transform/rules/simple_field_rule.rb +87 -0
- data/lib/coridoc/docx/transform/rules/structured_document_tag_rule.rb +36 -0
- data/lib/coridoc/docx/transform/rules/table_rule.rb +98 -0
- data/lib/coridoc/docx/transform/rules/text_rule.rb +25 -0
- data/lib/coridoc/docx/transform/style_resolver.rb +252 -0
- data/lib/coridoc/docx/transform/to_core_model.rb +382 -0
- data/lib/coridoc/docx/transform.rb +40 -0
- data/lib/coridoc/docx/version.rb +7 -0
- data/lib/coridoc/docx.rb +98 -0
- metadata +159 -0
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Orchestrator for OOXML → CoreModel transformation.
|
|
7
|
+
#
|
|
8
|
+
# Walks a Uniword::Wordprocessingml::DocumentRoot tree and dispatches
|
|
9
|
+
# to registered transform rules. Handles:
|
|
10
|
+
#
|
|
11
|
+
# - Style-based heading detection (via StyleResolver)
|
|
12
|
+
# - List grouping (consecutive numPr paragraphs → single ListBlock)
|
|
13
|
+
# - Footnote content collection
|
|
14
|
+
# - Image reference tracking
|
|
15
|
+
# - Bookmark ID propagation
|
|
16
|
+
#
|
|
17
|
+
# Dispatch strategy:
|
|
18
|
+
# - HeadingRule and ListItemRule are dispatched directly by the
|
|
19
|
+
# orchestrator (they need context for style resolution).
|
|
20
|
+
# - All other element types are dispatched via RuleRegistry.
|
|
21
|
+
#
|
|
22
|
+
# @example Transform a DOCX document
|
|
23
|
+
# doc = Uniword::DocumentFactory.from_file("input.docx")
|
|
24
|
+
# core = ToCoreModel.transform(doc)
|
|
25
|
+
# # => Coridoc::CoreModel::StructuralElement
|
|
26
|
+
class ToCoreModel
|
|
27
|
+
include NumberingAccess
|
|
28
|
+
|
|
29
|
+
class << self
|
|
30
|
+
def transform(document)
|
|
31
|
+
new.transform(document)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def transform(document)
|
|
36
|
+
registry = build_registry
|
|
37
|
+
|
|
38
|
+
context = Context.new(
|
|
39
|
+
styles_configuration: document.styles_configuration,
|
|
40
|
+
numbering_configuration: document.numbering_configuration,
|
|
41
|
+
footnotes: collect_footnotes(document),
|
|
42
|
+
registry: registry
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
@heading_rule = Rules::HeadingRule.new
|
|
46
|
+
@list_item_rule = Rules::ListItemRule.new
|
|
47
|
+
|
|
48
|
+
body = document.body
|
|
49
|
+
doc_title = extract_document_title(document, context)
|
|
50
|
+
children = transform_elements(body, context)
|
|
51
|
+
|
|
52
|
+
# Footnote definitions ride along as document-level children so
|
|
53
|
+
# writers can emit [^id]: definition blocks after the body
|
|
54
|
+
definitions = footnote_definitions(document)
|
|
55
|
+
children.concat(definitions) if definitions.any?
|
|
56
|
+
|
|
57
|
+
# If the first child is an H1 matching the doc title, skip the
|
|
58
|
+
# duplicate — the document title already captures it
|
|
59
|
+
if doc_title && children.first.is_a?(Coridoc::CoreModel::StructuralElement) &&
|
|
60
|
+
children.first.section? &&
|
|
61
|
+
children.first.title == doc_title &&
|
|
62
|
+
children.first.level == 1
|
|
63
|
+
children.shift
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
doc = Coridoc::CoreModel::DocumentElement.new(
|
|
67
|
+
title: doc_title,
|
|
68
|
+
children: children
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
# Extract semantic content from headers/footers
|
|
72
|
+
extract_header_footer_metadata(document, doc)
|
|
73
|
+
|
|
74
|
+
doc
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
# Walk body elements with list grouping look-ahead
|
|
80
|
+
def transform_elements(body, context)
|
|
81
|
+
return [] unless body
|
|
82
|
+
|
|
83
|
+
elements = body_ordered_elements(body)
|
|
84
|
+
|
|
85
|
+
result = []
|
|
86
|
+
i = 0
|
|
87
|
+
|
|
88
|
+
while i < elements.length
|
|
89
|
+
element = elements[i]
|
|
90
|
+
|
|
91
|
+
transformed = dispatch_element(element, i, elements, context)
|
|
92
|
+
|
|
93
|
+
case transformed
|
|
94
|
+
when Array
|
|
95
|
+
consumed = transformed.length
|
|
96
|
+
result.concat(transformed.compact)
|
|
97
|
+
i += consumed
|
|
98
|
+
when nil
|
|
99
|
+
i += 1
|
|
100
|
+
else
|
|
101
|
+
result << transformed
|
|
102
|
+
i += 1
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
result
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Dispatch a single body element, handling paragraphs specially
|
|
110
|
+
def dispatch_element(element, index, elements, context)
|
|
111
|
+
# Paragraphs need style-based dispatch (heading, list, or plain)
|
|
112
|
+
return dispatch_paragraph(element, index, elements, context) if paragraph?(element)
|
|
113
|
+
|
|
114
|
+
# Tables go through registry directly
|
|
115
|
+
context.transform(element)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def dispatch_paragraph(paragraph, index, elements, context)
|
|
119
|
+
resolver = context.style_resolver
|
|
120
|
+
|
|
121
|
+
# Check for section break in paragraph properties
|
|
122
|
+
if section_break?(paragraph)
|
|
123
|
+
# Section break without heading → thematic break
|
|
124
|
+
return section_break_element(paragraph, context)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Heading
|
|
128
|
+
return @heading_rule.apply(paragraph, context) if resolver.heading?(paragraph)
|
|
129
|
+
|
|
130
|
+
# List item — group consecutive items with same numId
|
|
131
|
+
return group_list(elements, index, context) if resolver.list_item?(paragraph)
|
|
132
|
+
|
|
133
|
+
# Regular paragraph (via registry)
|
|
134
|
+
context.transform(paragraph)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def section_break?(paragraph)
|
|
138
|
+
return false unless paragraph.is_a?(Uniword::Wordprocessingml::Paragraph)
|
|
139
|
+
return false unless paragraph.properties
|
|
140
|
+
|
|
141
|
+
sect_pr = paragraph.properties.section_properties
|
|
142
|
+
return false unless sect_pr
|
|
143
|
+
|
|
144
|
+
sect_pr.type ? true : false
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def section_break_element(paragraph, context)
|
|
148
|
+
# First, transform the paragraph content if it has text
|
|
149
|
+
content = paragraph.runs&.map { |r| Array(r.text).map(&:to_s).join }&.join
|
|
150
|
+
if content && !content.strip.empty?
|
|
151
|
+
# Has content — transform normally (content comes before the break)
|
|
152
|
+
context.transform(paragraph)
|
|
153
|
+
else
|
|
154
|
+
# Standalone section break → thematic break
|
|
155
|
+
Coridoc::CoreModel::Block.new(
|
|
156
|
+
block_semantic_type: 'thematic_break'
|
|
157
|
+
)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Collect consecutive list items with the same numId into a ListBlock
|
|
162
|
+
def group_list(elements, start_index, context)
|
|
163
|
+
first = elements[start_index]
|
|
164
|
+
num_id = paragraph_num_id(first).to_i
|
|
165
|
+
items = []
|
|
166
|
+
consumed = 0
|
|
167
|
+
|
|
168
|
+
idx = start_index
|
|
169
|
+
while idx < elements.length
|
|
170
|
+
para = elements[idx]
|
|
171
|
+
break unless paragraph?(para)
|
|
172
|
+
break unless context.style_resolver.list_item?(para)
|
|
173
|
+
break unless paragraph_num_id(para).to_i == num_id
|
|
174
|
+
|
|
175
|
+
items << @list_item_rule.apply(para, context)
|
|
176
|
+
consumed += 1
|
|
177
|
+
idx += 1
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
list_block = Coridoc::CoreModel::ListBlock.new(
|
|
181
|
+
marker_type: context.numbering_resolver.marker_type(num_id),
|
|
182
|
+
items: items
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
# Return array so caller knows how many elements were consumed
|
|
186
|
+
consumed > 1 ? [list_block] + Array.new(consumed - 1, nil) : list_block
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def body_ordered_elements(body)
|
|
190
|
+
order = body.is_a?(Uniword::Wordprocessingml::Body) ? body.element_order : nil
|
|
191
|
+
return body.elements if order.nil? || order.empty?
|
|
192
|
+
|
|
193
|
+
p_idx = tbl_idx = sdt_idx = 0
|
|
194
|
+
order.filter_map do |entry|
|
|
195
|
+
name = entry.is_a?(String) ? entry : entry.name
|
|
196
|
+
case name
|
|
197
|
+
when 'p'
|
|
198
|
+
para = body.paragraphs[p_idx]
|
|
199
|
+
p_idx += 1
|
|
200
|
+
para
|
|
201
|
+
when 'tbl'
|
|
202
|
+
tbl = body.tables[tbl_idx]
|
|
203
|
+
tbl_idx += 1
|
|
204
|
+
tbl
|
|
205
|
+
when 'sdt'
|
|
206
|
+
sdt = body.structured_document_tags&.[](sdt_idx)
|
|
207
|
+
sdt_idx += 1
|
|
208
|
+
sdt
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def paragraph?(element)
|
|
214
|
+
defined?(Uniword::Wordprocessingml::Paragraph) &&
|
|
215
|
+
element.is_a?(Uniword::Wordprocessingml::Paragraph)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# uniword 1.7+ names the Footnote collection +footnote_entries+;
|
|
219
|
+
# 1.6-era shapes used +footnotes+. Read whichever exists.
|
|
220
|
+
def footnote_collection(part)
|
|
221
|
+
if part.class.method_defined?(:footnote_entries)
|
|
222
|
+
part.footnote_entries
|
|
223
|
+
else
|
|
224
|
+
part.footnotes
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# Footnote entries from the Uniword footnotes part. User notes
|
|
229
|
+
# only: separator/continuationSeparator entries carry w:type and
|
|
230
|
+
# no prose, so they never become definitions.
|
|
231
|
+
def footnote_entries(document)
|
|
232
|
+
part = document.footnotes
|
|
233
|
+
return [] unless defined?(Uniword::Wordprocessingml::Footnotes) &&
|
|
234
|
+
part.is_a?(Uniword::Wordprocessingml::Footnotes)
|
|
235
|
+
|
|
236
|
+
footnote_collection(part).reject(&:type)
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def collect_footnotes(document)
|
|
240
|
+
footnotes = {}
|
|
241
|
+
|
|
242
|
+
doc_footnotes = document.footnotes
|
|
243
|
+
if doc_footnotes.is_a?(Hash)
|
|
244
|
+
doc_footnotes.each do |id, fn|
|
|
245
|
+
paragraphs = fn.is_a?(Uniword::Wordprocessingml::Footnote) ? fn.paragraphs : fn[:content]
|
|
246
|
+
footnotes[id.to_s] = Array(paragraphs)
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
footnote_entries(document).each do |fn|
|
|
251
|
+
footnotes[fn.id&.to_s] = Array(fn.paragraphs || [])
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
footnotes
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Build CoreModel::Footnote definitions from the footnotes part.
|
|
258
|
+
# Content is the entry's paragraph run text (plain text; inline
|
|
259
|
+
# formatting inside footnotes is out of scope for the reference).
|
|
260
|
+
def footnote_definitions(document)
|
|
261
|
+
footnote_entries(document).filter_map do |fn|
|
|
262
|
+
id = fn.id&.to_s
|
|
263
|
+
next unless id
|
|
264
|
+
|
|
265
|
+
text = footnote_text(fn)
|
|
266
|
+
next if text.nil? || text.strip.empty?
|
|
267
|
+
|
|
268
|
+
CoreModel::Footnote.new(id: id, content: text)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def footnote_text(entry)
|
|
273
|
+
Array(entry.paragraphs).map do |para|
|
|
274
|
+
Array(para.runs).map { |r| Array(r.text).map(&:to_s).join }.join
|
|
275
|
+
end.join("\n")
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def extract_document_title(document, context)
|
|
279
|
+
body = document.body
|
|
280
|
+
return nil unless body
|
|
281
|
+
|
|
282
|
+
paragraphs = body.paragraphs || []
|
|
283
|
+
paragraphs.each do |para|
|
|
284
|
+
next unless context.style_resolver.heading?(para)
|
|
285
|
+
next unless context.style_resolver.heading_level(para) == 1
|
|
286
|
+
|
|
287
|
+
runs = para.runs || []
|
|
288
|
+
return runs.map { |r| Array(r.text).map(&:to_s).join }.join
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
nil
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def build_registry
|
|
295
|
+
registry = RuleRegistry.new
|
|
296
|
+
|
|
297
|
+
# Only register rules that don't need context for dispatch
|
|
298
|
+
registry.register(Rules::ParagraphRule.new)
|
|
299
|
+
registry.register(Rules::RunRule.new)
|
|
300
|
+
registry.register(Rules::TextRule.new)
|
|
301
|
+
registry.register(Rules::BreakRule.new)
|
|
302
|
+
registry.register(Rules::HyperlinkRule.new)
|
|
303
|
+
registry.register(Rules::ImageRule.new)
|
|
304
|
+
registry.register(Rules::FootnoteRule.new)
|
|
305
|
+
registry.register(Rules::BookmarkRule.new)
|
|
306
|
+
registry.register(Rules::TableRule.new)
|
|
307
|
+
registry.register(Rules::MathRule.new)
|
|
308
|
+
registry.register(Rules::StructuredDocumentTagRule.new)
|
|
309
|
+
registry.register(Rules::SimpleFieldRule.new)
|
|
310
|
+
registry.register(Rules::ProofErrorRule.new)
|
|
311
|
+
|
|
312
|
+
registry
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
# Extract semantic text from headers and footers.
|
|
316
|
+
# Discards purely layout text ("Page X of Y", page numbers, dates).
|
|
317
|
+
# Preserves meaningful text (title, version, confidentiality notices).
|
|
318
|
+
def extract_header_footer_metadata(document, core_doc)
|
|
319
|
+
extract_from_parts(document, :headers, 'header', core_doc)
|
|
320
|
+
extract_from_parts(document, :footers, 'footer', core_doc)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
def extract_from_parts(document, method, prefix, core_doc)
|
|
324
|
+
parts = case method
|
|
325
|
+
when :headers then document.headers
|
|
326
|
+
when :footers then document.footers
|
|
327
|
+
end
|
|
328
|
+
return unless parts
|
|
329
|
+
|
|
330
|
+
Array(parts).each_with_index do |part, idx|
|
|
331
|
+
text = extract_part_text(part)
|
|
332
|
+
next if text.nil? || text.strip.empty?
|
|
333
|
+
next if layout_only_text?(text)
|
|
334
|
+
|
|
335
|
+
part_type = if part.is_a?(Uniword::Wordprocessingml::Header) ||
|
|
336
|
+
part.is_a?(Uniword::Wordprocessingml::Footer)
|
|
337
|
+
part.type
|
|
338
|
+
else
|
|
339
|
+
idx
|
|
340
|
+
end
|
|
341
|
+
core_doc.set_metadata("docx.#{prefix}.#{part_type}", text.strip)
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def extract_part_text(part)
|
|
346
|
+
paragraphs = part.paragraphs || []
|
|
347
|
+
return nil unless paragraphs
|
|
348
|
+
|
|
349
|
+
paragraphs.map do |para|
|
|
350
|
+
extract_paragraph_text_content(para)
|
|
351
|
+
end.compact.join(' ').strip
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def extract_paragraph_text_content(para)
|
|
355
|
+
runs = para.runs || []
|
|
356
|
+
return nil unless runs
|
|
357
|
+
|
|
358
|
+
runs.map { |r| Array(r.text).map(&:to_s).join }.join
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# Check if header/footer text is purely layout content
|
|
362
|
+
# (page numbers, "Page X of Y", dates, etc.)
|
|
363
|
+
def layout_only_text?(text)
|
|
364
|
+
stripped = text.strip
|
|
365
|
+
return true if stripped.empty?
|
|
366
|
+
|
|
367
|
+
# Pure numbers (page numbers)
|
|
368
|
+
return true if stripped.match?(/\A\d+\z/)
|
|
369
|
+
|
|
370
|
+
# Common page number patterns
|
|
371
|
+
return true if stripped.match?(/\APage\s+\d+(\s+of\s+\d+)?\z/i)
|
|
372
|
+
|
|
373
|
+
# Pure date patterns
|
|
374
|
+
return true if stripped.match?(%r{\A\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\z})
|
|
375
|
+
return true if stripped.match?(%r{\A\d{4}[/-]\d{1,2}[/-]\d{1,2}\z})
|
|
376
|
+
|
|
377
|
+
false
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
autoload :Rule, 'coridoc/docx/transform/rule'
|
|
7
|
+
autoload :RuleRegistry, 'coridoc/docx/transform/rule_registry'
|
|
8
|
+
autoload :Context, 'coridoc/docx/transform/context'
|
|
9
|
+
autoload :ToCoreModel, 'coridoc/docx/transform/to_core_model'
|
|
10
|
+
autoload :FromCoreModel, 'coridoc/docx/transform/from_core_model'
|
|
11
|
+
autoload :StyleResolver, 'coridoc/docx/transform/style_resolver'
|
|
12
|
+
autoload :NumberingResolver, 'coridoc/docx/transform/numbering_resolver'
|
|
13
|
+
autoload :NumberingAccess, 'coridoc/docx/transform/numbering_access'
|
|
14
|
+
autoload :OrderedContent, 'coridoc/docx/transform/ordered_content'
|
|
15
|
+
autoload :FrontmatterCoreProperties, 'coridoc/docx/transform/frontmatter_core_properties'
|
|
16
|
+
|
|
17
|
+
# Element transform rules
|
|
18
|
+
module Rules
|
|
19
|
+
autoload :TextRule, 'coridoc/docx/transform/rules/text_rule'
|
|
20
|
+
autoload :BreakRule, 'coridoc/docx/transform/rules/break_rule'
|
|
21
|
+
autoload :RunRule, 'coridoc/docx/transform/rules/run_rule'
|
|
22
|
+
autoload :HyperlinkRule, 'coridoc/docx/transform/rules/hyperlink_rule'
|
|
23
|
+
autoload :ImageRule, 'coridoc/docx/transform/rules/image_rule'
|
|
24
|
+
autoload :FootnoteRule, 'coridoc/docx/transform/rules/footnote_rule'
|
|
25
|
+
autoload :HeadingRule, 'coridoc/docx/transform/rules/heading_rule'
|
|
26
|
+
autoload :ListItemRule, 'coridoc/docx/transform/rules/list_item_rule'
|
|
27
|
+
autoload :ParagraphRule, 'coridoc/docx/transform/rules/paragraph_rule'
|
|
28
|
+
autoload :TableRule, 'coridoc/docx/transform/rules/table_rule'
|
|
29
|
+
autoload :MathRule, 'coridoc/docx/transform/rules/math_rule'
|
|
30
|
+
autoload :BookmarkRule, 'coridoc/docx/transform/rules/bookmark_rule'
|
|
31
|
+
autoload :StructuredDocumentTagRule,
|
|
32
|
+
'coridoc/docx/transform/rules/structured_document_tag_rule'
|
|
33
|
+
autoload :SimpleFieldRule,
|
|
34
|
+
'coridoc/docx/transform/rules/simple_field_rule'
|
|
35
|
+
autoload :ProofErrorRule,
|
|
36
|
+
'coridoc/docx/transform/rules/proof_error_rule'
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/coridoc/docx.rb
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'coridoc'
|
|
4
|
+
require 'uniword'
|
|
5
|
+
|
|
6
|
+
# Coridoc::Docx provides DOCX (OOXML) format support for Coridoc.
|
|
7
|
+
#
|
|
8
|
+
# Transforms Uniword::Wordprocessingml model trees into
|
|
9
|
+
# Coridoc::CoreModel, enabling DOCX → AsciiDoc and DOCX → Markdown
|
|
10
|
+
# conversion via the hub-and-spoke architecture.
|
|
11
|
+
#
|
|
12
|
+
# @example Convert DOCX to AsciiDoc
|
|
13
|
+
# Coridoc.convert("input.docx", from: :docx, to: :asciidoc)
|
|
14
|
+
#
|
|
15
|
+
# @example Parse DOCX to CoreModel
|
|
16
|
+
# core = Coridoc::Docx.parse_to_core("input.docx")
|
|
17
|
+
#
|
|
18
|
+
module Coridoc
|
|
19
|
+
module Docx
|
|
20
|
+
autoload :VERSION, 'coridoc/docx/version'
|
|
21
|
+
autoload :Transform, 'coridoc/docx/transform'
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# Parse a DOCX input to CoreModel
|
|
25
|
+
#
|
|
26
|
+
# @param input [String, IO, Uniword::Wordprocessingml::DocumentRoot]
|
|
27
|
+
# Path to .docx file, IO stream, or pre-parsed Uniword document
|
|
28
|
+
# @param _options [Hash] additional options (reserved)
|
|
29
|
+
# @return [Coridoc::CoreModel::StructuralElement] CoreModel document
|
|
30
|
+
def parse_to_core(input, _options = {})
|
|
31
|
+
document = coerce_to_document(input)
|
|
32
|
+
Transform::ToCoreModel.transform(document)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Parse a DOCX input to Uniword model (no CoreModel conversion)
|
|
36
|
+
#
|
|
37
|
+
# @param input [String, IO] path to .docx file or IO stream
|
|
38
|
+
# @return [Uniword::Wordprocessingml::DocumentRoot] Uniword document model
|
|
39
|
+
def parse(input, _options = {})
|
|
40
|
+
coerce_to_document(input)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Whether this format supports serialization
|
|
44
|
+
def serialize?
|
|
45
|
+
true
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Serialize CoreModel to DOCX
|
|
49
|
+
#
|
|
50
|
+
# @param core_model [Coridoc::CoreModel::Base] CoreModel document
|
|
51
|
+
# @param options [Hash] serialization options
|
|
52
|
+
# @option options [String] :output_path Path to write .docx file
|
|
53
|
+
# @return [String, Uniword::Wordprocessingml::DocumentRoot]
|
|
54
|
+
# Returns the output path if :output_path given, otherwise the DocumentRoot
|
|
55
|
+
def serialize(core_model, **options)
|
|
56
|
+
document = Transform::FromCoreModel.transform(core_model)
|
|
57
|
+
|
|
58
|
+
if options[:output_path]
|
|
59
|
+
document.save(options[:output_path])
|
|
60
|
+
options[:output_path]
|
|
61
|
+
elsif options[:to_io]
|
|
62
|
+
io = options[:to_io]
|
|
63
|
+
document.save(io.path)
|
|
64
|
+
io
|
|
65
|
+
else
|
|
66
|
+
document
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def coerce_to_document(input)
|
|
73
|
+
case input
|
|
74
|
+
when Uniword::Wordprocessingml::DocumentRoot
|
|
75
|
+
input
|
|
76
|
+
when String
|
|
77
|
+
raise ArgumentError, "File not found: #{input}" unless File.exist?(input)
|
|
78
|
+
|
|
79
|
+
Uniword::DocumentFactory.from_file(input)
|
|
80
|
+
|
|
81
|
+
when IO, StringIO
|
|
82
|
+
Uniword::DocumentFactory.from_io(input)
|
|
83
|
+
else
|
|
84
|
+
raise ArgumentError,
|
|
85
|
+
"Expected file path, IO, or DocumentRoot, got #{input.class}"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Auto-register :docx format with Coridoc when both gems are loaded
|
|
93
|
+
unless Coridoc.registered_formats.include?(:docx)
|
|
94
|
+
Coridoc.register_format(:docx, Coridoc::Docx,
|
|
95
|
+
aliases: %w[docx],
|
|
96
|
+
extensions: %w[.docx],
|
|
97
|
+
binary: true)
|
|
98
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: coridoc-docx
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.6
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ribose Inc.
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: coridoc
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '3.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '3.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: lutaml-model
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 0.8.0
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 0.8.0
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: uniword
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.6'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.6'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: rake
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: rspec
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
- !ruby/object:Gem::Dependency
|
|
83
|
+
name: rubocop
|
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
type: :development
|
|
90
|
+
prerelease: false
|
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '0'
|
|
96
|
+
description: Provides OOXML (DOCX) to CoreModel transformation for the Coridoc document
|
|
97
|
+
transformation hub. Uses Uniword to read DOCX files and transforms the OOXML model
|
|
98
|
+
tree to Coridoc::CoreModel.
|
|
99
|
+
email:
|
|
100
|
+
- open.source@ribose.com
|
|
101
|
+
executables: []
|
|
102
|
+
extensions: []
|
|
103
|
+
extra_rdoc_files: []
|
|
104
|
+
files:
|
|
105
|
+
- README.adoc
|
|
106
|
+
- Rakefile
|
|
107
|
+
- lib/coridoc/docx.rb
|
|
108
|
+
- lib/coridoc/docx/transform.rb
|
|
109
|
+
- lib/coridoc/docx/transform/context.rb
|
|
110
|
+
- lib/coridoc/docx/transform/from_core_model.rb
|
|
111
|
+
- lib/coridoc/docx/transform/frontmatter_core_properties.rb
|
|
112
|
+
- lib/coridoc/docx/transform/numbering_access.rb
|
|
113
|
+
- lib/coridoc/docx/transform/numbering_resolver.rb
|
|
114
|
+
- lib/coridoc/docx/transform/ordered_content.rb
|
|
115
|
+
- lib/coridoc/docx/transform/rule.rb
|
|
116
|
+
- lib/coridoc/docx/transform/rule_registry.rb
|
|
117
|
+
- lib/coridoc/docx/transform/rules/bookmark_rule.rb
|
|
118
|
+
- lib/coridoc/docx/transform/rules/break_rule.rb
|
|
119
|
+
- lib/coridoc/docx/transform/rules/footnote_rule.rb
|
|
120
|
+
- lib/coridoc/docx/transform/rules/heading_rule.rb
|
|
121
|
+
- lib/coridoc/docx/transform/rules/hyperlink_rule.rb
|
|
122
|
+
- lib/coridoc/docx/transform/rules/image_rule.rb
|
|
123
|
+
- lib/coridoc/docx/transform/rules/list_item_rule.rb
|
|
124
|
+
- lib/coridoc/docx/transform/rules/math_rule.rb
|
|
125
|
+
- lib/coridoc/docx/transform/rules/paragraph_rule.rb
|
|
126
|
+
- lib/coridoc/docx/transform/rules/proof_error_rule.rb
|
|
127
|
+
- lib/coridoc/docx/transform/rules/run_rule.rb
|
|
128
|
+
- lib/coridoc/docx/transform/rules/simple_field_rule.rb
|
|
129
|
+
- lib/coridoc/docx/transform/rules/structured_document_tag_rule.rb
|
|
130
|
+
- lib/coridoc/docx/transform/rules/table_rule.rb
|
|
131
|
+
- lib/coridoc/docx/transform/rules/text_rule.rb
|
|
132
|
+
- lib/coridoc/docx/transform/style_resolver.rb
|
|
133
|
+
- lib/coridoc/docx/transform/to_core_model.rb
|
|
134
|
+
- lib/coridoc/docx/version.rb
|
|
135
|
+
homepage: https://github.com/coridoc/coridoc
|
|
136
|
+
licenses:
|
|
137
|
+
- MIT
|
|
138
|
+
metadata:
|
|
139
|
+
homepage_uri: https://github.com/coridoc/coridoc
|
|
140
|
+
source_code_uri: https://github.com/coridoc/coridoc
|
|
141
|
+
rubygems_mfa_required: 'true'
|
|
142
|
+
rdoc_options: []
|
|
143
|
+
require_paths:
|
|
144
|
+
- lib
|
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
146
|
+
requirements:
|
|
147
|
+
- - ">="
|
|
148
|
+
- !ruby/object:Gem::Version
|
|
149
|
+
version: 3.3.0
|
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
|
+
requirements:
|
|
152
|
+
- - ">="
|
|
153
|
+
- !ruby/object:Gem::Version
|
|
154
|
+
version: '0'
|
|
155
|
+
requirements: []
|
|
156
|
+
rubygems_version: 4.0.20
|
|
157
|
+
specification_version: 4
|
|
158
|
+
summary: DOCX (OOXML) format support for Coridoc
|
|
159
|
+
test_files: []
|