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,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:tbl (Table) elements to CoreModel::Table.
|
|
8
|
+
#
|
|
9
|
+
# Walks the OOXML table structure (Table → TableRow → TableCell)
|
|
10
|
+
# and produces the corresponding CoreModel tree.
|
|
11
|
+
#
|
|
12
|
+
# Cell paragraphs are transformed through the rule system to preserve
|
|
13
|
+
# inline formatting (bold, italic, links) as InlineElement objects.
|
|
14
|
+
# Print-layout properties (frame, grid, width) are NOT mapped — CoreModel
|
|
15
|
+
# is a semantic model, not a print layout language.
|
|
16
|
+
class TableRule < Rule
|
|
17
|
+
include OrderedContent
|
|
18
|
+
|
|
19
|
+
def matches?(element)
|
|
20
|
+
defined?(Uniword::Wordprocessingml::Table) &&
|
|
21
|
+
element.is_a?(Uniword::Wordprocessingml::Table)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def apply(table, context)
|
|
25
|
+
CoreModel::Table.new(
|
|
26
|
+
rows: table.rows.map { |r| transform_row(r, context) }
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def transform_row(row, context)
|
|
33
|
+
CoreModel::TableRow.new(
|
|
34
|
+
cells: row.cells.map { |c| transform_cell(c, context) },
|
|
35
|
+
header: row_header?(row)
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The persisted header flag lives in w:trPr/w:tblHeader
|
|
40
|
+
# (row.properties.tbl_header); TableRow#header? is a runtime-only
|
|
41
|
+
# accessor that deserialized documents never populate.
|
|
42
|
+
def row_header?(row)
|
|
43
|
+
tbl_header = row.properties&.tbl_header
|
|
44
|
+
return row.header? unless tbl_header.is_a?(Uniword::Wordprocessingml::ValInt)
|
|
45
|
+
|
|
46
|
+
# <w:tblHeader/> without w:val means "on" (ST_OnOff semantics)
|
|
47
|
+
return true if tbl_header.value.nil?
|
|
48
|
+
|
|
49
|
+
tbl_header.value.to_i.positive?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def transform_cell(cell, context)
|
|
53
|
+
inline_children = cell_paragraphs(cell).flat_map do |para|
|
|
54
|
+
extract_inline_from_paragraph(para, context)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
props = cell.properties
|
|
58
|
+
|
|
59
|
+
CoreModel::TableCell.new(
|
|
60
|
+
content: extract_plain_text(inline_children),
|
|
61
|
+
alignment: props&.vertical_align&.to_s,
|
|
62
|
+
colspan: cell.column_span,
|
|
63
|
+
rowspan: cell.row_span,
|
|
64
|
+
header: header_cell?(cell),
|
|
65
|
+
children: inline_children
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Transform a cell paragraph and extract its inline children
|
|
70
|
+
def extract_inline_from_paragraph(para, context)
|
|
71
|
+
transformed = context.transform(para)
|
|
72
|
+
return [] unless transformed
|
|
73
|
+
|
|
74
|
+
# If it's a Block with children (inline elements), extract them
|
|
75
|
+
if transformed.is_a?(CoreModel::Block) && transformed.children.any?
|
|
76
|
+
transformed.children
|
|
77
|
+
elsif transformed.is_a?(CoreModel::Block)
|
|
78
|
+
[transformed.content].compact
|
|
79
|
+
else
|
|
80
|
+
[transformed]
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def cell_paragraphs(cell)
|
|
85
|
+
cell.paragraphs || []
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def header_cell?(cell)
|
|
89
|
+
return false unless cell.properties
|
|
90
|
+
|
|
91
|
+
vm = cell.properties.v_merge
|
|
92
|
+
vm&.value.to_s == 'restart'
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:t (Text) elements to plain strings.
|
|
8
|
+
#
|
|
9
|
+
# Text is returned as a raw string — not wrapped in a CoreModel node.
|
|
10
|
+
# The caller (RunRule) is responsible for wrapping in InlineElement
|
|
11
|
+
# when formatting is present.
|
|
12
|
+
class TextRule < Rule
|
|
13
|
+
def matches?(element)
|
|
14
|
+
defined?(Uniword::Wordprocessingml::Text) &&
|
|
15
|
+
element.is_a?(Uniword::Wordprocessingml::Text)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def apply(text, _context)
|
|
19
|
+
text.content.to_s
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Resolves paragraph and run styles to semantic roles.
|
|
7
|
+
#
|
|
8
|
+
# OOXML paragraphs don't have explicit element types. Instead, their
|
|
9
|
+
# meaning is determined by style references (e.g., "Heading1" → section)
|
|
10
|
+
# or by formatting properties (e.g., numPr → list item).
|
|
11
|
+
#
|
|
12
|
+
# StyleResolver centralizes this detection so HeadingRule, ListItemRule,
|
|
13
|
+
# and ParagraphRule don't duplicate the logic.
|
|
14
|
+
#
|
|
15
|
+
# The style map is built from the Uniword StylesConfiguration by walking
|
|
16
|
+
# all style definitions and their basedOn chains.
|
|
17
|
+
class StyleResolver
|
|
18
|
+
include NumberingAccess
|
|
19
|
+
|
|
20
|
+
HEADING_PATTERN = /^(heading|heading|h)\s*(\d+)$/i
|
|
21
|
+
QUOTE_PATTERN = /\bquote\b/i
|
|
22
|
+
CODE_PATTERN = /\b(code|source|listing)\b/i
|
|
23
|
+
LITERAL_PATTERN = /\bliteral\b/i
|
|
24
|
+
EXAMPLE_PATTERN = /\bexample\b/i
|
|
25
|
+
|
|
26
|
+
# @param styles_configuration [Object, nil] Uniword styles configuration
|
|
27
|
+
def initialize(styles_configuration)
|
|
28
|
+
@config = styles_configuration
|
|
29
|
+
@style_map = build_style_map(styles_configuration)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Determine the semantic role of a paragraph
|
|
33
|
+
#
|
|
34
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
35
|
+
# @return [Symbol] :heading, :list_item, :quote, :source, :literal,
|
|
36
|
+
# :example, or :paragraph
|
|
37
|
+
def semantic_role(paragraph)
|
|
38
|
+
return :heading if heading?(paragraph)
|
|
39
|
+
return :list_item if list_item?(paragraph)
|
|
40
|
+
|
|
41
|
+
style_role = role_from_style(paragraph)
|
|
42
|
+
return style_role if style_role
|
|
43
|
+
|
|
44
|
+
:paragraph
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Check if paragraph is a heading
|
|
48
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
49
|
+
# @return [Boolean]
|
|
50
|
+
def heading?(paragraph)
|
|
51
|
+
return false unless paragraph.properties
|
|
52
|
+
|
|
53
|
+
style_name = resolve_style_name(paragraph)
|
|
54
|
+
return true if style_name && HEADING_PATTERN.match?(style_name)
|
|
55
|
+
|
|
56
|
+
ol = paragraph.properties.outline_level
|
|
57
|
+
if ol
|
|
58
|
+
ol_level = ol.is_a?(Uniword::Properties::OutlineLevel) ? ol.value.to_i : ol.to_i
|
|
59
|
+
return true if ol_level.positive?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
style = find_style_for_paragraph(paragraph)
|
|
63
|
+
if style&.outline_level
|
|
64
|
+
ol_val = style.outline_level
|
|
65
|
+
ol_val = ol_val.is_a?(Uniword::Properties::OutlineLevel) ? ol_val.value.to_i : ol_val.to_i
|
|
66
|
+
return true if ol_val.positive?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
false
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Get heading level (1-6) or nil
|
|
73
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
74
|
+
# @return [Integer, nil]
|
|
75
|
+
def heading_level(paragraph)
|
|
76
|
+
style_name = resolve_style_name(paragraph)
|
|
77
|
+
if style_name
|
|
78
|
+
match = HEADING_PATTERN.match(style_name)
|
|
79
|
+
return match[2].to_i if match
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Check outline_level on paragraph properties
|
|
83
|
+
ol = paragraph.properties&.outline_level
|
|
84
|
+
if ol
|
|
85
|
+
level = ol.is_a?(Uniword::Properties::OutlineLevel) ? ol.value.to_i : ol.to_i
|
|
86
|
+
return level if level.positive?
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Check if paragraph is a list item
|
|
93
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
94
|
+
# @return [Boolean]
|
|
95
|
+
def list_item?(paragraph)
|
|
96
|
+
return false unless paragraph.properties
|
|
97
|
+
|
|
98
|
+
# w:numPr is persisted as nested numbering_properties; the flat
|
|
99
|
+
# num_id is only set by the builder/YAML API (see NumberingAccess)
|
|
100
|
+
num_id = paragraph_num_id(paragraph)
|
|
101
|
+
!num_id.nil? && num_id.positive?
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Check if paragraph has a specific role based on style name
|
|
105
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
106
|
+
# @return [Symbol, nil]
|
|
107
|
+
def role_from_style(paragraph)
|
|
108
|
+
style_name = resolve_style_name(paragraph)
|
|
109
|
+
return nil unless style_name
|
|
110
|
+
|
|
111
|
+
case style_name
|
|
112
|
+
when QUOTE_PATTERN then :quote
|
|
113
|
+
when CODE_PATTERN then :source
|
|
114
|
+
when LITERAL_PATTERN then :literal
|
|
115
|
+
when EXAMPLE_PATTERN then :example
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Detect semantic role of a run based on its rStyle
|
|
120
|
+
# @param run [Uniword::Wordprocessingml::Run]
|
|
121
|
+
# @return [Symbol, nil]
|
|
122
|
+
def run_semantic_role(run)
|
|
123
|
+
return nil unless run.properties
|
|
124
|
+
return nil unless run.properties.style
|
|
125
|
+
|
|
126
|
+
style_name = resolve_run_style_name(run)
|
|
127
|
+
return nil unless style_name
|
|
128
|
+
|
|
129
|
+
case style_name
|
|
130
|
+
when /\b(code|verbatim|teletype|keyboard)\b/i then :monospace
|
|
131
|
+
when /\bstrong\b/i then :bold
|
|
132
|
+
when /\b(emphasis|em)\b/i then :italic
|
|
133
|
+
when /\bcitation\b/i then :italic
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
private
|
|
138
|
+
|
|
139
|
+
def resolve_style_name(paragraph)
|
|
140
|
+
value = paragraph_style_value(paragraph)
|
|
141
|
+
return nil unless value
|
|
142
|
+
|
|
143
|
+
mapped = @style_map[value]
|
|
144
|
+
return mapped if mapped
|
|
145
|
+
|
|
146
|
+
value
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def resolve_run_style_name(run)
|
|
150
|
+
style_ref = run.properties.style
|
|
151
|
+
return nil unless style_ref
|
|
152
|
+
|
|
153
|
+
value = style_ref.value.to_s
|
|
154
|
+
return nil unless value
|
|
155
|
+
|
|
156
|
+
mapped = @style_map[value]
|
|
157
|
+
mapped || value
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def find_style_for_paragraph(paragraph)
|
|
161
|
+
return nil unless @config
|
|
162
|
+
|
|
163
|
+
style_id = style_id_from_paragraph(paragraph)
|
|
164
|
+
return nil unless style_id
|
|
165
|
+
|
|
166
|
+
return unless @config.is_a?(Uniword::Wordprocessingml::StylesConfiguration)
|
|
167
|
+
|
|
168
|
+
@config.style_by_id(style_id)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def style_id_from_paragraph(paragraph)
|
|
172
|
+
paragraph_style_value(paragraph)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# uniword models w:pStyle as a collection of StyleReference
|
|
176
|
+
def paragraph_style_value(paragraph)
|
|
177
|
+
Array(paragraph.properties&.style).filter_map(&:value).first.to_s
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def build_style_map(config)
|
|
181
|
+
return {} unless config
|
|
182
|
+
return {} unless config.is_a?(Uniword::Wordprocessingml::StylesConfiguration)
|
|
183
|
+
|
|
184
|
+
map = {}
|
|
185
|
+
config.styles.each do |style|
|
|
186
|
+
id = style.styleId
|
|
187
|
+
name = extract_style_name(style)
|
|
188
|
+
next unless id && name
|
|
189
|
+
|
|
190
|
+
map[id] = name
|
|
191
|
+
|
|
192
|
+
if heading_by_based_on?(config, style)
|
|
193
|
+
level = heading_level_from_chain(config, style)
|
|
194
|
+
map[id] = "Heading#{level}"
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
map
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def extract_style_name(style)
|
|
202
|
+
sn = style.style_name
|
|
203
|
+
return sn if sn
|
|
204
|
+
|
|
205
|
+
name = style.name
|
|
206
|
+
return nil unless name
|
|
207
|
+
|
|
208
|
+
name.is_a?(Uniword::Wordprocessingml::StyleName) ? name.val.to_s : name.to_s
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def heading_by_based_on?(config, style)
|
|
212
|
+
based_on = style.based_on
|
|
213
|
+
return false unless based_on
|
|
214
|
+
|
|
215
|
+
visited = Set.new
|
|
216
|
+
current = style
|
|
217
|
+
while current && !visited.include?(current.styleId)
|
|
218
|
+
visited << current.styleId
|
|
219
|
+
parent_id = current.based_on
|
|
220
|
+
return true if parent_id && HEADING_PATTERN.match?(parent_id)
|
|
221
|
+
|
|
222
|
+
break unless parent_id
|
|
223
|
+
|
|
224
|
+
current = config.styles.find { |s| s.styleId == parent_id }
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
false
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def heading_level_from_chain(config, style)
|
|
231
|
+
visited = Set.new
|
|
232
|
+
current = style
|
|
233
|
+
while current && !visited.include?(current.styleId)
|
|
234
|
+
visited << current.styleId
|
|
235
|
+
name = extract_style_name(current)
|
|
236
|
+
if name
|
|
237
|
+
match = HEADING_PATTERN.match(name)
|
|
238
|
+
return match[2].to_i if match
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
parent_id = current.based_on
|
|
242
|
+
break unless parent_id
|
|
243
|
+
|
|
244
|
+
current = config.styles.find { |s| s.styleId == parent_id }
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
1
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|