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.
- checksums.yaml +7 -0
- data/lib/coridoc/mirror/core_model_to_mirror.rb +221 -0
- data/lib/coridoc/mirror/frontmatter_query.rb +50 -0
- data/lib/coridoc/mirror/frontmatter_tree_to_hash.rb +40 -0
- data/lib/coridoc/mirror/handler_registry.rb +94 -0
- data/lib/coridoc/mirror/handlers/abstract.rb +19 -0
- data/lib/coridoc/mirror/handlers/admonition.rb +29 -0
- data/lib/coridoc/mirror/handlers/bibliography.rb +43 -0
- data/lib/coridoc/mirror/handlers/blockquote.rb +19 -0
- data/lib/coridoc/mirror/handlers/code_block.rb +62 -0
- data/lib/coridoc/mirror/handlers/comment.rb +14 -0
- data/lib/coridoc/mirror/handlers/definition_list.rb +133 -0
- data/lib/coridoc/mirror/handlers/example.rb +19 -0
- data/lib/coridoc/mirror/handlers/footnote.rb +18 -0
- data/lib/coridoc/mirror/handlers/frontmatter.rb +71 -0
- data/lib/coridoc/mirror/handlers/generic_block.rb +24 -0
- data/lib/coridoc/mirror/handlers/horizontal_rule.rb +14 -0
- data/lib/coridoc/mirror/handlers/image.rb +64 -0
- data/lib/coridoc/mirror/handlers/include.rb +50 -0
- data/lib/coridoc/mirror/handlers/inline.rb +282 -0
- data/lib/coridoc/mirror/handlers/list.rb +80 -0
- data/lib/coridoc/mirror/handlers/open_block.rb +16 -0
- data/lib/coridoc/mirror/handlers/paragraph.rb +16 -0
- data/lib/coridoc/mirror/handlers/partintro.rb +19 -0
- data/lib/coridoc/mirror/handlers/reviewer.rb +14 -0
- data/lib/coridoc/mirror/handlers/sidebar.rb +19 -0
- data/lib/coridoc/mirror/handlers/structural.rb +84 -0
- data/lib/coridoc/mirror/handlers/table.rb +82 -0
- data/lib/coridoc/mirror/handlers/toc.rb +48 -0
- data/lib/coridoc/mirror/handlers/verse.rb +22 -0
- data/lib/coridoc/mirror/handlers.rb +41 -0
- data/lib/coridoc/mirror/mark.rb +181 -0
- data/lib/coridoc/mirror/mark_reverse_builder.rb +142 -0
- data/lib/coridoc/mirror/mirror_json_format.rb +48 -0
- data/lib/coridoc/mirror/mirror_to_core_model.rb +93 -0
- data/lib/coridoc/mirror/mirror_yaml_format.rb +47 -0
- data/lib/coridoc/mirror/node.rb +1005 -0
- data/lib/coridoc/mirror/partitioner.rb +75 -0
- data/lib/coridoc/mirror/reverse_builder/abstract.rb +17 -0
- data/lib/coridoc/mirror/reverse_builder/admonition.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder/base.rb +42 -0
- data/lib/coridoc/mirror/reverse_builder/biblio_entry.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/bibliography.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/blockquote.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder/bullet_list.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/caption.rb +15 -0
- data/lib/coridoc/mirror/reverse_builder/code_block.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/definition_list.rb +31 -0
- data/lib/coridoc/mirror/reverse_builder/document.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/example.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder/figure.rb +31 -0
- data/lib/coridoc/mirror/reverse_builder/footnote_entry.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/footnote_marker.rb +20 -0
- data/lib/coridoc/mirror/reverse_builder/footnotes.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder/frontmatter.rb +19 -0
- data/lib/coridoc/mirror/reverse_builder/generic_block.rb +22 -0
- data/lib/coridoc/mirror/reverse_builder/hard_break.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/header.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/horizontal_rule.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/image.rb +21 -0
- data/lib/coridoc/mirror/reverse_builder/include.rb +42 -0
- data/lib/coridoc/mirror/reverse_builder/inline_text.rb +15 -0
- data/lib/coridoc/mirror/reverse_builder/list_item.rb +33 -0
- data/lib/coridoc/mirror/reverse_builder/literal_block.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/open_block.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/ordered_list.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/paragraph.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/partintro.rb +17 -0
- data/lib/coridoc/mirror/reverse_builder/pass_block.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/preamble.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/raw_inline.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/section.rb +22 -0
- data/lib/coridoc/mirror/reverse_builder/sections.rb +15 -0
- data/lib/coridoc/mirror/reverse_builder/sidebar.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder/soft_break.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/stem_block.rb +18 -0
- data/lib/coridoc/mirror/reverse_builder/table.rb +23 -0
- data/lib/coridoc/mirror/reverse_builder/table_body.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/table_cell.rb +20 -0
- data/lib/coridoc/mirror/reverse_builder/table_head.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/table_row.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/text.rb +20 -0
- data/lib/coridoc/mirror/reverse_builder/toc.rb +13 -0
- data/lib/coridoc/mirror/reverse_builder/toc_entry.rb +14 -0
- data/lib/coridoc/mirror/reverse_builder/verse.rb +16 -0
- data/lib/coridoc/mirror/reverse_builder.rb +147 -0
- data/lib/coridoc/mirror/transformer.rb +41 -0
- data/lib/coridoc/mirror/version.rb +7 -0
- data/lib/coridoc/mirror.rb +177 -0
- data/lib/coridoc-mirror.rb +12 -0
- metadata +191 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a8d41d26ea0afc02613e7bb144b733e2739972a9f68c0bcfa14264176d31d7a1
|
|
4
|
+
data.tar.gz: f94fd697e60c9da411e5615d364927653b27c249b96110a7c6133a54229d0811
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 07cef4857f7ae4b9569aac2c9720431b5142288b1eaacb28140ecdc2b8ff9b6c220e2744eb8a6b1f0687a25609affce22000d0aa571374a66fb22f7068ea2a21
|
|
7
|
+
data.tar.gz: '0033977fb4f32716d94eb5225aa156a4cb2ab92e6ac8f592d8ba970f9de39ea3de2294f223723640e33d9f8ad1ce10f6346ad50c0ffa267b6eaa17a458f924e8'
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
# Transforms CoreModel documents into ProseMirror-compatible Mirror nodes.
|
|
6
|
+
#
|
|
7
|
+
# Uses a HandlerRegistry for OCP-compliant dispatch: each CoreModel type
|
|
8
|
+
# maps to a handler module/class that produces the corresponding Mirror node.
|
|
9
|
+
class CoreModelToMirror
|
|
10
|
+
attr_reader :registry
|
|
11
|
+
|
|
12
|
+
# Typed struct for pending footnote data (model-driven, not hash bags).
|
|
13
|
+
FootnoteData = Struct.new(:id, :ref_id, :number, :content, keyword_init: true)
|
|
14
|
+
|
|
15
|
+
# Maps element types to their children accessors.
|
|
16
|
+
COLLECTION_ACCESSORS = {
|
|
17
|
+
CoreModel::ListBlock => :items,
|
|
18
|
+
CoreModel::DefinitionList => :items,
|
|
19
|
+
CoreModel::Table => :rows,
|
|
20
|
+
CoreModel::Bibliography => :entries
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
def initialize(registry: Coridoc::Mirror.default_registry)
|
|
24
|
+
@registry = registry
|
|
25
|
+
@footnote_counter = 0
|
|
26
|
+
@footnotes = []
|
|
27
|
+
@partition_structural = false
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Read by Handlers::Structural.section to decide whether to emit
|
|
31
|
+
# generic `section` (legacy) or a JS SECTION_TYPE (`clause`, `annex`,
|
|
32
|
+
# etc.) when partition_structural mode is on.
|
|
33
|
+
attr_accessor :partition_structural
|
|
34
|
+
|
|
35
|
+
def call(document, partition_structural: false)
|
|
36
|
+
@footnote_counter = 0
|
|
37
|
+
@footnotes = []
|
|
38
|
+
@partition_structural = partition_structural
|
|
39
|
+
|
|
40
|
+
content = extract_content(document)
|
|
41
|
+
fn_block = flush_footnotes
|
|
42
|
+
content << fn_block if fn_block
|
|
43
|
+
|
|
44
|
+
attrs = build_document_attrs(document)
|
|
45
|
+
Node::Document.new(
|
|
46
|
+
attrs: Node::Document::Attrs.new(title: attrs[:title], id: attrs[:id]),
|
|
47
|
+
content: partition_structural ? wrap_structural(content) : content
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Partitions flat doc children into [*metadata, preface?, sections?,
|
|
52
|
+
# *bibliography, *trailing] per the @metanorma/mirror JS structural
|
|
53
|
+
# contract. See Partitioner for the bucketing rules.
|
|
54
|
+
#
|
|
55
|
+
# Metadata blocks (frontmatter) are prepended verbatim so consumers
|
|
56
|
+
# like FrontmatterQuery can find them by walking content[0..n], and
|
|
57
|
+
# renderers can skip them via their type ('frontmatter').
|
|
58
|
+
def wrap_structural(children)
|
|
59
|
+
partitioned = Partitioner.partition(children)
|
|
60
|
+
wrapped = []
|
|
61
|
+
wrapped.concat(partitioned[:metadata])
|
|
62
|
+
wrapped << Node::Preamble.new(content: partitioned[:preface]) if partitioned[:preface].any?
|
|
63
|
+
wrapped << Node::Sections.new(content: partitioned[:sections]) if partitioned[:sections].any?
|
|
64
|
+
wrapped.concat(partitioned[:bibliography])
|
|
65
|
+
wrapped.concat(partitioned[:trailing])
|
|
66
|
+
wrapped
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def extract_content(element)
|
|
70
|
+
children = element_children(element)
|
|
71
|
+
|
|
72
|
+
if children && !children.empty?
|
|
73
|
+
content = []
|
|
74
|
+
children.each { |child| handle_element(child, content) }
|
|
75
|
+
content.compact
|
|
76
|
+
elsif element_has_text_content?(element)
|
|
77
|
+
process_inline_content(element)
|
|
78
|
+
else
|
|
79
|
+
[]
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def process_inline_content(element)
|
|
84
|
+
Handlers::Inline.process(element, context: self)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def text_node(text, marks: [])
|
|
88
|
+
Node::Text.new(text: text, marks: marks)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def register_footnote(footnote)
|
|
92
|
+
@footnote_counter += 1
|
|
93
|
+
num = @footnote_counter
|
|
94
|
+
fn_id = footnote.id || "fn-#{num}"
|
|
95
|
+
ref_id = "fn-ref-#{num}"
|
|
96
|
+
|
|
97
|
+
fn_content = footnote.content ? [text_node(footnote.content)] : []
|
|
98
|
+
@footnotes << FootnoteData.new(
|
|
99
|
+
id: fn_id, ref_id: ref_id, number: num, content: fn_content
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
Node::FootnoteMarker.new(
|
|
103
|
+
attrs: Node::FootnoteMarker::Attrs.new(
|
|
104
|
+
id: fn_id, ref_id: ref_id, number: num
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def resolve_footnote_reference(ref)
|
|
110
|
+
target_id = ref.id
|
|
111
|
+
entry = @footnotes.find { |fn| fn.id == target_id } if target_id
|
|
112
|
+
|
|
113
|
+
if entry
|
|
114
|
+
Node::FootnoteMarker.new(
|
|
115
|
+
attrs: Node::FootnoteMarker::Attrs.new(
|
|
116
|
+
id: entry.id,
|
|
117
|
+
ref_id: "fn-ref-#{entry.number}-dup-#{@footnote_counter}",
|
|
118
|
+
number: entry.number
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
else
|
|
122
|
+
text_node("[#{target_id || 'footnote'}]")
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def flush_footnotes
|
|
127
|
+
return nil if @footnotes.empty?
|
|
128
|
+
|
|
129
|
+
entries = @footnotes.map do |fn|
|
|
130
|
+
Node::FootnoteEntry.new(
|
|
131
|
+
attrs: Node::FootnoteEntry::Attrs.new(
|
|
132
|
+
id: fn.id, ref_id: fn.ref_id, number: fn.number
|
|
133
|
+
),
|
|
134
|
+
content: fn.content
|
|
135
|
+
)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
@footnotes = []
|
|
139
|
+
Node::Footnotes.new(content: entries)
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Public entrypoint for handlers that need to dispatch a nested
|
|
143
|
+
# CoreModel element through the same registry path as a top-level
|
|
144
|
+
# walk (e.g., `+`-continuation blocks attached to a dlist dd must
|
|
145
|
+
# be transformed by their own registered handler rather than
|
|
146
|
+
# inlined as text). Returns an Array of Mirror nodes (possibly
|
|
147
|
+
# empty). Single source of truth for "transform one element" so
|
|
148
|
+
# handlers don't bypass source-line propagation.
|
|
149
|
+
def transform_element(element)
|
|
150
|
+
result = @registry.handle(element, context: self)
|
|
151
|
+
return [] unless result
|
|
152
|
+
|
|
153
|
+
value, = result
|
|
154
|
+
return [] unless value
|
|
155
|
+
|
|
156
|
+
propagate_source_line(value, element)
|
|
157
|
+
Array(value)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
private
|
|
161
|
+
|
|
162
|
+
def element_has_text_content?(element)
|
|
163
|
+
element.is_a?(CoreModel::Block) &&
|
|
164
|
+
element.content &&
|
|
165
|
+
!element.content.to_s.empty?
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def element_children(element)
|
|
169
|
+
if element.is_a?(Coridoc::CoreModel::HasChildren)
|
|
170
|
+
children = element.children
|
|
171
|
+
return children if children && !children.empty?
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
accessor = COLLECTION_ACCESSORS[element.class]
|
|
175
|
+
element.public_send(accessor) if accessor
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def handle_element(element, content)
|
|
179
|
+
result = @registry.handle(element, context: self)
|
|
180
|
+
return unless result
|
|
181
|
+
|
|
182
|
+
value, concat = result
|
|
183
|
+
return unless value
|
|
184
|
+
|
|
185
|
+
propagate_source_line(value, element)
|
|
186
|
+
concat ? content.concat(Array(value)) : content << value
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# Copy parser-attached source_line from the CoreModel element onto
|
|
190
|
+
# every Mirror node the handler produced. Single touchpoint for the
|
|
191
|
+
# entire CoreModel → Mirror direction — handlers stay focused on
|
|
192
|
+
# per-type mapping and don't repeat this concern (DRY).
|
|
193
|
+
def propagate_source_line(value, element)
|
|
194
|
+
line = element.source_line
|
|
195
|
+
return unless line
|
|
196
|
+
|
|
197
|
+
Array(value).each do |node|
|
|
198
|
+
next unless node.is_a?(Node)
|
|
199
|
+
next if node.source_line
|
|
200
|
+
|
|
201
|
+
node.source_line = line
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def build_document_attrs(document)
|
|
206
|
+
attrs = {}
|
|
207
|
+
attrs[:title] = document.title if document.title
|
|
208
|
+
attrs[:id] = document.id if document.id
|
|
209
|
+
|
|
210
|
+
if document.is_a?(CoreModel::DocumentElement) &&
|
|
211
|
+
document.attributes.is_a?(CoreModel::Metadata)
|
|
212
|
+
document.attributes.entries&.each do |entry|
|
|
213
|
+
attrs[entry.key.to_sym] = entry.value
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
attrs
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
# Public read-API: extract a flat Ruby Hash from a Mirror document's
|
|
6
|
+
# frontmatter node.
|
|
7
|
+
#
|
|
8
|
+
# Why this exists: site generators (e.g. metanorma.org's convert-adoc.rb)
|
|
9
|
+
# need frontmatter as a plain Hash for templating VitePress/Jekyll
|
|
10
|
+
# frontmatter output. Without this, callers would either (a) re-parse
|
|
11
|
+
# the source YAML — violating FrontmatterBlock::Codec's "single source
|
|
12
|
+
# of truth" rule — or (b) hand-walk the typed FrontmatterEntry /
|
|
13
|
+
# FrontmatterValue tree themselves, duplicating the reverse builder's
|
|
14
|
+
# logic. This module is the single entry point for both problems.
|
|
15
|
+
#
|
|
16
|
+
# Reuses FrontmatterTreeToHash — same translator the reverse builder
|
|
17
|
+
# uses — so the read-path is shared (DRY/MECE).
|
|
18
|
+
module FrontmatterQuery
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# @param mirror_doc [Mirror::Node::Document, nil]
|
|
22
|
+
# @return [Hash{String,Object}] flat key→value mapping; empty Hash
|
|
23
|
+
# if the document has no frontmatter node or no entries
|
|
24
|
+
def to_hash(mirror_doc)
|
|
25
|
+
frontmatter = find_frontmatter(mirror_doc)
|
|
26
|
+
return {} unless frontmatter
|
|
27
|
+
|
|
28
|
+
entries = frontmatter.attrs&.entries || []
|
|
29
|
+
FrontmatterTreeToHash.to_hash(entries)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @param mirror_doc [Mirror::Node::Document, nil] (see #to_hash)
|
|
33
|
+
# @return [Boolean] true if the document carries a frontmatter node
|
|
34
|
+
# with at least one entry
|
|
35
|
+
def has_frontmatter?(mirror_doc)
|
|
36
|
+
frontmatter = find_frontmatter(mirror_doc)
|
|
37
|
+
!frontmatter.nil? && !(frontmatter.attrs&.entries || []).empty?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def find_frontmatter(mirror_doc)
|
|
41
|
+
return nil if mirror_doc.nil?
|
|
42
|
+
|
|
43
|
+
content = mirror_doc.content
|
|
44
|
+
return nil unless content
|
|
45
|
+
|
|
46
|
+
content.find { |node| node.is_a?(Node) && node.type == 'frontmatter' }
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
# Walks a typed FrontmatterEntry / FrontmatterValue tree (the Mirror
|
|
6
|
+
# representation of CoreModel::FrontmatterBlock#data) and rebuilds the
|
|
7
|
+
# flat Ruby Hash. Inverse of Handlers::Frontmatter.build_value.
|
|
8
|
+
#
|
|
9
|
+
# Single source of truth for tree → Hash translation. Consumed by:
|
|
10
|
+
# - ReverseBuilder::Frontmatter (mirror → CoreModel round-trip)
|
|
11
|
+
# - FrontmatterQuery (mirror doc → flat Hash for downstream readers)
|
|
12
|
+
#
|
|
13
|
+
# Extracted from ReverseBuilder so the read-path is shared (DRY/MECE)
|
|
14
|
+
# and FrontmatterQuery does not depend on the reverse-builder constant.
|
|
15
|
+
module FrontmatterTreeToHash
|
|
16
|
+
module_function
|
|
17
|
+
|
|
18
|
+
def to_hash(entries)
|
|
19
|
+
entries.each_with_object({}) do |entry, result|
|
|
20
|
+
result[entry.key] = unwrap_value(entry.value)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def unwrap_value(value)
|
|
25
|
+
case value.value_type
|
|
26
|
+
when 'map' then to_hash(value.entries || [])
|
|
27
|
+
when 'array' then (value.items || []).map { |v| unwrap_value(v) }
|
|
28
|
+
when 'integer' then value.integer_value
|
|
29
|
+
when 'float' then value.float_value
|
|
30
|
+
when 'boolean' then value.boolean_value
|
|
31
|
+
when 'date' then value.date_value
|
|
32
|
+
when 'datetime' then value.datetime_value
|
|
33
|
+
when 'symbol' then value.symbol_value&.to_sym
|
|
34
|
+
when 'nil' then nil
|
|
35
|
+
else value.string_value
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
# Open registry mapping CoreModel classes to handler modules.
|
|
6
|
+
#
|
|
7
|
+
# Replaces closed case/when dispatch with an extensible registry.
|
|
8
|
+
# Third-party gems can register additional handlers without modifying
|
|
9
|
+
# core classes (OCP).
|
|
10
|
+
#
|
|
11
|
+
# Lookup (including ancestor walking) is delegated to
|
|
12
|
+
# `Coridoc::Dispatch.hierarchical`. This class keeps the Entry shape and
|
|
13
|
+
# the handler-invocation semantics — those are mirror-specific.
|
|
14
|
+
#
|
|
15
|
+
# @example Registering a handler
|
|
16
|
+
# registry = Coridoc::Mirror.default_registry
|
|
17
|
+
# registry.register(MyCustomBlock, MyHandler)
|
|
18
|
+
#
|
|
19
|
+
# @example Creating a custom registry
|
|
20
|
+
# registry = Coridoc::Mirror::HandlerRegistry.new
|
|
21
|
+
# registry.register(Coridoc::CoreModel::ParagraphBlock, MyParagraphHandler)
|
|
22
|
+
#
|
|
23
|
+
class HandlerRegistry
|
|
24
|
+
Entry = Struct.new(:handler, :method_name, :concat, :extra_kwargs,
|
|
25
|
+
keyword_init: true)
|
|
26
|
+
|
|
27
|
+
def initialize
|
|
28
|
+
@dispatch = Coridoc::Dispatch.hierarchical
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Register a handler for a CoreModel class.
|
|
32
|
+
#
|
|
33
|
+
# @param model_class [Class] CoreModel class to handle
|
|
34
|
+
# @param handler [Module, Class, Proc] handler implementation.
|
|
35
|
+
# If a Module/Class, +method_name+ is called on it.
|
|
36
|
+
# If a Proc, called directly with (element, context:).
|
|
37
|
+
# @param method_name [Symbol] method to call on handler (default: :call)
|
|
38
|
+
# @param concat [Boolean] if true, handler result is an array to
|
|
39
|
+
# concat into content rather than a single item to append
|
|
40
|
+
# @param extra_kwargs [Hash] additional keyword arguments passed
|
|
41
|
+
# to the handler
|
|
42
|
+
def register(model_class, handler, method_name: :call, concat: false,
|
|
43
|
+
extra_kwargs: {})
|
|
44
|
+
@dispatch.register(
|
|
45
|
+
model_class,
|
|
46
|
+
Entry.new(
|
|
47
|
+
handler: handler,
|
|
48
|
+
method_name: method_name,
|
|
49
|
+
concat: concat,
|
|
50
|
+
extra_kwargs: extra_kwargs
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Find the handler entry for a given CoreModel element.
|
|
56
|
+
#
|
|
57
|
+
# Walks the element's class ancestors (via Dispatch.hierarchical) to
|
|
58
|
+
# find the most specific registered handler. This allows registering
|
|
59
|
+
# a handler for a base class (e.g., Block) that applies to all
|
|
60
|
+
# subclasses, while also registering specific handlers for subclasses.
|
|
61
|
+
#
|
|
62
|
+
# @param element [CoreModel::Base] element to find handler for
|
|
63
|
+
# @return [Entry, nil]
|
|
64
|
+
def entry_for(element)
|
|
65
|
+
@dispatch.lookup(element.class)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def registered?(model_class)
|
|
69
|
+
@dispatch.registered?(model_class)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Invoke the handler for a given element.
|
|
73
|
+
#
|
|
74
|
+
# @param element [CoreModel::Base] element to handle
|
|
75
|
+
# @param context [CoreModelToMirror] transformer context
|
|
76
|
+
# @return [Array(result, concat_flag), nil] handler result or nil
|
|
77
|
+
def handle(element, context:)
|
|
78
|
+
entry = entry_for(element)
|
|
79
|
+
return nil unless entry
|
|
80
|
+
|
|
81
|
+
kwargs = { context: context }.merge(entry.extra_kwargs || {})
|
|
82
|
+
|
|
83
|
+
result = case entry.handler
|
|
84
|
+
when Proc
|
|
85
|
+
entry.handler.call(element, context)
|
|
86
|
+
else
|
|
87
|
+
entry.handler.public_send(entry.method_name, element, **kwargs)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
[result, entry.concat]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
module Handlers
|
|
6
|
+
module Abstract
|
|
7
|
+
def self.call(element, context:)
|
|
8
|
+
content = context.extract_content(element)
|
|
9
|
+
return nil if content.empty?
|
|
10
|
+
|
|
11
|
+
Node::Abstract.new(
|
|
12
|
+
attrs: Node::Abstract::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,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
module Handlers
|
|
6
|
+
# Admonition (NOTE, TIP, WARNING, CAUTION, IMPORTANT) handler.
|
|
7
|
+
#
|
|
8
|
+
# Emits a dialect-agnostic `Node::Admonition`. The canonical Ruby
|
|
9
|
+
# attribute is `admonition_type`; the model renames it to the wire
|
|
10
|
+
# name `type` on #to_h unconditionally. No flag, no dialect branch.
|
|
11
|
+
module Admonition
|
|
12
|
+
def self.call(element, context:)
|
|
13
|
+
content = context.extract_content(element)
|
|
14
|
+
return nil if content.empty?
|
|
15
|
+
|
|
16
|
+
Node::Admonition.new(
|
|
17
|
+
attrs: Node::Admonition::Attrs.new(
|
|
18
|
+
admonition_type: element.annotation_type,
|
|
19
|
+
title: element.title,
|
|
20
|
+
label: element.annotation_label,
|
|
21
|
+
id: element.id
|
|
22
|
+
),
|
|
23
|
+
content: content
|
|
24
|
+
)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
module Handlers
|
|
6
|
+
module Bibliography
|
|
7
|
+
def self.call(element, context:)
|
|
8
|
+
entries = Array(element.entries).filter_map do |entry|
|
|
9
|
+
build_entry(entry, context)
|
|
10
|
+
end
|
|
11
|
+
return nil if entries.empty?
|
|
12
|
+
|
|
13
|
+
Node::Bibliography.new(
|
|
14
|
+
attrs: Node::Bibliography::Attrs.new(
|
|
15
|
+
id: element.id,
|
|
16
|
+
title: element.title,
|
|
17
|
+
level: element.level
|
|
18
|
+
),
|
|
19
|
+
content: entries
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def build_entry(entry, context)
|
|
27
|
+
text = entry.display_text
|
|
28
|
+
return nil if text.nil? || text.empty?
|
|
29
|
+
|
|
30
|
+
Node::BibliographyEntry.new(
|
|
31
|
+
attrs: Node::BibliographyEntry::Attrs.new(
|
|
32
|
+
anchor_name: entry.anchor_name,
|
|
33
|
+
document_id: entry.document_id,
|
|
34
|
+
url: entry.url
|
|
35
|
+
),
|
|
36
|
+
content: [context.text_node(text)]
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
module Handlers
|
|
6
|
+
module Blockquote
|
|
7
|
+
def self.call(element, context:)
|
|
8
|
+
content = context.extract_content(element)
|
|
9
|
+
return nil if content.empty?
|
|
10
|
+
|
|
11
|
+
Node::Blockquote.new(
|
|
12
|
+
attrs: Node::Blockquote::Attrs.new(attribution: element.attribution),
|
|
13
|
+
content: content
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Mirror
|
|
5
|
+
module Handlers
|
|
6
|
+
module CodeBlock
|
|
7
|
+
def self.source(element, context:)
|
|
8
|
+
build_code_block(element, context, node_class: Node::CodeBlock)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.listing(element, context:)
|
|
12
|
+
build_code_block(element, context, node_class: Node::CodeBlock)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.literal(element, context:)
|
|
16
|
+
build_code_block(element, context, node_class: Node::LiteralBlock)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.pass(element, context:)
|
|
20
|
+
build_code_block(element, context, node_class: Node::PassBlock, passthrough: true)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.stem(element, context:)
|
|
24
|
+
build_code_block(element, context, node_class: Node::StemBlock)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def build_code_block(element, context, node_class:, passthrough: false)
|
|
31
|
+
text = extract_text(element)
|
|
32
|
+
js_mode = context.partition_structural
|
|
33
|
+
attrs = Node::CodeBlock::Attrs.new(
|
|
34
|
+
title: element.title,
|
|
35
|
+
language: element.language,
|
|
36
|
+
passthrough: passthrough || nil,
|
|
37
|
+
text: js_mode ? text : nil
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
if js_mode
|
|
41
|
+
node_class.new(attrs: attrs, content: [])
|
|
42
|
+
else
|
|
43
|
+
node_class.new(attrs: attrs, content: [context.text_node(text)])
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def extract_text(element)
|
|
48
|
+
return '' unless element.is_a?(CoreModel::Block)
|
|
49
|
+
|
|
50
|
+
if element.content && !element.content.to_s.empty?
|
|
51
|
+
element.flat_text || element.content.to_s
|
|
52
|
+
elsif element.lines && !element.lines.empty?
|
|
53
|
+
Array(element.lines).join("\n")
|
|
54
|
+
else
|
|
55
|
+
''
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|