coridoc-markdown 1.0.15
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/LICENSE.txt +21 -0
- data/lib/coridoc/markdown/behaviors/admonition.rb +21 -0
- data/lib/coridoc/markdown/behaviors/attribute_list.rb +81 -0
- data/lib/coridoc/markdown/behaviors/document.rb +34 -0
- data/lib/coridoc/markdown/behaviors/extension.rb +69 -0
- data/lib/coridoc/markdown/behaviors/heading.rb +33 -0
- data/lib/coridoc/markdown/behaviors/math.rb +32 -0
- data/lib/coridoc/markdown/behaviors/text.rb +14 -0
- data/lib/coridoc/markdown/behaviors.rb +18 -0
- data/lib/coridoc/markdown/bindings/captures.rb +145 -0
- data/lib/coridoc/markdown/bindings/scope.rb +31 -0
- data/lib/coridoc/markdown/bindings.rb +307 -0
- data/lib/coridoc/markdown/errors.rb +28 -0
- data/lib/coridoc/markdown/lint/registrations.rb +38 -0
- data/lib/coridoc/markdown/lint.rb +16 -0
- data/lib/coridoc/markdown/model/abbreviation.rb +27 -0
- data/lib/coridoc/markdown/model/admonition.rb +33 -0
- data/lib/coridoc/markdown/model/attribute_list.rb +25 -0
- data/lib/coridoc/markdown/model/base.rb +54 -0
- data/lib/coridoc/markdown/model/blockquote.rb +16 -0
- data/lib/coridoc/markdown/model/code.rb +11 -0
- data/lib/coridoc/markdown/model/code_block.rb +18 -0
- data/lib/coridoc/markdown/model/comment.rb +14 -0
- data/lib/coridoc/markdown/model/cross_reference.rb +10 -0
- data/lib/coridoc/markdown/model/definition_item.rb +26 -0
- data/lib/coridoc/markdown/model/definition_list.rb +35 -0
- data/lib/coridoc/markdown/model/definition_term.rb +29 -0
- data/lib/coridoc/markdown/model/document.rb +33 -0
- data/lib/coridoc/markdown/model/emphasis.rb +11 -0
- data/lib/coridoc/markdown/model/example_block.rb +21 -0
- data/lib/coridoc/markdown/model/extension.rb +23 -0
- data/lib/coridoc/markdown/model/footnote.rb +31 -0
- data/lib/coridoc/markdown/model/footnote_reference.rb +17 -0
- data/lib/coridoc/markdown/model/hard_line_break.rb +18 -0
- data/lib/coridoc/markdown/model/heading.rb +17 -0
- data/lib/coridoc/markdown/model/highlight.rb +13 -0
- data/lib/coridoc/markdown/model/horizontal_rule.rb +11 -0
- data/lib/coridoc/markdown/model/image.rb +19 -0
- data/lib/coridoc/markdown/model/link.rb +19 -0
- data/lib/coridoc/markdown/model/list.rb +22 -0
- data/lib/coridoc/markdown/model/list_item.rb +14 -0
- data/lib/coridoc/markdown/model/literal.rb +16 -0
- data/lib/coridoc/markdown/model/math.rb +21 -0
- data/lib/coridoc/markdown/model/named_value.rb +18 -0
- data/lib/coridoc/markdown/model/open_block.rb +17 -0
- data/lib/coridoc/markdown/model/paragraph.rb +15 -0
- data/lib/coridoc/markdown/model/pass.rb +16 -0
- data/lib/coridoc/markdown/model/sidebar.rb +18 -0
- data/lib/coridoc/markdown/model/strikethrough.rb +13 -0
- data/lib/coridoc/markdown/model/strong.rb +11 -0
- data/lib/coridoc/markdown/model/subscript.rb +9 -0
- data/lib/coridoc/markdown/model/superscript.rb +9 -0
- data/lib/coridoc/markdown/model/table.rb +13 -0
- data/lib/coridoc/markdown/model/text.rb +13 -0
- data/lib/coridoc/markdown/model/underline.rb +9 -0
- data/lib/coridoc/markdown/model/verse.rb +20 -0
- data/lib/coridoc/markdown/parser/ast_processor.rb +541 -0
- data/lib/coridoc/markdown/parser/attribute_lists.rb +48 -0
- data/lib/coridoc/markdown/parser/block_parser.rb +735 -0
- data/lib/coridoc/markdown/parser/frontmatter_parser.rb +23 -0
- data/lib/coridoc/markdown/parser/grammar.rb +56 -0
- data/lib/coridoc/markdown/parser/grammar_runtime.rb +164 -0
- data/lib/coridoc/markdown/parser/html_entities.rb +2149 -0
- data/lib/coridoc/markdown/parser/inline_parser.rb +296 -0
- data/lib/coridoc/markdown/parser/parsanol_atoms.rb +36 -0
- data/lib/coridoc/markdown/parser.rb +11 -0
- data/lib/coridoc/markdown/parser_util.rb +90 -0
- data/lib/coridoc/markdown/schema/definition.rb +81 -0
- data/lib/coridoc/markdown/schema/loader.rb +68 -0
- data/lib/coridoc/markdown/schema/parity.rb +101 -0
- data/lib/coridoc/markdown/schema.rb +53 -0
- data/lib/coridoc/markdown/serializer/builder.rb +57 -0
- data/lib/coridoc/markdown/serializer/config.rb +71 -0
- data/lib/coridoc/markdown/serializer/context.rb +66 -0
- data/lib/coridoc/markdown/serializer/element_serializer.rb +46 -0
- data/lib/coridoc/markdown/serializer/flavor.rb +82 -0
- data/lib/coridoc/markdown/serializer/registrations.rb +113 -0
- data/lib/coridoc/markdown/serializer/registry.rb +62 -0
- data/lib/coridoc/markdown/serializer/runner.rb +84 -0
- data/lib/coridoc/markdown/serializer/serializers/abbreviation.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/admonition.rb +20 -0
- data/lib/coridoc/markdown/serializer/serializers/attribute_list.rb +25 -0
- data/lib/coridoc/markdown/serializer/serializers/blockquote.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/code.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/code_block.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/comment.rb +31 -0
- data/lib/coridoc/markdown/serializer/serializers/cross_reference.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/definition_list.rb +20 -0
- data/lib/coridoc/markdown/serializer/serializers/document.rb +22 -0
- data/lib/coridoc/markdown/serializer/serializers/emphasis.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/example_block.rb +48 -0
- data/lib/coridoc/markdown/serializer/serializers/extension.rb +30 -0
- data/lib/coridoc/markdown/serializer/serializers/footnote.rb +20 -0
- data/lib/coridoc/markdown/serializer/serializers/footnote_reference.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/hard_line_break.rb +22 -0
- data/lib/coridoc/markdown/serializer/serializers/heading.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/highlight.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/horizontal_rule.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/image.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/link.rb +29 -0
- data/lib/coridoc/markdown/serializer/serializers/list.rb +43 -0
- data/lib/coridoc/markdown/serializer/serializers/literal.rb +26 -0
- data/lib/coridoc/markdown/serializer/serializers/math.rb +23 -0
- data/lib/coridoc/markdown/serializer/serializers/open_block.rb +38 -0
- data/lib/coridoc/markdown/serializer/serializers/paragraph.rb +23 -0
- data/lib/coridoc/markdown/serializer/serializers/pass.rb +30 -0
- data/lib/coridoc/markdown/serializer/serializers/sidebar.rb +33 -0
- data/lib/coridoc/markdown/serializer/serializers/strikethrough.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/strong.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/subscript.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/superscript.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/table.rb +37 -0
- data/lib/coridoc/markdown/serializer/serializers/text.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/underline.rb +19 -0
- data/lib/coridoc/markdown/serializer/serializers/verse.rb +31 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/base.rb +41 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/container.rb +34 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/gfm_alert.rb +28 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/github.rb +27 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/html.rb +25 -0
- data/lib/coridoc/markdown/serializer/strategies/admonition/registry.rb +50 -0
- data/lib/coridoc/markdown/serializer/strategies/autolink/angle.rb +35 -0
- data/lib/coridoc/markdown/serializer/strategies/autolink/bare.rb +23 -0
- data/lib/coridoc/markdown/serializer/strategies/autolink/base.rb +33 -0
- data/lib/coridoc/markdown/serializer/strategies/autolink/none.rb +27 -0
- data/lib/coridoc/markdown/serializer/strategies/autolink/registry.rb +58 -0
- data/lib/coridoc/markdown/serializer/strategies/definition_list/base.rb +37 -0
- data/lib/coridoc/markdown/serializer/strategies/definition_list/flat.rb +64 -0
- data/lib/coridoc/markdown/serializer/strategies/definition_list/nested_html.rb +54 -0
- data/lib/coridoc/markdown/serializer/strategies/definition_list/registry.rb +37 -0
- data/lib/coridoc/markdown/serializer.rb +48 -0
- data/lib/coridoc/markdown/toc_generator.rb +209 -0
- data/lib/coridoc/markdown/transform/block_transformer.rb +163 -0
- data/lib/coridoc/markdown/transform/from_core_model.rb +613 -0
- data/lib/coridoc/markdown/transform/image_transformer.rb +21 -0
- data/lib/coridoc/markdown/transform/inline_transformer.rb +78 -0
- data/lib/coridoc/markdown/transform/list_transformer.rb +58 -0
- data/lib/coridoc/markdown/transform/structural_transformer.rb +94 -0
- data/lib/coridoc/markdown/transform/table_transformer.rb +40 -0
- data/lib/coridoc/markdown/transform/text_extraction.rb +19 -0
- data/lib/coridoc/markdown/transform/to_core_model.rb +304 -0
- data/lib/coridoc/markdown/transformer.rb +48 -0
- data/lib/coridoc/markdown/version.rb +7 -0
- data/lib/coridoc/markdown.rb +218 -0
- data/schemas/blocks.yml +73 -0
- data/schemas/document.yml +55 -0
- data/schemas/inline.yml +57 -0
- data/schemas/lists.yml +35 -0
- data/schemas/tables.yml +11 -0
- metadata +279 -0
|
@@ -0,0 +1,613 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'cgi'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Markdown
|
|
7
|
+
module Transform
|
|
8
|
+
# Transforms CoreModel models to Markdown equivalents
|
|
9
|
+
#
|
|
10
|
+
# This transformer converts the canonical CoreModel representation
|
|
11
|
+
# to format-specific Markdown model.
|
|
12
|
+
class FromCoreModel
|
|
13
|
+
class << self
|
|
14
|
+
# Transform a CoreModel to Markdown model
|
|
15
|
+
#
|
|
16
|
+
# @param model [Coridoc::CoreModel::Base] CoreModel to transform
|
|
17
|
+
# @return [Coridoc::Markdown::Base] Markdown model equivalent
|
|
18
|
+
def transform(model)
|
|
19
|
+
case model
|
|
20
|
+
when Coridoc::CoreModel::StructuralElement
|
|
21
|
+
transform_structural_element(model)
|
|
22
|
+
when Coridoc::CoreModel::AnnotationBlock
|
|
23
|
+
# Must be checked before Block since AnnotationBlock < Block
|
|
24
|
+
transform_annotation_block(model)
|
|
25
|
+
when Coridoc::CoreModel::CommentBlock
|
|
26
|
+
# Must be checked before Block since CommentBlock < Block
|
|
27
|
+
Coridoc::Markdown::Comment.new(text: model.content.to_s)
|
|
28
|
+
when Coridoc::CoreModel::Block
|
|
29
|
+
transform_block(model)
|
|
30
|
+
when Coridoc::CoreModel::ListBlock
|
|
31
|
+
transform_list(model)
|
|
32
|
+
when Coridoc::CoreModel::DefinitionList
|
|
33
|
+
transform_definition_list(model)
|
|
34
|
+
when Coridoc::CoreModel::Table
|
|
35
|
+
transform_table(model)
|
|
36
|
+
when Coridoc::CoreModel::Image
|
|
37
|
+
transform_image(model)
|
|
38
|
+
when Coridoc::CoreModel::InlineElement
|
|
39
|
+
transform_inline(model)
|
|
40
|
+
when Coridoc::CoreModel::Footnote
|
|
41
|
+
transform_footnote(model)
|
|
42
|
+
when Coridoc::CoreModel::FootnoteReference
|
|
43
|
+
transform_footnote_reference(model)
|
|
44
|
+
when Coridoc::CoreModel::Abbreviation
|
|
45
|
+
transform_abbreviation(model)
|
|
46
|
+
when Coridoc::CoreModel::Toc
|
|
47
|
+
Coridoc::Markdown::Extension.toc
|
|
48
|
+
when Coridoc::CoreModel::Term
|
|
49
|
+
Coridoc::Markdown::Strong.new(text: model.text.to_s)
|
|
50
|
+
when Coridoc::CoreModel::Bibliography
|
|
51
|
+
transform_bibliography(model)
|
|
52
|
+
when Coridoc::CoreModel::BibliographyEntry
|
|
53
|
+
transform_bibliography_entry(model)
|
|
54
|
+
when Coridoc::CoreModel::TocEntry
|
|
55
|
+
Coridoc::Markdown::Text.new(content: model.title.to_s)
|
|
56
|
+
when Coridoc::CoreModel::CommentLine
|
|
57
|
+
Coridoc::Markdown::Comment.new(text: model.text.to_s)
|
|
58
|
+
when Coridoc::CoreModel::Include
|
|
59
|
+
transform_include(model)
|
|
60
|
+
when Coridoc::CoreModel::TextContent
|
|
61
|
+
model.text.to_s
|
|
62
|
+
when Array
|
|
63
|
+
model.flat_map { |item| flatten_result(transform(item)) }
|
|
64
|
+
else
|
|
65
|
+
model
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
# Markdown has no transclusion — an unresolved include edge is
|
|
72
|
+
# preserved as an HTML comment so no information is lost and
|
|
73
|
+
# serialization never crashes on graph-mode documents.
|
|
74
|
+
def transform_include(element)
|
|
75
|
+
Coridoc::Markdown::Comment.new(text: include_directive_text(element))
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def include_directive_text(element)
|
|
79
|
+
"include::#{element.target}[#{element.raw_options}]"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def transform_structural_element(element)
|
|
83
|
+
case element
|
|
84
|
+
when CoreModel::DocumentElement
|
|
85
|
+
transform_document(element)
|
|
86
|
+
when CoreModel::SectionElement
|
|
87
|
+
transform_section(element)
|
|
88
|
+
else
|
|
89
|
+
transform_generic_element(element)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def transform_document(doc)
|
|
94
|
+
blocks, frontmatter = extract_frontmatter(Array(doc.children))
|
|
95
|
+
blocks = blocks.flat_map { |child| flatten_result(transform(child)) }
|
|
96
|
+
blocks = prepend_title_heading(doc.title, blocks)
|
|
97
|
+
|
|
98
|
+
Coridoc::Markdown::Document.new(
|
|
99
|
+
id: doc.id,
|
|
100
|
+
blocks: blocks,
|
|
101
|
+
frontmatter: frontmatter
|
|
102
|
+
)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The document title renders as a leading "# {title}" H1. Skipped
|
|
106
|
+
# when the body already opens with a heading of the same text —
|
|
107
|
+
# a Markdown→CoreModel pass keeps its first heading (of any
|
|
108
|
+
# level) both as the title and as a section, and emitting the
|
|
109
|
+
# title again would duplicate it.
|
|
110
|
+
def prepend_title_heading(title, blocks)
|
|
111
|
+
return blocks if title.nil? || title.to_s.strip.empty?
|
|
112
|
+
return blocks if title_heading_duplicated?(title, blocks)
|
|
113
|
+
|
|
114
|
+
[Coridoc::Markdown::Heading.new(level: 1, text: title.to_s), *blocks]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def title_heading_duplicated?(title, blocks)
|
|
118
|
+
first = blocks.first
|
|
119
|
+
first.is_a?(Coridoc::Markdown::Heading) && first.text == title.to_s
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# If the first CoreModel child is a FrontmatterBlock, serialize
|
|
123
|
+
# it to YAML text via Codec (single source of truth) and pop
|
|
124
|
+
# it from the children list. Returns [remaining_children,
|
|
125
|
+
# frontmatter_text].
|
|
126
|
+
def extract_frontmatter(children)
|
|
127
|
+
first = children.first
|
|
128
|
+
return [children, nil] unless first.is_a?(CoreModel::FrontmatterBlock)
|
|
129
|
+
|
|
130
|
+
yaml = CoreModel::FrontmatterBlock::Codec.to_yaml(first)
|
|
131
|
+
[children.drop(1), yaml.nil? || yaml.empty? ? nil : yaml]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def transform_section(section)
|
|
135
|
+
heading = Coridoc::Markdown::Heading.new(
|
|
136
|
+
level: section.level || 1,
|
|
137
|
+
text: section.title.to_s
|
|
138
|
+
)
|
|
139
|
+
child_blocks = Array(section.children).map { |child| transform(child) }
|
|
140
|
+
[heading, *child_blocks]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def transform_generic_element(element)
|
|
144
|
+
blocks = Array(element.children).flat_map { |child| flatten_result(transform(child)) }
|
|
145
|
+
|
|
146
|
+
Coridoc::Markdown::Document.new(
|
|
147
|
+
id: element.id,
|
|
148
|
+
blocks: blocks
|
|
149
|
+
)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def transform_block(block)
|
|
153
|
+
semantic = block.resolve_semantic_type
|
|
154
|
+
case semantic
|
|
155
|
+
when :paragraph
|
|
156
|
+
transform_paragraph(block)
|
|
157
|
+
when :comment
|
|
158
|
+
Coridoc::Markdown::Comment.new(text: block.content.to_s)
|
|
159
|
+
else
|
|
160
|
+
transform_delimited_block(block)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def transform_paragraph(block)
|
|
165
|
+
content = block.renderable_content
|
|
166
|
+
has_nested_blocks = content.is_a?(Array) && content.any? do |c|
|
|
167
|
+
c.is_a?(CoreModel::Block) || c.is_a?(CoreModel::StructuralElement)
|
|
168
|
+
end
|
|
169
|
+
if has_nested_blocks
|
|
170
|
+
content.filter_map do |c|
|
|
171
|
+
next c.text if c.is_a?(CoreModel::TextContent)
|
|
172
|
+
next nil if c.is_a?(String) && c.strip.empty?
|
|
173
|
+
|
|
174
|
+
transform(c)
|
|
175
|
+
end.flat_map { |c| flatten_result(c) }
|
|
176
|
+
elsif content.is_a?(Array) && content.any? { |c| !c.is_a?(CoreModel::TextContent) }
|
|
177
|
+
children = content.map { |c| transform_inline_content(c) }
|
|
178
|
+
Coridoc::Markdown::Paragraph.new(text: block.flat_text, children: children)
|
|
179
|
+
else
|
|
180
|
+
Coridoc::Markdown::Paragraph.new(text: block.flat_text)
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def transform_inline_content(element)
|
|
185
|
+
case element
|
|
186
|
+
when Coridoc::CoreModel::InlineElement
|
|
187
|
+
transform_inline(element)
|
|
188
|
+
when CoreModel::TextContent
|
|
189
|
+
element.text
|
|
190
|
+
when CoreModel::Base
|
|
191
|
+
transform(element)
|
|
192
|
+
when String
|
|
193
|
+
element
|
|
194
|
+
when Array
|
|
195
|
+
element.map { |e| transform_inline_content(e) }
|
|
196
|
+
else
|
|
197
|
+
element.to_s
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def transform_inline_array(renderable)
|
|
202
|
+
return [] unless renderable.is_a?(Array)
|
|
203
|
+
|
|
204
|
+
renderable.map { |c| transform_inline_content(c) }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def transform_delimited_block(block)
|
|
208
|
+
semantic = resolve_markdown_semantic(block)
|
|
209
|
+
|
|
210
|
+
case semantic
|
|
211
|
+
when :source_code, :listing
|
|
212
|
+
transform_code_block(block)
|
|
213
|
+
when :quote
|
|
214
|
+
transform_blockquote(block)
|
|
215
|
+
when :verse
|
|
216
|
+
transform_verse_block(block)
|
|
217
|
+
when :horizontal_rule
|
|
218
|
+
transform_horizontal_rule(block)
|
|
219
|
+
when :pass
|
|
220
|
+
transform_pass_block(block)
|
|
221
|
+
when :literal
|
|
222
|
+
transform_literal_block(block)
|
|
223
|
+
when :example
|
|
224
|
+
transform_example_block(block)
|
|
225
|
+
when :sidebar
|
|
226
|
+
transform_sidebar_block(block)
|
|
227
|
+
when :open
|
|
228
|
+
transform_open_block(block)
|
|
229
|
+
when :reviewer
|
|
230
|
+
transform_admonition_block(block, default_type: 'reviewer')
|
|
231
|
+
else
|
|
232
|
+
transform_paragraph(block)
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def transform_container_block(block)
|
|
237
|
+
rc = block.renderable_content
|
|
238
|
+
if rc.is_a?(Array) && rc.any? { |c| c.is_a?(CoreModel::Block) || c.is_a?(CoreModel::ListBlock) }
|
|
239
|
+
rc.filter_map do |c|
|
|
240
|
+
next c.text if c.is_a?(CoreModel::TextContent)
|
|
241
|
+
next nil if c.is_a?(String) && c.strip.empty?
|
|
242
|
+
|
|
243
|
+
transform(c)
|
|
244
|
+
end.flat_map { |c| flatten_result(c) }
|
|
245
|
+
else
|
|
246
|
+
Coridoc::Markdown::Blockquote.new(content: block.flat_text)
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def transform_verse_block(block)
|
|
251
|
+
Coridoc::Markdown::Verse.new(
|
|
252
|
+
content: block.flat_text,
|
|
253
|
+
attribution: block.is_a?(Coridoc::CoreModel::VerseBlock) ? block.attribution : nil
|
|
254
|
+
)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def transform_pass_block(block)
|
|
258
|
+
return Coridoc::Markdown::Math.block(block.content.to_s) if block.language == 'latexmath'
|
|
259
|
+
|
|
260
|
+
Coridoc::Markdown::Pass.new(content: block.content.to_s)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def transform_literal_block(block)
|
|
264
|
+
content = CoreModel::CalloutText.strip_markers(block.content.to_s, block.callouts)
|
|
265
|
+
literal = Coridoc::Markdown::Literal.new(content: content)
|
|
266
|
+
return literal if block.callouts.nil? || block.callouts.empty?
|
|
267
|
+
|
|
268
|
+
[literal, transform_callout_list(block.callouts)]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def transform_example_block(block)
|
|
272
|
+
children = Array(block.children).map { |c| transform(c) }.flat_map { |c| flatten_result(c) }
|
|
273
|
+
Coridoc::Markdown::ExampleBlock.new(
|
|
274
|
+
content: block.flat_text,
|
|
275
|
+
caption: block.title.to_s,
|
|
276
|
+
children: children
|
|
277
|
+
)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def transform_sidebar_block(block)
|
|
281
|
+
children = Array(block.children).map { |c| transform(c) }.flat_map { |c| flatten_result(c) }
|
|
282
|
+
Coridoc::Markdown::Sidebar.new(
|
|
283
|
+
content: block.flat_text,
|
|
284
|
+
title: block.title.to_s,
|
|
285
|
+
children: children
|
|
286
|
+
)
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def transform_open_block(block)
|
|
290
|
+
children = Array(block.children).map { |c| transform(c) }.flat_map { |c| flatten_result(c) }
|
|
291
|
+
children = paragraphs_from_content(block.content) if children.empty?
|
|
292
|
+
Coridoc::Markdown::OpenBlock.new(
|
|
293
|
+
children: children,
|
|
294
|
+
id: block.id
|
|
295
|
+
)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def paragraphs_from_content(content)
|
|
299
|
+
content.to_s.split(/\n\s*\n/).map do |chunk|
|
|
300
|
+
Coridoc::Markdown::Paragraph.new(text: chunk.strip)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def transform_admonition_block(block, default_type: 'note')
|
|
305
|
+
children = transform_inline_array(block.renderable_content)
|
|
306
|
+
if block.is_a?(Coridoc::CoreModel::AnnotationBlock)
|
|
307
|
+
adm_type = block.annotation_type || default_type
|
|
308
|
+
adm_label = block.annotation_label
|
|
309
|
+
else
|
|
310
|
+
adm_type = default_type
|
|
311
|
+
adm_label = nil
|
|
312
|
+
end
|
|
313
|
+
Coridoc::Markdown::Admonition.new(
|
|
314
|
+
admonition_type: adm_type,
|
|
315
|
+
content: block.flat_text,
|
|
316
|
+
title: adm_label,
|
|
317
|
+
children: children
|
|
318
|
+
)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def resolve_markdown_semantic(block)
|
|
322
|
+
# Polymorphic dispatch: typed classes override semantic_type
|
|
323
|
+
semantic = block.resolve_semantic_type
|
|
324
|
+
return semantic if semantic
|
|
325
|
+
|
|
326
|
+
# Backward compat: derive from delimiter_type
|
|
327
|
+
markdown_delimiter_to_semantic(block.delimiter_type)
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
def markdown_delimiter_to_semantic(delimiter)
|
|
331
|
+
case delimiter
|
|
332
|
+
when '```', '~' then :source_code
|
|
333
|
+
when '>' then :quote
|
|
334
|
+
when '---', '***', '___' then :horizontal_rule
|
|
335
|
+
when '++++' then :pass
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def transform_code_block(block)
|
|
340
|
+
code = CoreModel::CalloutText.strip_markers(block.content.to_s, block.callouts)
|
|
341
|
+
code_block = Coridoc::Markdown::CodeBlock.new(
|
|
342
|
+
code: code,
|
|
343
|
+
language: block.language
|
|
344
|
+
)
|
|
345
|
+
return code_block if block.callouts.nil? || block.callouts.empty?
|
|
346
|
+
|
|
347
|
+
[code_block, transform_callout_list(block.callouts)]
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def transform_callout_list(callouts)
|
|
351
|
+
items = CoreModel::CalloutText.ordered(callouts).map do |callout|
|
|
352
|
+
Coridoc::Markdown::ListItem.new(text: callout.content.to_s)
|
|
353
|
+
end
|
|
354
|
+
Coridoc::Markdown::List.new(ordered: true, items: items)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
def transform_blockquote(block)
|
|
358
|
+
content = block.flat_text
|
|
359
|
+
|
|
360
|
+
Coridoc::Markdown::Blockquote.new(content: content)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def transform_horizontal_rule(_block)
|
|
364
|
+
Coridoc::Markdown::HorizontalRule.new(style: '---')
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def transform_list(list)
|
|
368
|
+
items = Array(list.items).map do |item|
|
|
369
|
+
content = item.renderable_content
|
|
370
|
+
has_structured = content.is_a?(Array) && content.any? { |c| !c.is_a?(CoreModel::TextContent) }
|
|
371
|
+
md_item = if has_structured
|
|
372
|
+
children = content.map { |c| transform_inline_content(c) }
|
|
373
|
+
Coridoc::Markdown::ListItem.new(text: item.flat_text, children: children)
|
|
374
|
+
else
|
|
375
|
+
Coridoc::Markdown::ListItem.new(text: item.flat_text)
|
|
376
|
+
end
|
|
377
|
+
md_item.sublist = transform_list(item.nested_list) if item.nested_list
|
|
378
|
+
md_item
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
Coridoc::Markdown::List.new(
|
|
382
|
+
ordered: list.marker_type == 'ordered',
|
|
383
|
+
items: items
|
|
384
|
+
)
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
def transform_table(table)
|
|
388
|
+
# Extract headers from first row if cells are marked as headers
|
|
389
|
+
headers = []
|
|
390
|
+
rows = []
|
|
391
|
+
|
|
392
|
+
table_rows = Array(table.rows)
|
|
393
|
+
if table_rows.any?
|
|
394
|
+
first_row = table_rows.first
|
|
395
|
+
first_row_cells = Array(first_row&.cells)
|
|
396
|
+
|
|
397
|
+
# Check if first row is marked as header, or if any of its cells are header cells
|
|
398
|
+
if first_row&.header || first_row_cells.any?(&:header)
|
|
399
|
+
headers = first_row_cells.map { |cell| strip_cell_callouts(cell.flat_text) }
|
|
400
|
+
table_rows = table_rows[1..] || []
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# Convert remaining rows to pipe-separated strings
|
|
404
|
+
rows = table_rows.map do |row|
|
|
405
|
+
Array(row.cells).map { |cell| strip_cell_callouts(cell.flat_text) }.join(' | ')
|
|
406
|
+
end
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
Coridoc::Markdown::Table.new(
|
|
410
|
+
headers: headers,
|
|
411
|
+
rows: rows
|
|
412
|
+
)
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
CALLOUT_MARKER_IN_CELL = /\s*<\d+>(?=\s|\z)/
|
|
416
|
+
|
|
417
|
+
def strip_cell_callouts(text)
|
|
418
|
+
text.to_s.gsub(CALLOUT_MARKER_IN_CELL, '')
|
|
419
|
+
end
|
|
420
|
+
|
|
421
|
+
def transform_image(image)
|
|
422
|
+
Coridoc::Markdown::Image.new(
|
|
423
|
+
src: image.src,
|
|
424
|
+
alt: image.alt.to_s,
|
|
425
|
+
title: image.title
|
|
426
|
+
)
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def transform_inline(element)
|
|
430
|
+
case element.resolve_format_type
|
|
431
|
+
when 'bold'
|
|
432
|
+
Coridoc::Markdown::Strong.new(text: element.content.to_s)
|
|
433
|
+
when 'italic'
|
|
434
|
+
Coridoc::Markdown::Emphasis.new(text: element.content.to_s)
|
|
435
|
+
when 'monospace'
|
|
436
|
+
Coridoc::Markdown::Code.new(text: element.content.to_s)
|
|
437
|
+
when 'link'
|
|
438
|
+
Coridoc::Markdown::Link.new(
|
|
439
|
+
text: element.content.to_s,
|
|
440
|
+
url: element.target.to_s
|
|
441
|
+
)
|
|
442
|
+
when 'footnote'
|
|
443
|
+
Coridoc::Markdown::FootnoteReference.new(id: element.target.to_s)
|
|
444
|
+
when 'stem'
|
|
445
|
+
Coridoc::Markdown::Math.inline(element.content.to_s)
|
|
446
|
+
when 'highlight'
|
|
447
|
+
Coridoc::Markdown::Highlight.new(text: element.content.to_s)
|
|
448
|
+
when 'strikethrough'
|
|
449
|
+
Coridoc::Markdown::Strikethrough.new(text: element.content.to_s)
|
|
450
|
+
when 'subscript'
|
|
451
|
+
Coridoc::Markdown::Subscript.new(text: element.content.to_s)
|
|
452
|
+
when 'superscript'
|
|
453
|
+
Coridoc::Markdown::Superscript.new(text: element.content.to_s)
|
|
454
|
+
when 'underline'
|
|
455
|
+
Coridoc::Markdown::Underline.new(text: element.content.to_s)
|
|
456
|
+
when 'xref'
|
|
457
|
+
Coridoc::Markdown::CrossReference.new(
|
|
458
|
+
text: element.content.to_s,
|
|
459
|
+
target: element.target.to_s
|
|
460
|
+
)
|
|
461
|
+
when 'raw_inline'
|
|
462
|
+
CGI.escapeHTML(element.content.to_s)
|
|
463
|
+
else
|
|
464
|
+
element.content.to_s
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
def transform_definition_list(dl)
|
|
469
|
+
# flat_map because each CoreModel DefinitionItem may emit
|
|
470
|
+
# multiple Markdown DefinitionTerms (multi-term `<dt>`).
|
|
471
|
+
items = Array(dl.items).flat_map do |item|
|
|
472
|
+
definitions = [build_definition_item(item)]
|
|
473
|
+
# `+`-continuation blocks attached to the dd become
|
|
474
|
+
# additional definitions in Markdown (each renders as
|
|
475
|
+
# `: line` in the Flat serializer). One DefinitionItem
|
|
476
|
+
# per attached block, preserving their distinct content
|
|
477
|
+
# without jamming them into the primary definition's text.
|
|
478
|
+
definitions.concat(Array(item.attached_children).map { |b| build_attached_definition_item(b) })
|
|
479
|
+
nested = item.nested ? transform_definition_list(item.nested) : nil
|
|
480
|
+
term_children = Array(item.term_children).map { |c| transform_inline_content(c) }
|
|
481
|
+
# Multi-term `<dt>`: emit one Markdown DefinitionTerm per
|
|
482
|
+
# term string. Inline children map 1:1 to the FIRST term
|
|
483
|
+
# (matches the mirror handler's behaviour; multi-term
|
|
484
|
+
# with inline markup on later terms is rare and the
|
|
485
|
+
# parser doesn't capture per-term children today).
|
|
486
|
+
terms = terms_for(item)
|
|
487
|
+
if terms.empty?
|
|
488
|
+
# Defensive: term and terms both empty — emit nothing.
|
|
489
|
+
next
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
terms.each_with_index.map do |term_text, idx|
|
|
493
|
+
Coridoc::Markdown::DefinitionTerm.new(
|
|
494
|
+
text: term_text.to_s,
|
|
495
|
+
text_children: idx.zero? ? term_children : [],
|
|
496
|
+
definitions: definitions,
|
|
497
|
+
nested: nested
|
|
498
|
+
)
|
|
499
|
+
end
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
Coridoc::Markdown::DefinitionList.new(items: items)
|
|
503
|
+
end
|
|
504
|
+
|
|
505
|
+
# Source of truth for "the terms on this <dt>": the `terms`
|
|
506
|
+
# collection when populated, else `[term]` for legacy callers.
|
|
507
|
+
def terms_for(item)
|
|
508
|
+
collection = item.terms if item.is_a?(Coridoc::CoreModel::DefinitionItem)
|
|
509
|
+
return Array(collection) unless collection.nil? || collection.empty?
|
|
510
|
+
|
|
511
|
+
primary = item.term
|
|
512
|
+
primary.to_s.empty? ? [] : [primary.to_s]
|
|
513
|
+
end
|
|
514
|
+
|
|
515
|
+
def build_definition_item(item)
|
|
516
|
+
children = Array(item.definition_children)
|
|
517
|
+
if children.empty?
|
|
518
|
+
content = item.definitions&.first&.to_s
|
|
519
|
+
return Coridoc::Markdown::DefinitionItem.new(content: content)
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
inline = children.map { |c| transform_inline_content(c) }
|
|
523
|
+
Coridoc::Markdown::DefinitionItem.new(
|
|
524
|
+
content: item.definitions&.first&.to_s,
|
|
525
|
+
inline_content: inline
|
|
526
|
+
)
|
|
527
|
+
end
|
|
528
|
+
|
|
529
|
+
# Build a DefinitionItem from a single attached block (e.g.
|
|
530
|
+
# paragraph from a `+`-continuation). Flat-text extraction
|
|
531
|
+
# via ChildrenContent is the single source of truth — every
|
|
532
|
+
# attached block in CoreModel includes ChildrenContent.
|
|
533
|
+
def build_attached_definition_item(block)
|
|
534
|
+
content = if block.is_a?(Coridoc::CoreModel::ChildrenContent)
|
|
535
|
+
block.flat_text
|
|
536
|
+
else
|
|
537
|
+
block.to_s
|
|
538
|
+
end
|
|
539
|
+
Coridoc::Markdown::DefinitionItem.new(content: content)
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
def transform_footnote(fn)
|
|
543
|
+
Coridoc::Markdown::Footnote.new(
|
|
544
|
+
id: fn.id.to_s,
|
|
545
|
+
content: fn.content.to_s,
|
|
546
|
+
backlink: fn.backlink
|
|
547
|
+
)
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
def transform_footnote_reference(ref)
|
|
551
|
+
Coridoc::Markdown::FootnoteReference.new(id: ref.id.to_s)
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
def transform_abbreviation(abbr)
|
|
555
|
+
Coridoc::Markdown::Abbreviation.new(
|
|
556
|
+
term: abbr.term.to_s,
|
|
557
|
+
definition: abbr.definition.to_s
|
|
558
|
+
)
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
def transform_annotation_block(annotation)
|
|
562
|
+
transform_admonition_block(annotation, default_type: 'note')
|
|
563
|
+
end
|
|
564
|
+
|
|
565
|
+
def transform_bibliography(bib)
|
|
566
|
+
entries = Array(bib.entries).map { |e| transform(e) }
|
|
567
|
+
blocks = []
|
|
568
|
+
blocks << Coridoc::Markdown::Heading.new(level: 2, text: bib.title.to_s) if bib.title
|
|
569
|
+
blocks.concat(entries)
|
|
570
|
+
Coridoc::Markdown::Document.new(id: bib.id, blocks: blocks)
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
def transform_bibliography_entry(entry)
|
|
574
|
+
text = entry.display_text.to_s
|
|
575
|
+
text = convert_asciidoc_markers_to_markdown(text)
|
|
576
|
+
Coridoc::Markdown::Paragraph.new(text: text)
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
def convert_asciidoc_markers_to_markdown(text)
|
|
580
|
+
return '' if text.nil?
|
|
581
|
+
|
|
582
|
+
result = text.dup
|
|
583
|
+
|
|
584
|
+
# footnote:[text] -> ^[text]
|
|
585
|
+
result.gsub!(/footnote:\[(.*?)\]/, '^[\1]')
|
|
586
|
+
|
|
587
|
+
# [smallcap]#text# -> text
|
|
588
|
+
result.gsub!(/\[smallcap\]#(.*?)#/, '\1')
|
|
589
|
+
|
|
590
|
+
# *text* -> **text**
|
|
591
|
+
# Use negative lookbehind and lookahead to avoid replacing **text** to ****text****
|
|
592
|
+
result.gsub!(/(?<!\*)\*(.*?)\*(?!\*)/, '**\1**')
|
|
593
|
+
|
|
594
|
+
# _text_ -> *text*
|
|
595
|
+
# Use negative lookbehind and lookahead to avoid replacing __text__ to **text**
|
|
596
|
+
result.gsub!(/(?<!_)_(.*?)_(?!_)/, '*\1*')
|
|
597
|
+
|
|
598
|
+
result
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
def flatten_result(result)
|
|
602
|
+
case result
|
|
603
|
+
when Array
|
|
604
|
+
result.flat_map { |r| flatten_result(r) }
|
|
605
|
+
else
|
|
606
|
+
[result]
|
|
607
|
+
end
|
|
608
|
+
end
|
|
609
|
+
end
|
|
610
|
+
end
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Transform
|
|
6
|
+
module ImageTransformer
|
|
7
|
+
class << self
|
|
8
|
+
def transform_image(image)
|
|
9
|
+
Coridoc::Markdown::Image.new(
|
|
10
|
+
src: image.src,
|
|
11
|
+
alt: image.alt.to_s,
|
|
12
|
+
title: image.title
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
FromCoreModel.register(CoreModel::Image, method(:transform_image))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|