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,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:drawing and w:pict elements to CoreModel::Image.
|
|
8
|
+
#
|
|
9
|
+
# Extracts image reference data (relationship ID, dimensions, alt text).
|
|
10
|
+
# Binary data extraction is handled by the caller via the image_refs
|
|
11
|
+
# list in Context.
|
|
12
|
+
class ImageRule < Rule
|
|
13
|
+
def matches?(element)
|
|
14
|
+
return false unless defined?(Uniword::Wordprocessingml)
|
|
15
|
+
|
|
16
|
+
element.is_a?(Uniword::Wordprocessingml::Drawing) ||
|
|
17
|
+
element.is_a?(Uniword::Wordprocessingml::Picture)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def apply(element, context)
|
|
21
|
+
ref = extract_reference(element)
|
|
22
|
+
context.register_image(ref)
|
|
23
|
+
|
|
24
|
+
CoreModel::Image.new(
|
|
25
|
+
src: ref[:src],
|
|
26
|
+
alt: ref[:alt],
|
|
27
|
+
width: ref[:width],
|
|
28
|
+
height: ref[:height],
|
|
29
|
+
inline: ref[:inline]
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def extract_reference(element)
|
|
36
|
+
case element
|
|
37
|
+
when Uniword::Wordprocessingml::Drawing
|
|
38
|
+
extract_drawing_ref(element)
|
|
39
|
+
when Uniword::Wordprocessingml::Picture
|
|
40
|
+
extract_picture_ref(element)
|
|
41
|
+
else
|
|
42
|
+
{ src: nil, alt: nil, width: nil, height: nil, inline: true }
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def extract_drawing_ref(drawing)
|
|
47
|
+
if drawing.inline
|
|
48
|
+
extract_inline_ref(drawing.inline)
|
|
49
|
+
elsif drawing.anchor
|
|
50
|
+
extract_anchor_ref(drawing.anchor)
|
|
51
|
+
else
|
|
52
|
+
{ src: nil, alt: nil, width: nil, height: nil, inline: true }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def extract_inline_ref(inline)
|
|
57
|
+
extent = inline.extent
|
|
58
|
+
doc_pr = inline.doc_properties
|
|
59
|
+
graphic = inline.graphic
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
src: extract_embed_ref(graphic),
|
|
63
|
+
alt: doc_pr&.name&.to_s || doc_pr&.id&.to_s,
|
|
64
|
+
width: extent_to_px(extent, :cx),
|
|
65
|
+
height: extent_to_px(extent, :cy),
|
|
66
|
+
inline: true
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def extract_anchor_ref(anchor)
|
|
71
|
+
extent = anchor.extent
|
|
72
|
+
doc_pr = anchor.doc_properties
|
|
73
|
+
graphic = anchor.graphic
|
|
74
|
+
|
|
75
|
+
{
|
|
76
|
+
src: extract_embed_ref(graphic),
|
|
77
|
+
alt: doc_pr&.name&.to_s || doc_pr&.id&.to_s,
|
|
78
|
+
width: extent_to_px(extent, :cx),
|
|
79
|
+
height: extent_to_px(extent, :cy),
|
|
80
|
+
inline: false
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def extract_picture_ref(_pict)
|
|
85
|
+
# VML-based pictures — less common, extract basic info
|
|
86
|
+
{ src: nil, alt: nil, width: nil, height: nil, inline: true }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def extract_embed_ref(graphic)
|
|
90
|
+
return nil unless graphic
|
|
91
|
+
|
|
92
|
+
graphic_data = graphic.graphic_data
|
|
93
|
+
return nil unless graphic_data
|
|
94
|
+
|
|
95
|
+
# Navigate: GraphicData → Picture → BlipFill → Blip → embed
|
|
96
|
+
picture = graphic_data.picture
|
|
97
|
+
return nil unless picture
|
|
98
|
+
|
|
99
|
+
blip_fill = picture.blip_fill
|
|
100
|
+
return nil unless blip_fill
|
|
101
|
+
|
|
102
|
+
blip = blip_fill.blip
|
|
103
|
+
blip&.embed
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# OOXML uses EMU (English Metric Units): 1 inch = 914400 EMU
|
|
107
|
+
EMU_PER_PX = 9525
|
|
108
|
+
|
|
109
|
+
def extent_to_px(extent, dimension)
|
|
110
|
+
return nil unless extent
|
|
111
|
+
|
|
112
|
+
value = case dimension
|
|
113
|
+
when :cx then extent.cx
|
|
114
|
+
when :cy then extent.cy
|
|
115
|
+
end
|
|
116
|
+
return nil unless value
|
|
117
|
+
|
|
118
|
+
px = value.to_i / EMU_PER_PX
|
|
119
|
+
px.positive? ? "#{px}px" : nil
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms list-item paragraphs to CoreModel::ListItem.
|
|
8
|
+
#
|
|
9
|
+
# Each paragraph with numPr (numbering properties) becomes a ListItem.
|
|
10
|
+
# The ToCoreModel orchestrator groups consecutive items with the same
|
|
11
|
+
# numId into a single ListBlock.
|
|
12
|
+
#
|
|
13
|
+
# Children are stored as InlineElement objects (via transform_paragraph_content)
|
|
14
|
+
# while content is the plain text representation.
|
|
15
|
+
#
|
|
16
|
+
# This rule is NOT registered in the RuleRegistry — the orchestrator
|
|
17
|
+
# dispatches directly after checking style_resolver.list_item?.
|
|
18
|
+
class ListItemRule < Rule
|
|
19
|
+
include OrderedContent
|
|
20
|
+
include NumberingAccess
|
|
21
|
+
|
|
22
|
+
def matches?(_element)
|
|
23
|
+
false # Never auto-matched; orchestrator dispatches directly
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def apply(paragraph, context)
|
|
27
|
+
ilvl = paragraph_ilvl(paragraph).to_i
|
|
28
|
+
|
|
29
|
+
children = transform_paragraph_content(paragraph, context)
|
|
30
|
+
|
|
31
|
+
item = CoreModel::ListItem.new(
|
|
32
|
+
marker: marker_for(ilvl),
|
|
33
|
+
content: extract_plain_text(children)
|
|
34
|
+
)
|
|
35
|
+
item.children = children
|
|
36
|
+
item
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def marker_for(level)
|
|
42
|
+
level.zero? ? '*' : '*' * (level + 1)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms OMML math elements to CoreModel.
|
|
8
|
+
#
|
|
9
|
+
# Display math (MathEquation#block?) → CoreModel::Block (stem)
|
|
10
|
+
# Inline math (MathEquation#inline?) → CoreModel::InlineElement (stem)
|
|
11
|
+
#
|
|
12
|
+
# LaTeX conversion is provided natively by Uniword::MathEquation.
|
|
13
|
+
class MathRule < Rule
|
|
14
|
+
def matches?(element)
|
|
15
|
+
defined?(Uniword::MathEquation) &&
|
|
16
|
+
element.is_a?(Uniword::MathEquation)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def apply(element, _context)
|
|
20
|
+
latex = element.to_latex.to_s
|
|
21
|
+
|
|
22
|
+
if element.block?
|
|
23
|
+
CoreModel::PassBlock.new(
|
|
24
|
+
delimiter_type: '++++',
|
|
25
|
+
language: 'latexmath',
|
|
26
|
+
content: latex
|
|
27
|
+
)
|
|
28
|
+
else
|
|
29
|
+
CoreModel::InlineElement.new(
|
|
30
|
+
format_type: 'stem',
|
|
31
|
+
content: latex
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms regular paragraphs to CoreModel::Block.
|
|
8
|
+
#
|
|
9
|
+
# Handles paragraph-style detection for block types:
|
|
10
|
+
# quote, source, literal, example, or plain paragraph.
|
|
11
|
+
#
|
|
12
|
+
# This is the default rule for paragraphs. The orchestrator dispatches
|
|
13
|
+
# heading and list item paragraphs directly, so this rule only sees
|
|
14
|
+
# regular paragraphs.
|
|
15
|
+
class ParagraphRule < Rule
|
|
16
|
+
include OrderedContent
|
|
17
|
+
|
|
18
|
+
def priority
|
|
19
|
+
0
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def matches?(element)
|
|
23
|
+
defined?(Uniword::Wordprocessingml::Paragraph) &&
|
|
24
|
+
element.is_a?(Uniword::Wordprocessingml::Paragraph)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def apply(paragraph, context)
|
|
28
|
+
role = context.style_resolver.role_from_style(paragraph)
|
|
29
|
+
block_type = block_type_for(role)
|
|
30
|
+
|
|
31
|
+
children = transform_paragraph_content(paragraph, context)
|
|
32
|
+
id = extract_bookmark_id(paragraph)
|
|
33
|
+
|
|
34
|
+
block = CoreModel::Block.new(
|
|
35
|
+
block_semantic_type: block_type,
|
|
36
|
+
content: extract_plain_text(children)
|
|
37
|
+
)
|
|
38
|
+
block.children = children
|
|
39
|
+
block.id = id if id
|
|
40
|
+
block
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def block_type_for(role)
|
|
46
|
+
case role
|
|
47
|
+
when :quote then 'quote'
|
|
48
|
+
when :source then 'source'
|
|
49
|
+
when :literal then 'literal'
|
|
50
|
+
when :example then 'example'
|
|
51
|
+
else 'paragraph'
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def extract_bookmark_id(paragraph)
|
|
56
|
+
starts = paragraph.bookmark_starts
|
|
57
|
+
return nil if starts.nil? || starts.empty?
|
|
58
|
+
|
|
59
|
+
starts.first.id&.to_s
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Silently ignores w:proofErr (proofing error) elements.
|
|
8
|
+
#
|
|
9
|
+
# Proofing errors are spelling/grammar markers in OOXML that have
|
|
10
|
+
# no semantic representation in CoreModel. This rule matches them
|
|
11
|
+
# and returns nil, effectively stripping them from the output.
|
|
12
|
+
class ProofErrorRule < Rule
|
|
13
|
+
def matches?(element)
|
|
14
|
+
defined?(Uniword::Wordprocessingml::ProofError) &&
|
|
15
|
+
element.is_a?(Uniword::Wordprocessingml::ProofError)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def apply(_element, _context)
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:r (Run) elements to InlineElement or String.
|
|
8
|
+
#
|
|
9
|
+
# Runs with formatting become CoreModel::InlineElement nodes.
|
|
10
|
+
# Plain runs (no formatting properties) return their text directly.
|
|
11
|
+
#
|
|
12
|
+
# A single run may carry multiple formatting properties (e.g., bold +
|
|
13
|
+
# italic). The most specific one wins for format_type, while the
|
|
14
|
+
# text content is preserved.
|
|
15
|
+
#
|
|
16
|
+
# Uses effective_run_properties (when available) to resolve style
|
|
17
|
+
# inheritance: explicit properties > paragraph style's rPr > basedOn chain.
|
|
18
|
+
# Falls back to run.properties for backward compatibility.
|
|
19
|
+
class RunRule < Rule
|
|
20
|
+
def matches?(element)
|
|
21
|
+
defined?(Uniword::Wordprocessingml::Run) &&
|
|
22
|
+
element.is_a?(Uniword::Wordprocessingml::Run)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def apply(run, context)
|
|
26
|
+
# Delegate non-text children (breaks, drawings, footnotes, etc.)
|
|
27
|
+
non_text = extract_non_text_children(run, context)
|
|
28
|
+
return non_text.first if non_text.any? && Array(run.text).empty?
|
|
29
|
+
|
|
30
|
+
# w:r may carry several w:t children (uniword models text as
|
|
31
|
+
# a collection) — join them in document order
|
|
32
|
+
text = Array(run.text).map { |node| node&.content.to_s }.join
|
|
33
|
+
return '' if text.empty? && non_text.empty?
|
|
34
|
+
|
|
35
|
+
props = effective_props(run)
|
|
36
|
+
return text if plain_run?(props)
|
|
37
|
+
|
|
38
|
+
fmt = format_type(props, run, context)
|
|
39
|
+
return text unless fmt
|
|
40
|
+
|
|
41
|
+
CoreModel::InlineElement.new(
|
|
42
|
+
format_type: fmt,
|
|
43
|
+
content: text
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def effective_props(run)
|
|
50
|
+
ep = run.effective_run_properties
|
|
51
|
+
return ep if ep
|
|
52
|
+
|
|
53
|
+
run.properties
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def extract_non_text_children(run, context)
|
|
57
|
+
result = []
|
|
58
|
+
|
|
59
|
+
result.concat(Array(run.break).map { |brk| context.transform(brk) })
|
|
60
|
+
|
|
61
|
+
result << context.transform(run.footnote_reference) if run.footnote_reference
|
|
62
|
+
|
|
63
|
+
result << context.transform(run.endnote_reference) if run.endnote_reference
|
|
64
|
+
|
|
65
|
+
run.drawings&.each do |drawing|
|
|
66
|
+
result << context.transform(drawing)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
result << "\t" if run.tab
|
|
70
|
+
|
|
71
|
+
result << context.transform(run.o_math) if run.class.attributes.key?(:o_math) && run.o_math
|
|
72
|
+
|
|
73
|
+
if run.del_text
|
|
74
|
+
text = run.del_text.is_a?(Uniword::Wordprocessingml::DeletedText) ? run.del_text.content.to_s : run.del_text.to_s
|
|
75
|
+
unless text.empty?
|
|
76
|
+
result << CoreModel::InlineElement.new(
|
|
77
|
+
format_type: 'strikethrough',
|
|
78
|
+
content: text
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
if run.sym
|
|
84
|
+
char = run.sym.char
|
|
85
|
+
result << char.to_s if char && !char.empty?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
result << "\u2011" if run.no_break_hyphen
|
|
89
|
+
|
|
90
|
+
result << "\u00AD" if run.class.attributes.key?(:soft_hyphen) && run.soft_hyphen
|
|
91
|
+
|
|
92
|
+
result << CoreModel::InlineElement.new(format_type: 'hard_line_break') if run.class.attributes.key?(:carriage_return) && run.carriage_return
|
|
93
|
+
|
|
94
|
+
if run.alternate_content
|
|
95
|
+
result << extract_alternate_content(run.alternate_content,
|
|
96
|
+
context)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
result.compact
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def extract_alternate_content(ac, context)
|
|
103
|
+
content = if ac.fallback
|
|
104
|
+
ac.fallback
|
|
105
|
+
elsif ac.choice
|
|
106
|
+
ac.choice
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
return nil unless content
|
|
110
|
+
|
|
111
|
+
if content.is_a?(Uniword::Wordprocessingml::Run)
|
|
112
|
+
content.runs&.each { |r| context.transform(r) }
|
|
113
|
+
elsif content.is_a?(Uniword::Wordprocessingml::Paragraph)
|
|
114
|
+
content.paragraphs&.flat_map { |p| context.transform(p) }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def plain_run?(props)
|
|
119
|
+
return true unless props
|
|
120
|
+
|
|
121
|
+
props.bold.nil? &&
|
|
122
|
+
props.italic.nil? &&
|
|
123
|
+
props.underline.nil? &&
|
|
124
|
+
props.strike.nil? &&
|
|
125
|
+
props.double_strike.nil? &&
|
|
126
|
+
props.vertical_align.nil? &&
|
|
127
|
+
props.small_caps.nil? &&
|
|
128
|
+
props.caps.nil? &&
|
|
129
|
+
props.hidden.nil? &&
|
|
130
|
+
props.highlight.nil?
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Determine the dominant format type.
|
|
134
|
+
# Checks rStyle-based semantic detection first, then explicit formatting.
|
|
135
|
+
def format_type(props, run, context)
|
|
136
|
+
return nil unless props
|
|
137
|
+
|
|
138
|
+
# Check rStyle for semantic role
|
|
139
|
+
if context.style_resolver.is_a?(Coridoc::Docx::Transform::StyleResolver)
|
|
140
|
+
role = context.style_resolver.run_semantic_role(run)
|
|
141
|
+
case role
|
|
142
|
+
when :monospace then return 'monospace'
|
|
143
|
+
when :bold then return 'bold'
|
|
144
|
+
when :italic then return 'italic'
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Explicit formatting properties
|
|
149
|
+
if bold?(props)
|
|
150
|
+
'bold'
|
|
151
|
+
elsif italic?(props)
|
|
152
|
+
'italic'
|
|
153
|
+
elsif props.underline
|
|
154
|
+
'underline'
|
|
155
|
+
elsif props.strike || props.double_strike
|
|
156
|
+
'strikethrough'
|
|
157
|
+
elsif subscript?(props)
|
|
158
|
+
'subscript'
|
|
159
|
+
elsif superscript?(props)
|
|
160
|
+
'superscript'
|
|
161
|
+
elsif props.small_caps
|
|
162
|
+
'small'
|
|
163
|
+
elsif props.caps
|
|
164
|
+
'bold'
|
|
165
|
+
elsif props.highlight
|
|
166
|
+
'highlight'
|
|
167
|
+
elsif props.hidden
|
|
168
|
+
nil
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def bold?(props)
|
|
173
|
+
props.bold && props.bold.value != false
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def italic?(props)
|
|
177
|
+
props.italic && props.italic.value != false
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def subscript?(props)
|
|
181
|
+
props.vertical_align&.value.to_s == 'subscript'
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def superscript?(props)
|
|
185
|
+
props.vertical_align&.value.to_s == 'superscript'
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:fldSimple (simple field) elements.
|
|
8
|
+
#
|
|
9
|
+
# Simple fields include page numbers, dates, document properties,
|
|
10
|
+
# and other computed content. This rule extracts the field's text
|
|
11
|
+
# content when available, otherwise produces the instruction text.
|
|
12
|
+
#
|
|
13
|
+
# Common field types:
|
|
14
|
+
# - PAGE → current page number
|
|
15
|
+
# - NUMPAGES → total page count
|
|
16
|
+
# - DATE → current date
|
|
17
|
+
# - TIME → current time
|
|
18
|
+
# - DOCPROPERTY → document property value
|
|
19
|
+
# - TITLE → document title
|
|
20
|
+
# - AUTHOR → document author
|
|
21
|
+
class SimpleFieldRule < Rule
|
|
22
|
+
def matches?(element)
|
|
23
|
+
defined?(Uniword::Wordprocessingml::SimpleField) &&
|
|
24
|
+
element.is_a?(Uniword::Wordprocessingml::SimpleField)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def apply(field, _context)
|
|
28
|
+
# Try to get the resolved text content first
|
|
29
|
+
text = field_text(field)
|
|
30
|
+
return nil if text.nil? || text.empty?
|
|
31
|
+
|
|
32
|
+
# Check if this is a semantic field we should preserve
|
|
33
|
+
instr = field_instruction(field)
|
|
34
|
+
case instr
|
|
35
|
+
when /\A(TITLE|AUTHOR|SUBJECT|KEYWORDS|DOCPROPERTY)\b/i
|
|
36
|
+
# Document metadata — embed as plain text (already resolved)
|
|
37
|
+
text
|
|
38
|
+
when /\A(PAGE|NUMPAGES)\b/i
|
|
39
|
+
# Page layout fields — skip (not semantic)
|
|
40
|
+
nil
|
|
41
|
+
when /\A(HYPERLINK)\b/i
|
|
42
|
+
# Hyperlink field — extract URL and text
|
|
43
|
+
url = extract_hyperlink_url(instr)
|
|
44
|
+
if url
|
|
45
|
+
CoreModel::InlineElement.new(
|
|
46
|
+
format_type: 'link',
|
|
47
|
+
content: text,
|
|
48
|
+
target: url
|
|
49
|
+
)
|
|
50
|
+
else
|
|
51
|
+
text
|
|
52
|
+
end
|
|
53
|
+
when /\A(TOC|PAGEREF|REF|NOTEREF)\b/i
|
|
54
|
+
# TOC / cross-reference fields — skip (print layout)
|
|
55
|
+
nil
|
|
56
|
+
else
|
|
57
|
+
# Generic field — pass through as text
|
|
58
|
+
text
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def field_text(field)
|
|
65
|
+
if field.runs && !field.runs.empty?
|
|
66
|
+
return field.runs.map do |r|
|
|
67
|
+
Array(r.text).map(&:to_s).join
|
|
68
|
+
end.join
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def field_instruction(field)
|
|
75
|
+
instr = field.instr
|
|
76
|
+
instr.to_s
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def extract_hyperlink_url(instr)
|
|
80
|
+
match = instr.match(/HYPERLINK\s+"([^"]+)"/i)
|
|
81
|
+
match&.[](1)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:sdt (Structured Document Tag) elements.
|
|
8
|
+
#
|
|
9
|
+
# SDTs wrap content with additional metadata. The transform
|
|
10
|
+
# unwraps them and delegates to the content's own rules.
|
|
11
|
+
class StructuredDocumentTagRule < Rule
|
|
12
|
+
def matches?(element)
|
|
13
|
+
defined?(Uniword::Wordprocessingml::StructuredDocumentTag) &&
|
|
14
|
+
element.is_a?(Uniword::Wordprocessingml::StructuredDocumentTag)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def apply(sdt, context)
|
|
18
|
+
# SDTs contain paragraphs and tables — delegate to their rules
|
|
19
|
+
# via the context's transform method
|
|
20
|
+
return nil unless sdt.content
|
|
21
|
+
|
|
22
|
+
paragraphs = sdt.content.paragraphs || []
|
|
23
|
+
tables = sdt.content.tables || []
|
|
24
|
+
|
|
25
|
+
results = []
|
|
26
|
+
paragraphs.each { |p| results << context.transform(p) }
|
|
27
|
+
tables.each { |t| results << context.transform(t) }
|
|
28
|
+
|
|
29
|
+
# Return single element or array
|
|
30
|
+
results.one? ? results.first : results.compact
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|