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,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Shared access to a paragraph's numbering (list) attributes.
|
|
7
|
+
#
|
|
8
|
+
# uniword persists w:numPr as NESTED properties:
|
|
9
|
+
# paragraph.properties.numbering_properties.num_id.value
|
|
10
|
+
# paragraph.properties.numbering_properties.ilvl.value
|
|
11
|
+
# while the flat integers (properties.num_id / properties.ilvl) are
|
|
12
|
+
# only populated programmatically (builder/YAML API). Readers must
|
|
13
|
+
# consult the nested shape first and fall back to the flat one —
|
|
14
|
+
# documents deserialized from DOCX only ever carry the nested shape.
|
|
15
|
+
#
|
|
16
|
+
# lutaml-model also returns an UninitializedClass sentinel for
|
|
17
|
+
# attributes that were not deserialized from source, so every read
|
|
18
|
+
# is guarded by an Integer type check instead of truthiness.
|
|
19
|
+
module NumberingAccess
|
|
20
|
+
# Numbering definition ID (w:numId) for a paragraph.
|
|
21
|
+
#
|
|
22
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
23
|
+
# @return [Integer, nil]
|
|
24
|
+
def paragraph_num_id(paragraph)
|
|
25
|
+
nested_numbering_value(paragraph, :num_id)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Numbering level (w:ilvl) for a paragraph.
|
|
29
|
+
#
|
|
30
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
31
|
+
# @return [Integer, nil]
|
|
32
|
+
def paragraph_ilvl(paragraph)
|
|
33
|
+
nested_numbering_value(paragraph, :ilvl)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def nested_numbering_value(paragraph, attribute)
|
|
39
|
+
numbering = paragraph_properties(paragraph)&.numbering_properties
|
|
40
|
+
nested = numbering&.public_send(attribute)
|
|
41
|
+
value = wrapper_value(nested)
|
|
42
|
+
return value if value.is_a?(Integer)
|
|
43
|
+
|
|
44
|
+
# Fall back to the flat builder/YAML attribute
|
|
45
|
+
flat = paragraph_properties(paragraph)&.public_send(attribute)
|
|
46
|
+
flat.is_a?(Integer) ? flat : nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def paragraph_properties(paragraph)
|
|
50
|
+
paragraph.properties if paragraph.respond_to?(:properties)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Unwrap w:numId / w:ilvl element models to their value; sentinel
|
|
54
|
+
# objects (UninitializedClass) and nils fail the Integer check.
|
|
55
|
+
def wrapper_value(node)
|
|
56
|
+
value = node.respond_to?(:value) ? node.value : node
|
|
57
|
+
value.is_a?(Integer) ? value : nil
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Resolves OOXML numbering definitions to list style information.
|
|
7
|
+
#
|
|
8
|
+
# OOXML stores list formatting in numbering definitions (abstractNum).
|
|
9
|
+
# Each numPr reference (numId + ilvl) points to a specific numbering
|
|
10
|
+
# format (bullet, decimal, lowerLetter, etc.).
|
|
11
|
+
#
|
|
12
|
+
# The resolver walks Uniword's NumberingConfiguration to build a map
|
|
13
|
+
# of numId → { ordered, marker_type, format }.
|
|
14
|
+
class NumberingResolver
|
|
15
|
+
ORDERED_FORMATS = %w[decimal lowerLetter upperLetter lowerRoman upperRoman
|
|
16
|
+
russianLower russianUpper hebrew1 hebrew2 thaiLetters
|
|
17
|
+
japaneseDigitalTenji japaneseKorean chineseCounting].freeze
|
|
18
|
+
|
|
19
|
+
# @param numbering_configuration [Object, nil] Uniword numbering config
|
|
20
|
+
def initialize(numbering_configuration)
|
|
21
|
+
@config = numbering_configuration
|
|
22
|
+
@num_map = build_num_map(numbering_configuration)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Determine if a numId represents an ordered list
|
|
26
|
+
#
|
|
27
|
+
# @param num_id [Integer, String, nil] numbering definition ID
|
|
28
|
+
# @return [Boolean] true if ordered, false if unordered
|
|
29
|
+
def ordered?(num_id)
|
|
30
|
+
return false unless num_id
|
|
31
|
+
|
|
32
|
+
info = @num_map[num_id.to_i]
|
|
33
|
+
return false unless info
|
|
34
|
+
|
|
35
|
+
info[:ordered]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Get the list style for a numId
|
|
39
|
+
#
|
|
40
|
+
# @param num_id [Integer, String, nil]
|
|
41
|
+
# @return [Symbol] :ordered, :unordered
|
|
42
|
+
def list_style(num_id)
|
|
43
|
+
ordered?(num_id) ? :ordered : :unordered
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Get marker type string for a numId
|
|
47
|
+
#
|
|
48
|
+
# @param num_id [Integer, String, nil]
|
|
49
|
+
# @return [String] "numbered", "asterisk", "lower_alpha", etc.
|
|
50
|
+
def marker_type(num_id)
|
|
51
|
+
return 'asterisk' unless num_id
|
|
52
|
+
|
|
53
|
+
info = @num_map[num_id.to_i]
|
|
54
|
+
return 'asterisk' unless info
|
|
55
|
+
|
|
56
|
+
info[:marker_type]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def build_num_map(config)
|
|
62
|
+
return {} unless config
|
|
63
|
+
|
|
64
|
+
instances = config.instances
|
|
65
|
+
return {} if instances.nil? || instances.empty?
|
|
66
|
+
|
|
67
|
+
map = {}
|
|
68
|
+
instances.each do |inst|
|
|
69
|
+
num_id = inst.num_id
|
|
70
|
+
abstract_num_id = extract_abstract_num_id(inst)
|
|
71
|
+
next unless num_id && abstract_num_id
|
|
72
|
+
|
|
73
|
+
definition = find_definition(config, abstract_num_id)
|
|
74
|
+
next unless definition
|
|
75
|
+
|
|
76
|
+
levels = definition.levels
|
|
77
|
+
level = levels&.first
|
|
78
|
+
next unless level
|
|
79
|
+
|
|
80
|
+
fmt = extract_num_fmt(level)
|
|
81
|
+
ordered = ORDERED_FORMATS.include?(fmt)
|
|
82
|
+
map[num_id] = {
|
|
83
|
+
ordered: ordered,
|
|
84
|
+
marker_type: marker_type_for(fmt),
|
|
85
|
+
format: fmt
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
map
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def extract_abstract_num_id(instance)
|
|
93
|
+
aid = instance.abstract_num_id
|
|
94
|
+
return nil unless aid
|
|
95
|
+
|
|
96
|
+
aid.is_a?(Uniword::Wordprocessingml::AbstractNumId) ? aid.val : aid
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def find_definition(config, abstract_num_id)
|
|
100
|
+
defs = config.definitions
|
|
101
|
+
return [] unless defs
|
|
102
|
+
|
|
103
|
+
defs.find { |d| d.abstract_num_id == abstract_num_id }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def extract_num_fmt(level)
|
|
107
|
+
nf = level.numFmt
|
|
108
|
+
return 'bullet' unless nf
|
|
109
|
+
|
|
110
|
+
nf.is_a?(Uniword::Wordprocessingml::NumFmt) ? nf.val.to_s : nf.to_s
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def marker_type_for(fmt)
|
|
114
|
+
case fmt
|
|
115
|
+
when 'decimal' then 'ordered'
|
|
116
|
+
when 'lowerLetter' then 'lower_alpha'
|
|
117
|
+
when 'upperLetter' then 'upper_alpha'
|
|
118
|
+
when 'lowerRoman' then 'lower_roman'
|
|
119
|
+
when 'upperRoman' then 'upper_roman'
|
|
120
|
+
when 'bullet' then 'unordered'
|
|
121
|
+
else 'ordered'
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Utility for iterating paragraph content in document order.
|
|
7
|
+
#
|
|
8
|
+
# OOXML paragraphs have separate arrays for runs, hyperlinks, SDTs, etc.
|
|
9
|
+
# but `element_order` (from lutaml-model mixed_content) preserves the
|
|
10
|
+
# interleaved sequence. This module provides a single method to walk
|
|
11
|
+
# paragraph content in the correct order.
|
|
12
|
+
#
|
|
13
|
+
# Used by ParagraphRule, ListItemRule, and HeadingRule.
|
|
14
|
+
module OrderedContent
|
|
15
|
+
# Iterate paragraph inline content in document order.
|
|
16
|
+
#
|
|
17
|
+
# @param paragraph [Uniword::Wordprocessingml::Paragraph]
|
|
18
|
+
# @param context [Context] transform context with registry
|
|
19
|
+
# @return [Array] transformed content (Strings, InlineElements, etc.)
|
|
20
|
+
def transform_paragraph_content(paragraph, context)
|
|
21
|
+
order = paragraph.is_a?(Uniword::Wordprocessingml::Paragraph) ? paragraph.element_order : nil
|
|
22
|
+
|
|
23
|
+
if order && !order.empty?
|
|
24
|
+
wrap_text_children(transform_ordered(paragraph, order, context))
|
|
25
|
+
else
|
|
26
|
+
wrap_text_children(transform_sequential(paragraph, context))
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Wrap raw strings in TextContent so all children are Base instances.
|
|
31
|
+
#
|
|
32
|
+
# @param items [Array] mixed Strings and CoreModel::Base
|
|
33
|
+
# @return [Array<CoreModel::Base>]
|
|
34
|
+
def wrap_text_children(items)
|
|
35
|
+
items.map do |item|
|
|
36
|
+
next nil if item.nil?
|
|
37
|
+
|
|
38
|
+
item.is_a?(String) ? CoreModel::TextContent.new(text: item) : item
|
|
39
|
+
end.compact
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Flatten children array to plain text string.
|
|
43
|
+
#
|
|
44
|
+
# @param children [Array] mixed content (Strings, InlineElements, Blocks)
|
|
45
|
+
# @return [String]
|
|
46
|
+
def extract_plain_text(children)
|
|
47
|
+
children.map do |c|
|
|
48
|
+
case c
|
|
49
|
+
when CoreModel::TextContent then c.text
|
|
50
|
+
when CoreModel::InlineElement then c.content.to_s
|
|
51
|
+
when CoreModel::Block then c.content.to_s
|
|
52
|
+
else c.to_s
|
|
53
|
+
end
|
|
54
|
+
end.join
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def transform_ordered(paragraph, order, context)
|
|
60
|
+
counters = Hash.new(0)
|
|
61
|
+
|
|
62
|
+
result = []
|
|
63
|
+
order.each do |entry|
|
|
64
|
+
name = entry.is_a?(String) ? entry : entry.name
|
|
65
|
+
idx = counters[name]
|
|
66
|
+
counters[name] = idx + 1
|
|
67
|
+
|
|
68
|
+
item = case name
|
|
69
|
+
when 'r'
|
|
70
|
+
run = paragraph.runs[idx]
|
|
71
|
+
context.transform(run) if run
|
|
72
|
+
when 'hyperlink'
|
|
73
|
+
hl = paragraph.hyperlinks[idx]
|
|
74
|
+
context.transform(hl) if hl
|
|
75
|
+
when 'sdt'
|
|
76
|
+
sdt = paragraph.structured_document_tags&.[](idx)
|
|
77
|
+
context.transform(sdt) if sdt
|
|
78
|
+
when 'oMathPara'
|
|
79
|
+
math = paragraph.o_math_paras&.[](idx)
|
|
80
|
+
context.transform(math) if math
|
|
81
|
+
when 'oMath'
|
|
82
|
+
math = paragraph.o_maths&.[](idx)
|
|
83
|
+
context.transform(math) if math
|
|
84
|
+
when 'fldSimple'
|
|
85
|
+
field = paragraph.simple_fields&.[](idx)
|
|
86
|
+
context.transform(field) if field
|
|
87
|
+
when 'proofErr'
|
|
88
|
+
# Proofing errors have no semantic value — skip silently
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
result << item if item
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
result.compact
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def transform_sequential(paragraph, context)
|
|
99
|
+
content = []
|
|
100
|
+
(paragraph.runs || []).each { |r| content << context.transform(r) }
|
|
101
|
+
(paragraph.hyperlinks || []).each { |h| content << context.transform(h) }
|
|
102
|
+
content.compact
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Base class for OOXML → CoreModel transform rules.
|
|
7
|
+
#
|
|
8
|
+
# Each rule handles one OOXML element type and produces a CoreModel
|
|
9
|
+
# node. Rules are registered in RuleRegistry and dispatched by the
|
|
10
|
+
# ToCoreModel orchestrator.
|
|
11
|
+
#
|
|
12
|
+
# Subclasses must implement:
|
|
13
|
+
# - matches?(element) → true if this rule handles the element
|
|
14
|
+
# - apply(element, context) → CoreModel node or Array of nodes
|
|
15
|
+
#
|
|
16
|
+
# @example Implementing a custom rule
|
|
17
|
+
# class MyRule < Rule
|
|
18
|
+
# def matches?(element)
|
|
19
|
+
# element.is_a?(Uniword::Wordprocessingml::MyElement)
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# def apply(element, context)
|
|
23
|
+
# Coridoc::CoreModel::Block.new(
|
|
24
|
+
# element_type: 'paragraph',
|
|
25
|
+
# content: element.text
|
|
26
|
+
# )
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
class Rule
|
|
30
|
+
# Check if this rule handles the given element
|
|
31
|
+
#
|
|
32
|
+
# @param element [Object] OOXML element to check
|
|
33
|
+
# @return [Boolean]
|
|
34
|
+
def matches?(element)
|
|
35
|
+
raise NotImplementedError, "#{self.class}#matches? not implemented"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Transform an OOXML element to a CoreModel node
|
|
39
|
+
#
|
|
40
|
+
# @param element [Object] OOXML element to transform
|
|
41
|
+
# @param context [Context] shared transform context
|
|
42
|
+
# @return [Coridoc::CoreModel::Base, Array, String, nil]
|
|
43
|
+
def apply(element, context)
|
|
44
|
+
raise NotImplementedError, "#{self.class}#apply not implemented"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Rule priority — higher priority rules are checked first.
|
|
48
|
+
# Override in subclasses when needed.
|
|
49
|
+
#
|
|
50
|
+
# @return [Integer]
|
|
51
|
+
def priority
|
|
52
|
+
0
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
# Registry for transform rules.
|
|
7
|
+
#
|
|
8
|
+
# Manages registration and lookup of rules for OOXML element types.
|
|
9
|
+
# Rules are checked in priority order (highest first).
|
|
10
|
+
# Falls back to NullRule which raises ArgumentError.
|
|
11
|
+
#
|
|
12
|
+
# Follows Open/Closed Principle: new rules are added by registering,
|
|
13
|
+
# not by modifying the registry class.
|
|
14
|
+
class RuleRegistry
|
|
15
|
+
def initialize
|
|
16
|
+
@rules = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Register a rule instance
|
|
20
|
+
#
|
|
21
|
+
# @param rule [Rule] the rule to register
|
|
22
|
+
# @return [self]
|
|
23
|
+
def register(rule)
|
|
24
|
+
unless rule.is_a?(Rule)
|
|
25
|
+
raise ArgumentError,
|
|
26
|
+
"Expected Rule, got #{rule.class}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
@rules << rule
|
|
30
|
+
@rules.sort_by! { |r| -r.priority }
|
|
31
|
+
self
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Find the first rule that matches the element
|
|
35
|
+
#
|
|
36
|
+
# @param element [Object] OOXML element to find a rule for
|
|
37
|
+
# @return [Rule] matching rule
|
|
38
|
+
# @raise [ArgumentError] if no rule matches
|
|
39
|
+
def find_rule(element)
|
|
40
|
+
@rules.find { |r| r.matches?(element) } ||
|
|
41
|
+
raise(ArgumentError, "No transform rule registered for #{element.class}")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Check if any rule matches the element
|
|
45
|
+
#
|
|
46
|
+
# @param element [Object] element to check
|
|
47
|
+
# @return [Boolean]
|
|
48
|
+
def matches?(element)
|
|
49
|
+
@rules.any? { |r| r.matches?(element) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Number of registered rules
|
|
53
|
+
# @return [Integer]
|
|
54
|
+
def size
|
|
55
|
+
@rules.size
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:bookmarkStart to a metadata hash for attaching to
|
|
8
|
+
# the next CoreModel element.
|
|
9
|
+
#
|
|
10
|
+
# Bookmarks in OOXML are position markers (not content containers).
|
|
11
|
+
# The orchestrator collects bookmark IDs and attaches them as
|
|
12
|
+
# element attributes on the containing paragraph/section.
|
|
13
|
+
class BookmarkRule < Rule
|
|
14
|
+
def matches?(element)
|
|
15
|
+
return false unless defined?(Uniword::Wordprocessingml)
|
|
16
|
+
|
|
17
|
+
element.is_a?(Uniword::Wordprocessingml::BookmarkStart) ||
|
|
18
|
+
element.is_a?(Uniword::Wordprocessingml::BookmarkEnd)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Returns a hash with bookmark metadata, not a CoreModel node.
|
|
22
|
+
# The orchestrator uses this to set the id on the parent element.
|
|
23
|
+
def apply(element, _context)
|
|
24
|
+
if element.is_a?(Uniword::Wordprocessingml::BookmarkStart)
|
|
25
|
+
{ id: element.id&.to_s, name: element.name&.to_s }
|
|
26
|
+
else
|
|
27
|
+
nil # BookmarkEnd — no useful data
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:br (Break) elements.
|
|
8
|
+
#
|
|
9
|
+
# Page breaks become CoreModel::Block (page_break).
|
|
10
|
+
# Line breaks become CoreModel::InlineElement (hard_line_break).
|
|
11
|
+
class BreakRule < Rule
|
|
12
|
+
def matches?(element)
|
|
13
|
+
defined?(Uniword::Wordprocessingml::Break) &&
|
|
14
|
+
element.is_a?(Uniword::Wordprocessingml::Break)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def apply(brk, _context)
|
|
18
|
+
if brk.type == 'page'
|
|
19
|
+
Coridoc::CoreModel::Block.new(block_semantic_type: 'page_break')
|
|
20
|
+
else
|
|
21
|
+
Coridoc::CoreModel::InlineElement.new(
|
|
22
|
+
format_type: 'hard_line_break'
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:footnoteReference to CoreModel::FootnoteReference.
|
|
8
|
+
#
|
|
9
|
+
# Footnote content is looked up from the context's footnotes map,
|
|
10
|
+
# which is populated by the ToCoreModel orchestrator before
|
|
11
|
+
# transforming body elements.
|
|
12
|
+
class FootnoteRule < Rule
|
|
13
|
+
def matches?(element)
|
|
14
|
+
defined?(Uniword::Wordprocessingml::FootnoteReference) &&
|
|
15
|
+
element.is_a?(Uniword::Wordprocessingml::FootnoteReference)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def apply(ref, _context)
|
|
19
|
+
id = ref.id&.to_s
|
|
20
|
+
|
|
21
|
+
CoreModel::FootnoteReference.new(id: id)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms heading paragraphs to CoreModel::StructuralElement.
|
|
8
|
+
#
|
|
9
|
+
# Heading detection uses StyleResolver which checks pStyle values
|
|
10
|
+
# (like "Heading1", "heading 2") and outline levels.
|
|
11
|
+
#
|
|
12
|
+
# This rule is NOT registered in the RuleRegistry — instead, the
|
|
13
|
+
# ToCoreModel orchestrator dispatches to it directly after checking
|
|
14
|
+
# the style resolver. This avoids the problem of matches() needing
|
|
15
|
+
# context to determine if a paragraph is a heading.
|
|
16
|
+
class HeadingRule < Rule
|
|
17
|
+
include OrderedContent
|
|
18
|
+
|
|
19
|
+
def matches?(_element)
|
|
20
|
+
false # Never auto-matched; orchestrator dispatches directly
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def apply(paragraph, context)
|
|
24
|
+
level = context.style_resolver.heading_level(paragraph) || 1
|
|
25
|
+
title = extract_title(paragraph, context)
|
|
26
|
+
id = extract_bookmark_id(paragraph)
|
|
27
|
+
|
|
28
|
+
CoreModel::SectionElement.new(
|
|
29
|
+
level: level,
|
|
30
|
+
title: title,
|
|
31
|
+
id: id
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def extract_title(paragraph, context)
|
|
38
|
+
children = transform_paragraph_content(paragraph, context)
|
|
39
|
+
extract_plain_text(children)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def extract_bookmark_id(paragraph)
|
|
43
|
+
starts = paragraph.bookmark_starts
|
|
44
|
+
return nil if starts.nil? || starts.empty?
|
|
45
|
+
|
|
46
|
+
starts.first.id&.to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Docx
|
|
5
|
+
module Transform
|
|
6
|
+
module Rules
|
|
7
|
+
# Transforms w:hyperlink elements to CoreModel::InlineElement (link).
|
|
8
|
+
#
|
|
9
|
+
# External hyperlinks have r:id (URL). Internal links have w:anchor
|
|
10
|
+
# (bookmark reference). Both are captured in the target attribute.
|
|
11
|
+
class HyperlinkRule < Rule
|
|
12
|
+
def matches?(element)
|
|
13
|
+
defined?(Uniword::Wordprocessingml::Hyperlink) &&
|
|
14
|
+
element.is_a?(Uniword::Wordprocessingml::Hyperlink)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def apply(hyperlink, context)
|
|
18
|
+
content = extract_content(hyperlink, context)
|
|
19
|
+
text = flatten_to_string(content)
|
|
20
|
+
|
|
21
|
+
Coridoc::CoreModel::InlineElement.new(
|
|
22
|
+
format_type: 'link',
|
|
23
|
+
target: resolve_target(hyperlink),
|
|
24
|
+
content: text
|
|
25
|
+
)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def resolve_target(hyperlink)
|
|
31
|
+
# External link (URL stored in r:id)
|
|
32
|
+
return hyperlink.id if hyperlink.id && !hyperlink.id.empty?
|
|
33
|
+
|
|
34
|
+
# Internal link (bookmark anchor)
|
|
35
|
+
"##{hyperlink.anchor}" if hyperlink.anchor
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def extract_content(hyperlink, context)
|
|
39
|
+
return [] if hyperlink.runs.nil?
|
|
40
|
+
|
|
41
|
+
hyperlink.runs.map { |r| context.transform(r) }.compact
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def flatten_to_string(content)
|
|
45
|
+
case content
|
|
46
|
+
when Array
|
|
47
|
+
content.map { |c| c.is_a?(String) ? c : c.to_s }.join
|
|
48
|
+
when String
|
|
49
|
+
content
|
|
50
|
+
else
|
|
51
|
+
content.to_s
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|