coridoc-mirror 0.1.11

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 (91) hide show
  1. checksums.yaml +7 -0
  2. data/lib/coridoc/mirror/core_model_to_mirror.rb +221 -0
  3. data/lib/coridoc/mirror/frontmatter_query.rb +50 -0
  4. data/lib/coridoc/mirror/frontmatter_tree_to_hash.rb +40 -0
  5. data/lib/coridoc/mirror/handler_registry.rb +94 -0
  6. data/lib/coridoc/mirror/handlers/abstract.rb +19 -0
  7. data/lib/coridoc/mirror/handlers/admonition.rb +29 -0
  8. data/lib/coridoc/mirror/handlers/bibliography.rb +43 -0
  9. data/lib/coridoc/mirror/handlers/blockquote.rb +19 -0
  10. data/lib/coridoc/mirror/handlers/code_block.rb +62 -0
  11. data/lib/coridoc/mirror/handlers/comment.rb +14 -0
  12. data/lib/coridoc/mirror/handlers/definition_list.rb +133 -0
  13. data/lib/coridoc/mirror/handlers/example.rb +19 -0
  14. data/lib/coridoc/mirror/handlers/footnote.rb +18 -0
  15. data/lib/coridoc/mirror/handlers/frontmatter.rb +71 -0
  16. data/lib/coridoc/mirror/handlers/generic_block.rb +24 -0
  17. data/lib/coridoc/mirror/handlers/horizontal_rule.rb +14 -0
  18. data/lib/coridoc/mirror/handlers/image.rb +64 -0
  19. data/lib/coridoc/mirror/handlers/include.rb +50 -0
  20. data/lib/coridoc/mirror/handlers/inline.rb +282 -0
  21. data/lib/coridoc/mirror/handlers/list.rb +80 -0
  22. data/lib/coridoc/mirror/handlers/open_block.rb +16 -0
  23. data/lib/coridoc/mirror/handlers/paragraph.rb +16 -0
  24. data/lib/coridoc/mirror/handlers/partintro.rb +19 -0
  25. data/lib/coridoc/mirror/handlers/reviewer.rb +14 -0
  26. data/lib/coridoc/mirror/handlers/sidebar.rb +19 -0
  27. data/lib/coridoc/mirror/handlers/structural.rb +84 -0
  28. data/lib/coridoc/mirror/handlers/table.rb +82 -0
  29. data/lib/coridoc/mirror/handlers/toc.rb +48 -0
  30. data/lib/coridoc/mirror/handlers/verse.rb +22 -0
  31. data/lib/coridoc/mirror/handlers.rb +41 -0
  32. data/lib/coridoc/mirror/mark.rb +181 -0
  33. data/lib/coridoc/mirror/mark_reverse_builder.rb +142 -0
  34. data/lib/coridoc/mirror/mirror_json_format.rb +48 -0
  35. data/lib/coridoc/mirror/mirror_to_core_model.rb +93 -0
  36. data/lib/coridoc/mirror/mirror_yaml_format.rb +47 -0
  37. data/lib/coridoc/mirror/node.rb +1005 -0
  38. data/lib/coridoc/mirror/partitioner.rb +75 -0
  39. data/lib/coridoc/mirror/reverse_builder/abstract.rb +17 -0
  40. data/lib/coridoc/mirror/reverse_builder/admonition.rb +16 -0
  41. data/lib/coridoc/mirror/reverse_builder/base.rb +42 -0
  42. data/lib/coridoc/mirror/reverse_builder/biblio_entry.rb +18 -0
  43. data/lib/coridoc/mirror/reverse_builder/bibliography.rb +14 -0
  44. data/lib/coridoc/mirror/reverse_builder/blockquote.rb +16 -0
  45. data/lib/coridoc/mirror/reverse_builder/bullet_list.rb +14 -0
  46. data/lib/coridoc/mirror/reverse_builder/caption.rb +15 -0
  47. data/lib/coridoc/mirror/reverse_builder/code_block.rb +18 -0
  48. data/lib/coridoc/mirror/reverse_builder/definition_list.rb +31 -0
  49. data/lib/coridoc/mirror/reverse_builder/document.rb +18 -0
  50. data/lib/coridoc/mirror/reverse_builder/example.rb +16 -0
  51. data/lib/coridoc/mirror/reverse_builder/figure.rb +31 -0
  52. data/lib/coridoc/mirror/reverse_builder/footnote_entry.rb +14 -0
  53. data/lib/coridoc/mirror/reverse_builder/footnote_marker.rb +20 -0
  54. data/lib/coridoc/mirror/reverse_builder/footnotes.rb +16 -0
  55. data/lib/coridoc/mirror/reverse_builder/frontmatter.rb +19 -0
  56. data/lib/coridoc/mirror/reverse_builder/generic_block.rb +22 -0
  57. data/lib/coridoc/mirror/reverse_builder/hard_break.rb +13 -0
  58. data/lib/coridoc/mirror/reverse_builder/header.rb +18 -0
  59. data/lib/coridoc/mirror/reverse_builder/horizontal_rule.rb +13 -0
  60. data/lib/coridoc/mirror/reverse_builder/image.rb +21 -0
  61. data/lib/coridoc/mirror/reverse_builder/include.rb +42 -0
  62. data/lib/coridoc/mirror/reverse_builder/inline_text.rb +15 -0
  63. data/lib/coridoc/mirror/reverse_builder/list_item.rb +33 -0
  64. data/lib/coridoc/mirror/reverse_builder/literal_block.rb +18 -0
  65. data/lib/coridoc/mirror/reverse_builder/open_block.rb +13 -0
  66. data/lib/coridoc/mirror/reverse_builder/ordered_list.rb +14 -0
  67. data/lib/coridoc/mirror/reverse_builder/paragraph.rb +13 -0
  68. data/lib/coridoc/mirror/reverse_builder/partintro.rb +17 -0
  69. data/lib/coridoc/mirror/reverse_builder/pass_block.rb +18 -0
  70. data/lib/coridoc/mirror/reverse_builder/preamble.rb +13 -0
  71. data/lib/coridoc/mirror/reverse_builder/raw_inline.rb +13 -0
  72. data/lib/coridoc/mirror/reverse_builder/section.rb +22 -0
  73. data/lib/coridoc/mirror/reverse_builder/sections.rb +15 -0
  74. data/lib/coridoc/mirror/reverse_builder/sidebar.rb +16 -0
  75. data/lib/coridoc/mirror/reverse_builder/soft_break.rb +13 -0
  76. data/lib/coridoc/mirror/reverse_builder/stem_block.rb +18 -0
  77. data/lib/coridoc/mirror/reverse_builder/table.rb +23 -0
  78. data/lib/coridoc/mirror/reverse_builder/table_body.rb +13 -0
  79. data/lib/coridoc/mirror/reverse_builder/table_cell.rb +20 -0
  80. data/lib/coridoc/mirror/reverse_builder/table_head.rb +13 -0
  81. data/lib/coridoc/mirror/reverse_builder/table_row.rb +14 -0
  82. data/lib/coridoc/mirror/reverse_builder/text.rb +20 -0
  83. data/lib/coridoc/mirror/reverse_builder/toc.rb +13 -0
  84. data/lib/coridoc/mirror/reverse_builder/toc_entry.rb +14 -0
  85. data/lib/coridoc/mirror/reverse_builder/verse.rb +16 -0
  86. data/lib/coridoc/mirror/reverse_builder.rb +147 -0
  87. data/lib/coridoc/mirror/transformer.rb +41 -0
  88. data/lib/coridoc/mirror/version.rb +7 -0
  89. data/lib/coridoc/mirror.rb +177 -0
  90. data/lib/coridoc-mirror.rb +12 -0
  91. metadata +191 -0
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ module DefinitionList
7
+ def self.call(element, context:)
8
+ entries = Array(element.items).flat_map do |item|
9
+ definition_entry(item, context)
10
+ end
11
+ return nil if entries.empty?
12
+
13
+ Node::DefinitionList.new(
14
+ attrs: Node::DefinitionList::Attrs.new(id: element.id),
15
+ content: entries
16
+ )
17
+ end
18
+
19
+ class << self
20
+ private
21
+
22
+ # One `<dt>` per term + one `<dd>` per item. Multi-term `<dt>`'s
23
+ # (AsciiDoc `term1::\nterm2::\ndef`) emit two `<dt>` elements
24
+ # sharing one `<dd>`. Single-term items — the common case —
25
+ # emit one `<dt>` + one `<dd>`.
26
+ def definition_entry(item, context)
27
+ term_nodes = build_terms(item, context)
28
+ desc_node = build_description(item, context)
29
+ return nil if term_nodes.empty? && desc_node.nil?
30
+
31
+ term_nodes + [desc_node].compact
32
+ end
33
+
34
+ # Emit one DefinitionTerm node per term in `item.terms`.
35
+ # Falls back to `[term]` for items populated via paths that
36
+ # only set the singular `term` accessor (backward compat).
37
+ def build_terms(item, context)
38
+ term_list = terms_for(item)
39
+ return [] if term_list.empty?
40
+
41
+ term_list.map { |term| build_term_node(term, item, context) }
42
+ end
43
+
44
+ # Source of truth for "the terms on this `<dt>`": the `terms`
45
+ # collection when populated, else `[term]` for legacy callers.
46
+ def terms_for(item)
47
+ collection = item.terms if item.is_a?(CoreModel::DefinitionItem)
48
+ return Array(collection) unless collection.nil? || collection.empty?
49
+
50
+ primary = item.term
51
+ primary.to_s.empty? ? [] : [primary.to_s]
52
+ end
53
+
54
+ # Build a single DefinitionTerm node for one term string,
55
+ # carrying inline children when the source had them. Children
56
+ # map 1:1 to the FIRST term only (multi-term `<dt>`'s with
57
+ # inline markup on later terms is rare; the parser doesn't
58
+ # capture per-term children today).
59
+ def build_term_node(term_text, item, context)
60
+ term_children = (item.term_children if (term_text == terms_for(item).first) && item.is_a?(CoreModel::DefinitionItem))
61
+ if term_children && !term_children.empty?
62
+ inline_nodes = term_children.flat_map do |child|
63
+ Handlers::Inline.process_child(child, context)
64
+ end
65
+ return Node::DefinitionTerm.new(content: inline_nodes) unless inline_nodes.empty?
66
+ end
67
+
68
+ Node::DefinitionTerm.new(content: [context.text_node(term_text.to_s)])
69
+ end
70
+
71
+ def build_description(item, context)
72
+ desc_nodes = description_nodes(item, context)
73
+ attached_nodes = attached_block_nodes(item, context)
74
+ nested_nodes = nested_dl_nodes(item, context)
75
+ all_nodes = desc_nodes.concat(attached_nodes).concat(nested_nodes)
76
+ return nil if all_nodes.empty?
77
+
78
+ Node::DefinitionDescription.new(content: all_nodes)
79
+ end
80
+
81
+ # `+`-continuation blocks attached to this dd in AsciiDoc source
82
+ # become children of the same DefinitionDescription node so they
83
+ # render inside the same <dd> element. Dispatch each through
84
+ # `context.transform_element` — same registry path as a top-level
85
+ # walk, so every registered block handler (paragraph, admonition,
86
+ # source, etc.) applies.
87
+ def attached_block_nodes(item, context)
88
+ return [] unless item.is_a?(CoreModel::DefinitionItem)
89
+ return [] if item.attached_children.empty?
90
+
91
+ item.attached_children.flat_map do |block|
92
+ context.transform_element(block)
93
+ end
94
+ end
95
+
96
+ # Multi-level dlist: AsciiDoc `::`/`:::`/`::::` delimiters
97
+ # express nesting depth. The parser builds the tree
98
+ # (CoreModel::DefinitionItem#nested); the mirror handler must
99
+ # recurse so the nested <dl> renders INSIDE the parent <dd>.
100
+ # Single source of truth: this method recurses through
101
+ # `DefinitionList.call`, so nested items use the same dispatch
102
+ # path as top-level items (DRY).
103
+ def nested_dl_nodes(item, context)
104
+ return [] unless item.is_a?(CoreModel::DefinitionItem)
105
+
106
+ nested = item.nested
107
+ return [] unless nested && nested.items.any?
108
+
109
+ [DefinitionList.call(nested, context: context)].compact
110
+ end
111
+
112
+ # Emit one text node per source — never both. Rich
113
+ # definition_children (inline nodes) win over plain definitions
114
+ # (strings) because they preserve formatting; falling back to
115
+ # the plain string keeps this handler robust for DefinitionItem
116
+ # instances populated by paths that don't build inline children.
117
+ def description_nodes(item, context)
118
+ unless item.is_a?(CoreModel::DefinitionItem)
119
+ return Array(item.definitions).map do |defn|
120
+ context.text_node(defn.to_s)
121
+ end
122
+ end
123
+
124
+ children = item.definition_children
125
+ return Array(item.definitions).map { |defn| context.text_node(defn.to_s) } if children.empty?
126
+
127
+ children.flat_map { |child| Handlers::Inline.process_child(child, context) }
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ module Example
7
+ def self.call(element, context:)
8
+ content = context.extract_content(element)
9
+ return nil if content.empty?
10
+
11
+ Node::Example.new(
12
+ attrs: Node::Example::Attrs.new(id: element.id, title: element.title),
13
+ content: content
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ # Handles Footnote and FootnoteReference.
7
+ module Footnote
8
+ def self.call(element, context:)
9
+ context.register_footnote(element)
10
+ end
11
+
12
+ def self.reference(element, context:)
13
+ context.resolve_footnote_reference(element)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ # Handles FrontmatterBlock → frontmatter node.
7
+ #
8
+ # Walks the CoreModel +data+ hash and builds a typed tree of
9
+ # FrontmatterEntry / FrontmatterValue nodes. Dates/times are ISO
10
+ # 8601-encoded so the wire shape is JSON-native.
11
+ module Frontmatter
12
+ def self.call(element, *)
13
+ entries = build_entries(Coridoc::CoreModel::FrontmatterBlock::Codec.to_hash(element))
14
+
15
+ Node::Frontmatter.new(
16
+ attrs: Node::Frontmatter::Attrs.new(
17
+ schema: element.schema,
18
+ entries: entries
19
+ )
20
+ )
21
+ end
22
+
23
+ class << self
24
+ private
25
+
26
+ def build_entries(data)
27
+ data.map { |key, value| build_entry(key.to_s, value) }
28
+ end
29
+
30
+ def build_entry(key, value)
31
+ Node::FrontmatterEntry.new(
32
+ key: key,
33
+ value: build_value(value)
34
+ )
35
+ end
36
+
37
+ def build_value(value)
38
+ case value
39
+ when Hash
40
+ Node::FrontmatterValue.new(
41
+ value_type: 'map',
42
+ entries: build_entries(value)
43
+ )
44
+ when Array
45
+ Node::FrontmatterValue.new(
46
+ value_type: 'array',
47
+ items: value.map { |v| build_value(v) }
48
+ )
49
+ when Integer
50
+ Node::FrontmatterValue.new(value_type: 'integer', integer_value: value)
51
+ when Float
52
+ Node::FrontmatterValue.new(value_type: 'float', float_value: value)
53
+ when TrueClass, FalseClass
54
+ Node::FrontmatterValue.new(value_type: 'boolean', boolean_value: value)
55
+ when Date, DateTime
56
+ Node::FrontmatterValue.new(value_type: 'date', date_value: value)
57
+ when Time
58
+ Node::FrontmatterValue.new(value_type: 'datetime', datetime_value: value)
59
+ when Symbol
60
+ Node::FrontmatterValue.new(value_type: 'symbol', symbol_value: value.to_s)
61
+ when nil
62
+ Node::FrontmatterValue.new(value_type: 'nil')
63
+ else
64
+ Node::FrontmatterValue.new(value_type: 'string', string_value: value.to_s)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ module GenericBlock
7
+ def self.call(element, context:)
8
+ semantic_type = element.resolve_semantic_type
9
+ content = context.extract_content(element)
10
+ return nil if content.empty?
11
+
12
+ Node::GenericBlock.new(
13
+ attrs: Node::GenericBlock::Attrs.new(
14
+ id: element.id,
15
+ title: element.title,
16
+ semantic_type: semantic_type&.to_s
17
+ ),
18
+ content: content
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ # Handles HorizontalRuleBlock → horizontal_rule node.
7
+ module HorizontalRule
8
+ def self.call(_element, *)
9
+ Node::HorizontalRule.new
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ # Image handler.
7
+ #
8
+ # Two emission shapes:
9
+ # - Ruby legacy (default): bare `image` node, title/caption in attrs.
10
+ # - JS @metanorma/mirror (`partition_structural: true`): when the
11
+ # source image is a **block** image with a title, wrap it in a
12
+ # `figure` node with the image plus a `caption` child, matching
13
+ # the JS schema. Inline images never wrap, even if they carry a
14
+ # role or title, because they live inside paragraph flow.
15
+ module Image
16
+ def self.call(element, context:)
17
+ image_node = build_image_node(element)
18
+
19
+ return image_node unless context.partition_structural
20
+ return image_node if element.inline
21
+ return image_node unless caption_text?(element)
22
+
23
+ Node::Figure.new(
24
+ attrs: Node::Figure::Attrs.new(id: element.id, title: caption_value(element)),
25
+ content: [image_node, Node::Caption.new(content: caption_text_nodes(element, context))]
26
+ )
27
+ end
28
+
29
+ class << self
30
+ private
31
+
32
+ def build_image_node(element)
33
+ Node::Image.new(attrs: image_attrs(element))
34
+ end
35
+
36
+ def image_attrs(element)
37
+ Node::Image::Attrs.new(
38
+ src: element.src,
39
+ alt: element.alt,
40
+ title: element.title,
41
+ caption: element.caption,
42
+ width: element.width,
43
+ height: element.height,
44
+ role: element.role,
45
+ inline: element.inline
46
+ )
47
+ end
48
+
49
+ def caption_text?(element)
50
+ !caption_value(element).to_s.empty?
51
+ end
52
+
53
+ def caption_value(element)
54
+ element.caption || element.title
55
+ end
56
+
57
+ def caption_text_nodes(element, context)
58
+ [context.text_node(caption_value(element).to_s)]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ # Include directive handler.
7
+ #
8
+ # Emits a ProseMirror-compatible +include+ node carrying the
9
+ # target path and parsed options as typed attrs. Graph-mode
10
+ # preservation: the node survives serialization and round-trips
11
+ # back to a +CoreModel::Include+ via the reverse builder.
12
+ module Include
13
+ def self.call(element, context:)
14
+ Node::Include.new(attrs: build_attrs(element))
15
+ end
16
+
17
+ class << self
18
+ private
19
+
20
+ def build_attrs(element)
21
+ options = element.options
22
+ Node::Include::Attrs.new(
23
+ target: element.target,
24
+ tags: tags_value(options),
25
+ tags_wildcard: options&.tags_wildcard,
26
+ tags_inverted: options&.tags_inverted,
27
+ lines: options&.lines_spec,
28
+ leveloffset: leveloffset_value(options),
29
+ indent: options&.indent,
30
+ file_encoding: options&.file_encoding,
31
+ raw_options: element.raw_options
32
+ )
33
+ end
34
+
35
+ def tags_value(options)
36
+ return [] unless options
37
+
38
+ options.tags
39
+ end
40
+
41
+ def leveloffset_value(options)
42
+ return nil unless options&.leveloffset
43
+
44
+ options.leveloffset.to_s
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,282 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Mirror
5
+ module Handlers
6
+ module Inline
7
+ # Classification of inline handlers.
8
+ SIMPLE_MARK_TYPES = {
9
+ CoreModel::BoldElement => Mark::Bold,
10
+ CoreModel::ItalicElement => Mark::Italic,
11
+ CoreModel::MonospaceElement => Mark::Monospace,
12
+ CoreModel::UnderlineElement => Mark::Underline,
13
+ CoreModel::StrikethroughElement => Mark::Strikethrough,
14
+ CoreModel::SubscriptElement => Mark::Subscript,
15
+ CoreModel::SuperscriptElement => Mark::Superscript,
16
+ CoreModel::HighlightElement => Mark::Highlight,
17
+ CoreModel::TermElement => Mark::Bold
18
+ }.freeze
19
+
20
+ def self.process(element, context:)
21
+ return [] unless element
22
+
23
+ children = inline_children_for(element)
24
+
25
+ children.flat_map do |child|
26
+ process_child(child, context)
27
+ end
28
+ end
29
+
30
+ def self.process_child(child, context)
31
+ case child
32
+ when CoreModel::TextContent
33
+ return [] if child.text.nil? || child.text.empty?
34
+
35
+ [context.text_node(child.text)]
36
+ when CoreModel::InlineElement
37
+ [dispatch_inline(child, context)].compact
38
+ when CoreModel::FootnoteReference
39
+ [context.resolve_footnote_reference(child)]
40
+ when CoreModel::Block, CoreModel::StructuralElement
41
+ result = context.registry.handle(child, context: context)
42
+ return [] unless result
43
+
44
+ value, concat = result
45
+ return [] unless value
46
+
47
+ if concat
48
+ Array(value)
49
+ else
50
+ [value].compact
51
+ end
52
+ when CoreModel::Image
53
+ [Handlers::Image.call(child, context: context)]
54
+ else
55
+ []
56
+ end
57
+ end
58
+
59
+ def self.call(element, context:)
60
+ dispatch_inline(element, context)
61
+ end
62
+
63
+ def self.text_content(element, context:)
64
+ return nil if element.text.nil? || element.text.empty?
65
+
66
+ context.text_node(element.text)
67
+ end
68
+
69
+ class << self
70
+ private
71
+
72
+ def inline_children_for(element)
73
+ if element.is_a?(CoreModel::InlineElement) ||
74
+ element.is_a?(CoreModel::Block) ||
75
+ element.is_a?(CoreModel::TableCell) ||
76
+ element.is_a?(CoreModel::StructuralElement)
77
+ children = element.children
78
+ return children if children && !children.empty?
79
+ end
80
+
81
+ if element.is_a?(CoreModel::InlineElement) ||
82
+ element.is_a?(CoreModel::Block)
83
+ content = element.content
84
+ return [CoreModel::TextContent.new(text: content.to_s)] if content && !content.to_s.empty?
85
+ end
86
+
87
+ []
88
+ end
89
+
90
+ def dispatch_inline(element, context)
91
+ mark_class = SIMPLE_MARK_TYPES[element.class]
92
+ return build_simple_mark(element, context, mark_class) if mark_class
93
+
94
+ case element
95
+ when CoreModel::LinkElement
96
+ build_link_mark(element, context)
97
+ when CoreModel::CrossReferenceElement
98
+ build_xref_mark(element, context)
99
+ when CoreModel::StemElement
100
+ build_stem_mark(element, context)
101
+ when CoreModel::SpanElement
102
+ build_span_mark(element, context)
103
+ when CoreModel::FootnoteElement
104
+ build_footnote_node(element, context)
105
+ when CoreModel::HardLineBreakElement
106
+ Node::HardBreak.new
107
+ when CoreModel::LineBreakElement
108
+ Node::SoftBreak.new
109
+ when CoreModel::TextElement
110
+ build_text_only(element, context)
111
+ when CoreModel::RawInlineElement
112
+ build_raw_inline(element, context)
113
+ when CoreModel::InlineElement
114
+ handle_generic_inline(element, context)
115
+ end
116
+ end
117
+
118
+ def handle_generic_inline(element, context)
119
+ text = element.content.to_s
120
+ return nil if text.empty?
121
+
122
+ context.text_node(text)
123
+ end
124
+
125
+ def build_simple_mark(element, context, mark_class)
126
+ # InlineElement always has children after the dual-shape fix
127
+ # (Bug 16A follow-up). Walk the children and apply this mark
128
+ # to each text leaf. Inner mark elements get the outer mark
129
+ # added to their marks list, producing ProseMirror's flat
130
+ # text-node-with-marks shape.
131
+ #
132
+ # Fallback: programmatically constructed elements (e.g. in
133
+ # specs or other format gems) may not have children. Fall
134
+ # back to the content string in that case.
135
+ children = element.children
136
+ if children.nil? || children.empty?
137
+ text = extract_inline_text(element)
138
+ return nil if text.empty?
139
+
140
+ return context.text_node(text, marks: [mark_class.new])
141
+ end
142
+
143
+ flatten_marked_children(children, [mark_class.new], context)
144
+ end
145
+
146
+ # Walk a list of children, attaching the active set of marks
147
+ # to each text leaf. When a child is itself an InlineElement
148
+ # with its own mark (e.g. CodeElement inside BoldElement),
149
+ # the inner mark is added to the active set and recursion
150
+ # proceeds into its children. Produces the conjunction of
151
+ # marks for each text run that ProseMirror expects:
152
+ # **a `b` c** → text("a ", [bold]), text("b", [bold, code]), text(" c", [bold])
153
+ def flatten_marked_children(children, active_marks, context)
154
+ result = []
155
+ children.each do |child|
156
+ case child
157
+ when CoreModel::TextContent
158
+ next if child.text.nil? || child.text.empty?
159
+
160
+ result << context.text_node(child.text, marks: active_marks.dup)
161
+ when CoreModel::InlineElement
162
+ inner_mark_class = SIMPLE_MARK_TYPES[child.class]
163
+ if inner_mark_class && child.children&.any?
164
+ combined = active_marks + [inner_mark_class.new]
165
+ result.concat(flatten_marked_children(child.children, combined, context))
166
+ elsif inner_mark_class
167
+ text = extract_inline_text(child)
168
+ next if text.empty?
169
+
170
+ combined = active_marks + [inner_mark_class.new]
171
+ result << context.text_node(text, marks: combined)
172
+ else
173
+ nested = dispatch_inline(child, context)
174
+ result << nested if nested
175
+ end
176
+ end
177
+ end
178
+ result
179
+ end
180
+
181
+ def build_link_mark(element, context)
182
+ text = extract_inline_text(element)
183
+ text = element.target.to_s if text.empty? && element.target
184
+ return nil if text.empty?
185
+
186
+ context.text_node(text, marks: [
187
+ Mark::Link.new(attrs: Mark::Link::Attrs.new(href: element.target))
188
+ ])
189
+ end
190
+
191
+ def build_xref_mark(element, context)
192
+ text = extract_inline_text(element)
193
+ target = element.target
194
+
195
+ display_text = text.empty? ? (target || '') : text
196
+ return nil if display_text.empty?
197
+
198
+ context.text_node(display_text, marks: [Mark::CrossReference.new(
199
+ attrs: Mark::CrossReference::Attrs.new(
200
+ target: target,
201
+ resolved: text.empty? ? nil : text
202
+ )
203
+ )])
204
+ end
205
+
206
+ def build_stem_mark(element, context)
207
+ text = extract_inline_text(element)
208
+ return nil if text.empty?
209
+
210
+ context.text_node(text, marks: [
211
+ Mark::Stem.new(attrs: Mark::Stem::Attrs.new(stem_type: element.stem_type))
212
+ ])
213
+ end
214
+
215
+ def build_span_mark(element, context)
216
+ text = extract_inline_text(element)
217
+ return nil if text.empty?
218
+
219
+ role = element.attr('role')
220
+ context.text_node(text, marks: [
221
+ Mark::Span.new(attrs: Mark::Span::Attrs.new(role: role))
222
+ ])
223
+ end
224
+
225
+ def build_footnote_node(element, context)
226
+ footnote = nil
227
+ if element.is_a?(CoreModel::InlineElement) && element.content
228
+ fn_id = element.attr('id')
229
+ footnote = CoreModel::Footnote.new(
230
+ id: fn_id,
231
+ content: element.content.to_s
232
+ )
233
+ end
234
+
235
+ context.register_footnote(footnote)
236
+ end
237
+
238
+ def build_text_only(element, context)
239
+ text = extract_inline_text(element)
240
+ return nil if text.empty?
241
+
242
+ context.text_node(text)
243
+ end
244
+
245
+ # Passthrough content carries its own raw output-format markup
246
+ # (HTML, XML, etc.). Emit a typed RawInline node so renderers
247
+ # can skip escaping unambiguously — never a plain text node,
248
+ # which would force every downstream consumer to choose between
249
+ # XSS risk and visible literal markup.
250
+ def build_raw_inline(element, _context)
251
+ text = element.content.to_s
252
+ return nil if text.empty?
253
+
254
+ Node::RawInline.new(text: text)
255
+ end
256
+
257
+ def extract_inline_text(element)
258
+ return element.content.to_s if element.content && !element.content.to_s.empty?
259
+
260
+ if element.is_a?(CoreModel::InlineElement) && element.nested_elements
261
+ return element.nested_elements.map do |nested|
262
+ extract_inline_text(nested)
263
+ end.join
264
+ end
265
+
266
+ if (element.is_a?(CoreModel::InlineElement) || element.is_a?(CoreModel::Block)) && element.children && !element.children.empty?
267
+ return element.children.map do |child|
268
+ case child
269
+ when CoreModel::TextContent then child.text.to_s
270
+ when CoreModel::InlineElement then extract_inline_text(child)
271
+ else ''
272
+ end
273
+ end.join
274
+ end
275
+
276
+ ''
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end