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,307 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# PARG bindings for the Markdown block grammar: which raw-tree rules
|
|
6
|
+
# inflate directly to schema-generated model classes through
|
|
7
|
+
# Coridoc::BindingInflater. A rule is bound by its exact capture-key
|
|
8
|
+
# set. The stateful rules (an ALD definition registers in the document's
|
|
9
|
+
# Scope, a standalone IAL resolves against it) are bound too, through
|
|
10
|
+
# scoped hooks; Transformer holds no rule. This is the only stage-1 to
|
|
11
|
+
# stage-2 path; no per-rule transformer remains.
|
|
12
|
+
#
|
|
13
|
+
# Every rule except the thematic break takes its attributes from the
|
|
14
|
+
# raw subtree through a named hook (`via:`), because the attribute is
|
|
15
|
+
# computed (heading level from the marker, text joined from lines,
|
|
16
|
+
# list items grouped) rather than copied from a capture.
|
|
17
|
+
#
|
|
18
|
+
# The binding machinery proper (codegen-ed bindings, typed capture
|
|
19
|
+
# assignment) is lutaml-model#1033. The table itself ships inside the
|
|
20
|
+
# compiled artifact (grammar/coridoc-markdown.bindings.yml is its
|
|
21
|
+
# sidecar source), so the inflater needs no side file at runtime.
|
|
22
|
+
module Bindings
|
|
23
|
+
Binding = Coridoc::BindingInflater::Binding
|
|
24
|
+
|
|
25
|
+
autoload :Captures, "#{__dir__}/bindings/captures"
|
|
26
|
+
autoload :Scope, "#{__dir__}/bindings/scope"
|
|
27
|
+
|
|
28
|
+
# Semantic steps: raw captures in, model attributes out. They read the
|
|
29
|
+
# raw captures through Captures.
|
|
30
|
+
module Hooks
|
|
31
|
+
module_function
|
|
32
|
+
|
|
33
|
+
# The active model class by short name: hand-written by default,
|
|
34
|
+
# the schema-generated one under CORIDOC_MARKDOWN_GENERATED=1.
|
|
35
|
+
def model(name)
|
|
36
|
+
Bindings.model(name)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# IAL attributes, set only when present (as Transformer does).
|
|
40
|
+
def ial(attrs)
|
|
41
|
+
return {} unless attrs.key?(:ial)
|
|
42
|
+
|
|
43
|
+
parsed = Captures.parse_ial_content(attrs[:ial].to_s)
|
|
44
|
+
{ id: parsed[:id], classes: parsed[:classes], attributes: parsed[:attributes] }.compact
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def heading(attrs)
|
|
48
|
+
text = attrs.key?(:text) ? Captures.extract_text(attrs[:text]).strip : ''
|
|
49
|
+
{ level: Captures.heading_level(attrs[:heading]), text: text, **ial(attrs) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def code_block(attrs)
|
|
53
|
+
language = attrs.key?(:info) ? { language: attrs[:info].to_s.strip } : {}
|
|
54
|
+
{ **language, code: Captures.extract_code(attrs[:code_block]), **ial(attrs) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def block_quote(attrs, scope)
|
|
58
|
+
content = attrs[:block_quote]
|
|
59
|
+
nodes = content.is_a?(Array) ? content : [content]
|
|
60
|
+
text = nodes.map { |node| Transformer.transform_element(node, scope) }.compact.map do |child|
|
|
61
|
+
child.is_a?(Base) && child.class.attributes.key?(:text) ? child.text : child.to_s
|
|
62
|
+
end.join("\n")
|
|
63
|
+
{ content: text, **ial(attrs) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# An ALD definition registers in the document scope and yields no block.
|
|
67
|
+
def ald_definition(attrs, scope)
|
|
68
|
+
scope.define(attrs[:ald_name].to_s, Captures.parse_ial_content(attrs[:ial].to_s))
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# An IAL on its own line: a registered ALD's attribute list, else a fresh one.
|
|
73
|
+
def ial_reference(attrs, scope)
|
|
74
|
+
scope.resolve(attrs[:ial].to_s)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def paragraph(attrs)
|
|
78
|
+
p = attrs[:p]
|
|
79
|
+
{ text: Captures.extract_text_from_p(p), children: inline_models(p), **ial(attrs) }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# A bare line that no other rule claimed is a paragraph.
|
|
83
|
+
def line(attrs)
|
|
84
|
+
{ text: attrs[:ln].to_s }
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def list(attrs)
|
|
88
|
+
ordered = attrs.key?(:ol)
|
|
89
|
+
list_attributes(attrs[ordered ? :ol : :ul], ordered: ordered)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def list_attributes(items, ordered:)
|
|
93
|
+
items = [items] unless items.is_a?(Array)
|
|
94
|
+
{ ordered: ordered, items: items.map { |node| list_item(node.is_a?(Hash) ? node[:li] : node) } }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def list_item(item_node)
|
|
98
|
+
text_parts = []
|
|
99
|
+
children = []
|
|
100
|
+
sublist = nil
|
|
101
|
+
Captures.normalize_list_item_nodes(item_node).each do |node|
|
|
102
|
+
if !node.is_a?(Hash)
|
|
103
|
+
text_parts << node.to_s
|
|
104
|
+
elsif node.key?(:p)
|
|
105
|
+
text_parts << Captures.extract_text_from_p(node[:p])
|
|
106
|
+
children.concat(inline_models(node[:p]))
|
|
107
|
+
elsif node.key?(:ul)
|
|
108
|
+
sublist = model('List').new(**list_attributes(node[:ul], ordered: false))
|
|
109
|
+
elsif node.key?(:ol)
|
|
110
|
+
sublist = model('List').new(**list_attributes(node[:ol], ordered: true))
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
item = model('ListItem').new(text: text_parts.join(' ').strip, children: children)
|
|
114
|
+
item.sublist = sublist if sublist
|
|
115
|
+
item
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The parser yields terms and definitions as a flat sequence;
|
|
119
|
+
# each definition belongs to the term before it.
|
|
120
|
+
def definition_list(attrs)
|
|
121
|
+
entries = []
|
|
122
|
+
Array(attrs[:dl]).each do |node|
|
|
123
|
+
next unless node.is_a?(Hash)
|
|
124
|
+
|
|
125
|
+
if node.key?(:def_term)
|
|
126
|
+
entries << [Captures.extract_text(node[:def_term]).strip, []]
|
|
127
|
+
elsif node.key?(:def_content) && entries.any?
|
|
128
|
+
entries.last.last << model('DefinitionItem').new(
|
|
129
|
+
content: Captures.extract_text(node[:def_content]).strip
|
|
130
|
+
)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
{ items: entries.map { |text, definitions| model('DefinitionTerm').new(text: text, definitions: definitions) } }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def table(attrs)
|
|
137
|
+
rows = Array(attrs[:table_body]).map do |node|
|
|
138
|
+
Captures.extract_row_cells(node[:table_body_row][:row]).join(' | ')
|
|
139
|
+
end
|
|
140
|
+
{ headers: Captures.extract_row_cells(attrs[:table_header][:row]), rows: rows }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def footnote(attrs)
|
|
144
|
+
lines = [attrs[:fn_content]]
|
|
145
|
+
lines += Array(attrs[:fn_content_continued]) if attrs[:fn_content_continued]
|
|
146
|
+
content = lines.map { |line| Captures.extract_text(line) }.join("\n")
|
|
147
|
+
{ id: attrs[:fn_id].to_s, content: content.strip }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def abbreviation(attrs)
|
|
151
|
+
{ term: attrs[:abbr_term].to_s, definition: attrs[:abbr_def].to_s.strip }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def footnote_reference(attrs)
|
|
155
|
+
{ id: attrs[:fn_ref].to_s }
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# `{extension: {ext_*}}` wraps the same captures the bare form has.
|
|
159
|
+
def extension(attrs)
|
|
160
|
+
node = attrs[:extension] || attrs
|
|
161
|
+
opts = node[:ext_options]
|
|
162
|
+
empty = !opts || (opts.is_a?(Array) && opts.empty?)
|
|
163
|
+
{ name: node[:ext_name].to_s,
|
|
164
|
+
options: empty ? {} : Captures.parse_extension_options(opts.to_s),
|
|
165
|
+
content: node[:ext_body]&.to_s }
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def math(attrs)
|
|
169
|
+
{ content: Captures.extract_text(attrs[:math_content]), inline: false }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def strong(attrs)
|
|
173
|
+
{ text: Captures.flatten_inline_text(attrs[:strong]) }
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def emphasis(attrs)
|
|
177
|
+
{ text: Captures.flatten_inline_text(attrs[:emph]) }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def code(attrs)
|
|
181
|
+
{ text: attrs[:code].to_s }
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def text(attrs)
|
|
185
|
+
{ content: attrs[:text].to_s }
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Inline models for a paragraph's raw lines, each parsed by the
|
|
189
|
+
# InlineParser and inflated through the inline bindings.
|
|
190
|
+
def inline_models(paragraph)
|
|
191
|
+
lines = if paragraph.is_a?(Hash)
|
|
192
|
+
[paragraph[:ln]]
|
|
193
|
+
else
|
|
194
|
+
Array(paragraph).map { |l| l.is_a?(Hash) ? l[:ln] : l }
|
|
195
|
+
end
|
|
196
|
+
lines.compact.flat_map.with_index do |line, index|
|
|
197
|
+
models = Coridoc::Markdown.parse_inline(line.to_s).flat_map { |node| inline_model(node) }
|
|
198
|
+
index.zero? ? models : [model('Text').new(content: "\n"), *models]
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def inline_model(node)
|
|
203
|
+
Bindings.inline_inflater.inflate(node) do |unbound|
|
|
204
|
+
content = unbound.is_a?(Hash) ? Captures.flatten_inline_text([unbound]) : unbound.to_s
|
|
205
|
+
model('Text').new(content: content)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# One entry per rule: the model, its hook, and the raw captures the
|
|
211
|
+
# node carries. `optional` captures may be absent; `ial` rules may
|
|
212
|
+
# carry a trailing inline attribute list (bound as a second key set).
|
|
213
|
+
DECLARATIONS = [
|
|
214
|
+
{ model: 'Heading', via: :heading, raw: %i[heading text], optional: %i[text], ial: true },
|
|
215
|
+
{ model: 'CodeBlock', via: :code_block, raw: %i[code_block info], optional: %i[info], ial: true },
|
|
216
|
+
{ model: 'Blockquote', via: :block_quote, raw: %i[block_quote], ial: true, scoped: true },
|
|
217
|
+
{ model: 'Paragraph', via: :paragraph, raw: %i[p], ial: true },
|
|
218
|
+
{ model: 'Paragraph', via: :line, raw: %i[ln] },
|
|
219
|
+
{ model: 'List', via: :list, raw: %i[ul] },
|
|
220
|
+
{ model: 'List', via: :list, raw: %i[ol] },
|
|
221
|
+
{ model: 'DefinitionList', via: :definition_list, raw: %i[dl] },
|
|
222
|
+
{ model: 'Table', via: :table, raw: %i[table_header table_separator table_body], optional: %i[table_body] },
|
|
223
|
+
{ model: 'Footnote', via: :footnote, raw: %i[fn_id fn_content fn_content_continued],
|
|
224
|
+
optional: %i[fn_content_continued] },
|
|
225
|
+
{ model: 'Abbreviation', via: :abbreviation, raw: %i[abbr_term abbr_def] },
|
|
226
|
+
{ model: 'FootnoteReference', via: :footnote_reference, raw: %i[fn_ref] },
|
|
227
|
+
{ model: 'Extension', via: :extension, raw: %i[extension] },
|
|
228
|
+
{ model: 'Extension', via: :extension, raw: %i[ext_name ext_options ext_body], optional: %i[ext_body] },
|
|
229
|
+
{ model: 'Math', via: :math, raw: %i[math_content] },
|
|
230
|
+
{ model: 'Text', via: :text, raw: %i[text] },
|
|
231
|
+
{ model: nil, via: :ald_definition, raw: %i[ald_name ial], scoped: true },
|
|
232
|
+
{ model: nil, via: :ial_reference, raw: %i[ial], scoped: true }
|
|
233
|
+
].freeze
|
|
234
|
+
|
|
235
|
+
# Inline rules, reached only through a paragraph's raw lines (the
|
|
236
|
+
# block parser never yields them as blocks), so they have their own
|
|
237
|
+
# inflater and a bare `{strong: ...}` block stays unbound.
|
|
238
|
+
INLINE_DECLARATIONS = [
|
|
239
|
+
{ model: 'Strong', via: :strong, raw: %i[strong] },
|
|
240
|
+
{ model: 'Emphasis', via: :emphasis, raw: %i[emph] },
|
|
241
|
+
{ model: 'Code', via: :code, raw: %i[code] },
|
|
242
|
+
{ model: 'Text', via: :text, raw: %i[text] }
|
|
243
|
+
].freeze
|
|
244
|
+
|
|
245
|
+
class << self
|
|
246
|
+
# The active model class by short name (Coridoc::Markdown::Heading).
|
|
247
|
+
# Hand-written by default; CORIDOC_MARKDOWN_GENERATED=1 replaces the
|
|
248
|
+
# constants with the schema-generated classes, so bound output
|
|
249
|
+
# always matches what the rest of the gem dispatches on.
|
|
250
|
+
def model(name)
|
|
251
|
+
Coridoc::Markdown.const_get(name, false)
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# @return [Coridoc::BindingInflater] the inflater for the bound rules
|
|
255
|
+
def inflater
|
|
256
|
+
@inflater ||= Coridoc::BindingInflater.new(
|
|
257
|
+
bindings: [*table_bindings, *declared_bindings(DECLARATIONS)]
|
|
258
|
+
)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# @return [Coridoc::BindingInflater] the inflater for inline nodes
|
|
262
|
+
def inline_inflater
|
|
263
|
+
@inline_inflater ||= Coridoc::BindingInflater.new(bindings: declared_bindings(INLINE_DECLARATIONS))
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
private
|
|
267
|
+
|
|
268
|
+
# Rules whose attributes are plain captures and defaults (the thematic
|
|
269
|
+
# break) ship as a table inside the compiled artifact.
|
|
270
|
+
def table_bindings
|
|
271
|
+
Coridoc::BindingInflater.from_table(
|
|
272
|
+
Parser::Grammar.model_bindings,
|
|
273
|
+
models: ->(name) { model(name) }
|
|
274
|
+
).bindings
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def declared_bindings(declarations)
|
|
278
|
+
declarations.flat_map do |declaration|
|
|
279
|
+
key_sets(declaration).map { |keys| declared_binding(declaration, keys) }
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Every key set the rule may arrive with: optional captures may
|
|
284
|
+
# be absent, an IAL may trail.
|
|
285
|
+
def key_sets(declaration)
|
|
286
|
+
optional = declaration.fetch(:optional, [])
|
|
287
|
+
required = declaration[:raw] - optional
|
|
288
|
+
sets = (0..optional.size).flat_map { |n| optional.combination(n).map { |picked| required + picked } }
|
|
289
|
+
declaration[:ial] ? sets.flat_map { |keys| [keys, [*keys, :ial]] } : sets
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def declared_binding(declaration, keys)
|
|
293
|
+
name = declaration[:model]
|
|
294
|
+
Binding.new(
|
|
295
|
+
keys: keys,
|
|
296
|
+
model: name && -> { Bindings.model(name) },
|
|
297
|
+
captures: {},
|
|
298
|
+
defaults: {},
|
|
299
|
+
raw: keys.to_h { |key| [key, key] },
|
|
300
|
+
via: Hooks.method(declaration[:via]),
|
|
301
|
+
scoped: declaration[:scoped]
|
|
302
|
+
)
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
end
|
|
307
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Errors
|
|
6
|
+
# Base error class for Markdown errors
|
|
7
|
+
# Inherits from Coridoc::Error for unified error handling
|
|
8
|
+
class Error < Coridoc::Error
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Raised when Markdown parsing fails
|
|
12
|
+
class ParseError < Error
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Raised when Markdown serialization fails
|
|
16
|
+
class SerializationError < Error
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Raised when an unsupported Markdown feature is encountered
|
|
20
|
+
class UnsupportedFeatureError < Error
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Raised when Markdown-to-CoreModel transformation fails
|
|
24
|
+
class TransformationError < Error
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
module Lint
|
|
6
|
+
# Markdown bindings for the shared invariant templates (#309).
|
|
7
|
+
# Headings are top-level blocks in the Markdown model.
|
|
8
|
+
module Registrations
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def headings(document)
|
|
12
|
+
Array(document.blocks).grep(Coridoc::Markdown::Heading)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# MD001 — a heading may be at most one level deeper than the
|
|
16
|
+
# previous heading (markdownlint MD001).
|
|
17
|
+
HEADING_LEVELS = Coridoc::Lint::Template.bind(
|
|
18
|
+
:monotonic, id: 'MD001', format: :markdown, order: :sequence,
|
|
19
|
+
roots: method(:headings).to_proc,
|
|
20
|
+
level: :level.to_proc,
|
|
21
|
+
label: :text.to_proc
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# MD024 — headings at the same level must have unique text.
|
|
25
|
+
DUPLICATE_HEADINGS = Coridoc::Lint::Template.bind(
|
|
26
|
+
:unique, id: 'MD024', format: :markdown,
|
|
27
|
+
roots: method(:headings).to_proc,
|
|
28
|
+
key: ->(heading) { [heading.level, heading.text] },
|
|
29
|
+
message: lambda { |(level, text), count|
|
|
30
|
+
"duplicate heading (level #{level}): #{text.inspect} used #{count} times"
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
RULES = [HEADING_LEVELS, DUPLICATE_HEADINGS].freeze
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Markdown lint rules (#165 follow-up): model-driven checks over
|
|
6
|
+
# the Markdown model tree. Rules self-register into
|
|
7
|
+
# Coridoc::Lint.registry when this file is required.
|
|
8
|
+
module Lint
|
|
9
|
+
require_relative 'lint/registrations'
|
|
10
|
+
|
|
11
|
+
RULES = Registrations::RULES
|
|
12
|
+
|
|
13
|
+
RULES.each { |rule| Coridoc::Lint.registry.register(rule) }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Abbreviation model representing an abbreviation definition.
|
|
6
|
+
#
|
|
7
|
+
# Kramdown syntax:
|
|
8
|
+
# `*[ABC]: A Big Corporation`
|
|
9
|
+
#
|
|
10
|
+
# When the abbreviation appears in the text, it will be wrapped
|
|
11
|
+
# with an <abbr> tag with the definition as the title.
|
|
12
|
+
#
|
|
13
|
+
# @example Abbreviation definition
|
|
14
|
+
# abbr = Coridoc::Markdown::Abbreviation.new(
|
|
15
|
+
# term: "ABC",
|
|
16
|
+
# definition: "A Big Corporation"
|
|
17
|
+
# )
|
|
18
|
+
#
|
|
19
|
+
class Abbreviation < Base
|
|
20
|
+
# The abbreviation term
|
|
21
|
+
attribute :term, :string
|
|
22
|
+
|
|
23
|
+
# The full definition/explanation
|
|
24
|
+
attribute :definition, :string
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'base'
|
|
4
|
+
|
|
5
|
+
module Coridoc
|
|
6
|
+
module Markdown
|
|
7
|
+
# Admonition block — a callout like NOTE, WARNING, TIP, etc.
|
|
8
|
+
#
|
|
9
|
+
# Markdown has no native admonition syntax (GFM Alerts since Dec 2023
|
|
10
|
+
# notwithstanding — see admonition/gfm_alert strategy). The
|
|
11
|
+
# `admonition_style` config option selects the output form:
|
|
12
|
+
#
|
|
13
|
+
# :github → > **NOTE:** text (broad compat)
|
|
14
|
+
# :gfm_alert → > [!NOTE]\n> text (GFM native since 2024)
|
|
15
|
+
# :container → :::note\n... \n::: (VitePress / markdown-it)
|
|
16
|
+
# :html → <div class="note">...</div>
|
|
17
|
+
#
|
|
18
|
+
# Type is stored lowercase. Content is raw Markdown text (already
|
|
19
|
+
# serialized). Title is optional.
|
|
20
|
+
class Admonition < Base
|
|
21
|
+
attribute :admonition_type, :string
|
|
22
|
+
attribute :content, :string
|
|
23
|
+
attribute :title, :string
|
|
24
|
+
|
|
25
|
+
# Mixed inline content (strings and inline model objects) carried
|
|
26
|
+
# from the CoreModel children so serializers can preserve cross
|
|
27
|
+
# references, code spans, etc. When empty, fall back to `content`.
|
|
28
|
+
attribute :children, Coridoc::Markdown::Base, collection: true, default: []
|
|
29
|
+
|
|
30
|
+
prepend Behaviors::Admonition
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Represents an Inline Attribute List (IAL) or Attribute List Definition (ALD)
|
|
6
|
+
#
|
|
7
|
+
# IAL syntax: {:.class #id key="value"}
|
|
8
|
+
# ALD syntax: {:name: #id .class key="value"}
|
|
9
|
+
#
|
|
10
|
+
# Examples:
|
|
11
|
+
# {:.highlight} - adds class "highlight"
|
|
12
|
+
# {#introduction} - sets id to "introduction"
|
|
13
|
+
# {:key="value"} - sets attribute key="value"
|
|
14
|
+
# {:name: #id .class} - defines ALD named "name"
|
|
15
|
+
#
|
|
16
|
+
class AttributeList < Base
|
|
17
|
+
attribute :id, :string
|
|
18
|
+
attribute :classes, :string, collection: true, default: []
|
|
19
|
+
attribute :attributes, NamedValue, collection: true, default: []
|
|
20
|
+
attribute :name, :string # For ALD - the reference name
|
|
21
|
+
|
|
22
|
+
prepend Behaviors::AttributeList
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Base class for all Markdown model objects.
|
|
6
|
+
#
|
|
7
|
+
# The Base class provides common functionality for all document model elements,
|
|
8
|
+
# including serialization support and tree traversal.
|
|
9
|
+
#
|
|
10
|
+
class Base < Lutaml::Model::Serializable
|
|
11
|
+
attribute :id, :string
|
|
12
|
+
|
|
13
|
+
# Attribute list for IAL (Inline Attribute List) support
|
|
14
|
+
# This allows attaching classes, id, and other attributes to elements
|
|
15
|
+
# Example: {:.highlight #intro data-role="main"}
|
|
16
|
+
attribute :attribute_list, :string
|
|
17
|
+
|
|
18
|
+
# Classes from IAL (for convenience)
|
|
19
|
+
attribute :classes, :string, collection: true
|
|
20
|
+
|
|
21
|
+
# Additional attributes from IAL (typed key-value pairs)
|
|
22
|
+
attribute :attributes, NamedValue, collection: true, default: []
|
|
23
|
+
|
|
24
|
+
# Visit pattern for traversing the document tree
|
|
25
|
+
def self.visit(element, &block)
|
|
26
|
+
return element if element.nil?
|
|
27
|
+
|
|
28
|
+
element = yield element, :pre
|
|
29
|
+
element = case element
|
|
30
|
+
when self
|
|
31
|
+
element.visit(&block)
|
|
32
|
+
when Array
|
|
33
|
+
element.map { |child| visit(child, &block) }.flatten.compact
|
|
34
|
+
when Hash
|
|
35
|
+
result = {}
|
|
36
|
+
element.each { |k, v| result[k] = visit(v, &block) }
|
|
37
|
+
result
|
|
38
|
+
else
|
|
39
|
+
element
|
|
40
|
+
end
|
|
41
|
+
yield element, :post
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def visit(&block)
|
|
45
|
+
self.class.attributes.each_key do |attr_name|
|
|
46
|
+
child = public_send(attr_name)
|
|
47
|
+
result = self.class.visit(child, &block)
|
|
48
|
+
public_send(:"#{attr_name}=", result) if result != child
|
|
49
|
+
end
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# Blockquote model representing a Markdown blockquote (> prefix).
|
|
6
|
+
#
|
|
7
|
+
# @example Create a blockquote
|
|
8
|
+
# quote = Coridoc::Markdown::Blockquote.new(
|
|
9
|
+
# content: "This is a quoted text."
|
|
10
|
+
# )
|
|
11
|
+
#
|
|
12
|
+
class Blockquote < Base
|
|
13
|
+
attribute :content, :string
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# CodeBlock model representing a fenced code block.
|
|
6
|
+
#
|
|
7
|
+
# @example Create a code block
|
|
8
|
+
# code = Coridoc::Markdown::CodeBlock.new(
|
|
9
|
+
# language: "ruby",
|
|
10
|
+
# code: "puts 'Hello World'"
|
|
11
|
+
# )
|
|
12
|
+
#
|
|
13
|
+
class CodeBlock < Base
|
|
14
|
+
attribute :language, :string
|
|
15
|
+
attribute :code, :string
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# HTML comment in Markdown source (`<!-- text -->`).
|
|
6
|
+
#
|
|
7
|
+
# Markdown has no native comment syntax; HTML comments are the conventional
|
|
8
|
+
# mechanism for non-rendered authoring notes. Both AsciiDoc single-line
|
|
9
|
+
# (`//`) and block (`//// ... ////`) comments collapse to this type.
|
|
10
|
+
class Comment < Base
|
|
11
|
+
attribute :text, :string
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# DefinitionItem model representing a single definition in a definition list.
|
|
6
|
+
#
|
|
7
|
+
# Each definition item contains the definition content and can have
|
|
8
|
+
# nested blocks (paragraphs, code blocks, lists, etc.)
|
|
9
|
+
#
|
|
10
|
+
# @example Simple definition
|
|
11
|
+
# defn = Coridoc::Markdown::DefinitionItem.new(content: "A Markdown parser")
|
|
12
|
+
#
|
|
13
|
+
class DefinitionItem < Base
|
|
14
|
+
# The definition content (text or nested blocks)
|
|
15
|
+
attribute :content, :string
|
|
16
|
+
|
|
17
|
+
# Inline content as typed Markdown elements (Code, Strong, Text, etc.).
|
|
18
|
+
# When present, the Flat serializer renders these via
|
|
19
|
+
# `ctx.serialize_inline_join` so inline formatting is preserved.
|
|
20
|
+
attribute :inline_content, Coridoc::Markdown::Base, collection: true, default: []
|
|
21
|
+
|
|
22
|
+
# Nested block content (paragraphs, code blocks, lists, etc.)
|
|
23
|
+
attribute :blocks, :string, collection: true
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Coridoc
|
|
4
|
+
module Markdown
|
|
5
|
+
# DefinitionList model representing a Kramdown definition list.
|
|
6
|
+
#
|
|
7
|
+
# Definition lists consist of terms followed by one or more definitions.
|
|
8
|
+
# The syntax uses `:` to start a definition.
|
|
9
|
+
#
|
|
10
|
+
# @example Simple definition list
|
|
11
|
+
# list = Coridoc::Markdown::DefinitionList.new(
|
|
12
|
+
# items: [
|
|
13
|
+
# Coridoc::Markdown::DefinitionTerm.new(
|
|
14
|
+
# text: "kramdown",
|
|
15
|
+
# definitions: [
|
|
16
|
+
# Coridoc::Markdown::DefinitionItem.new(content: "A Markdown parser")
|
|
17
|
+
# ]
|
|
18
|
+
# )
|
|
19
|
+
# ]
|
|
20
|
+
# )
|
|
21
|
+
#
|
|
22
|
+
# Syntax:
|
|
23
|
+
# term
|
|
24
|
+
# : definition content
|
|
25
|
+
#
|
|
26
|
+
# multiple terms
|
|
27
|
+
# : first definition
|
|
28
|
+
# : second definition
|
|
29
|
+
#
|
|
30
|
+
class DefinitionList < Base
|
|
31
|
+
# Terms with their definitions
|
|
32
|
+
attribute :items, Coridoc::Markdown::DefinitionTerm, collection: true
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|