ceradoc-markdown 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/grammar/ceradoc-markdown.artifact.json +1 -0
  4. data/grammar/ceradoc-markdown.bindings.yml +13 -0
  5. data/grammar/ceradoc-markdown.parg +159 -0
  6. data/lib/ceradoc/markdown/behaviors/admonition.rb +21 -0
  7. data/lib/ceradoc/markdown/behaviors/attribute_list.rb +81 -0
  8. data/lib/ceradoc/markdown/behaviors/document.rb +34 -0
  9. data/lib/ceradoc/markdown/behaviors/extension.rb +69 -0
  10. data/lib/ceradoc/markdown/behaviors/heading.rb +33 -0
  11. data/lib/ceradoc/markdown/behaviors/math.rb +32 -0
  12. data/lib/ceradoc/markdown/behaviors/text.rb +14 -0
  13. data/lib/ceradoc/markdown/behaviors.rb +18 -0
  14. data/lib/ceradoc/markdown/bindings/captures.rb +151 -0
  15. data/lib/ceradoc/markdown/bindings/scope.rb +31 -0
  16. data/lib/ceradoc/markdown/bindings.rb +327 -0
  17. data/lib/ceradoc/markdown/errors.rb +28 -0
  18. data/lib/ceradoc/markdown/lint/registrations.rb +38 -0
  19. data/lib/ceradoc/markdown/lint.rb +16 -0
  20. data/lib/ceradoc/markdown/metanorma_inline_parser.rb +23 -0
  21. data/lib/ceradoc/markdown/metanorma_input.rb +135 -0
  22. data/lib/ceradoc/markdown/model/abbreviation.rb +27 -0
  23. data/lib/ceradoc/markdown/model/admonition.rb +33 -0
  24. data/lib/ceradoc/markdown/model/attribute_list.rb +25 -0
  25. data/lib/ceradoc/markdown/model/base.rb +54 -0
  26. data/lib/ceradoc/markdown/model/blockquote.rb +16 -0
  27. data/lib/ceradoc/markdown/model/code.rb +11 -0
  28. data/lib/ceradoc/markdown/model/code_block.rb +18 -0
  29. data/lib/ceradoc/markdown/model/comment.rb +14 -0
  30. data/lib/ceradoc/markdown/model/cross_reference.rb +10 -0
  31. data/lib/ceradoc/markdown/model/definition_item.rb +26 -0
  32. data/lib/ceradoc/markdown/model/definition_list.rb +35 -0
  33. data/lib/ceradoc/markdown/model/definition_term.rb +29 -0
  34. data/lib/ceradoc/markdown/model/document.rb +33 -0
  35. data/lib/ceradoc/markdown/model/emphasis.rb +11 -0
  36. data/lib/ceradoc/markdown/model/example_block.rb +21 -0
  37. data/lib/ceradoc/markdown/model/extension.rb +23 -0
  38. data/lib/ceradoc/markdown/model/footnote.rb +31 -0
  39. data/lib/ceradoc/markdown/model/footnote_reference.rb +17 -0
  40. data/lib/ceradoc/markdown/model/hard_line_break.rb +18 -0
  41. data/lib/ceradoc/markdown/model/heading.rb +17 -0
  42. data/lib/ceradoc/markdown/model/highlight.rb +13 -0
  43. data/lib/ceradoc/markdown/model/horizontal_rule.rb +11 -0
  44. data/lib/ceradoc/markdown/model/image.rb +19 -0
  45. data/lib/ceradoc/markdown/model/link.rb +19 -0
  46. data/lib/ceradoc/markdown/model/list.rb +22 -0
  47. data/lib/ceradoc/markdown/model/list_item.rb +14 -0
  48. data/lib/ceradoc/markdown/model/literal.rb +16 -0
  49. data/lib/ceradoc/markdown/model/math.rb +21 -0
  50. data/lib/ceradoc/markdown/model/named_value.rb +18 -0
  51. data/lib/ceradoc/markdown/model/open_block.rb +17 -0
  52. data/lib/ceradoc/markdown/model/paragraph.rb +15 -0
  53. data/lib/ceradoc/markdown/model/pass.rb +16 -0
  54. data/lib/ceradoc/markdown/model/sidebar.rb +18 -0
  55. data/lib/ceradoc/markdown/model/strikethrough.rb +13 -0
  56. data/lib/ceradoc/markdown/model/strong.rb +11 -0
  57. data/lib/ceradoc/markdown/model/subscript.rb +9 -0
  58. data/lib/ceradoc/markdown/model/superscript.rb +9 -0
  59. data/lib/ceradoc/markdown/model/table.rb +13 -0
  60. data/lib/ceradoc/markdown/model/text.rb +13 -0
  61. data/lib/ceradoc/markdown/model/underline.rb +9 -0
  62. data/lib/ceradoc/markdown/model/verse.rb +20 -0
  63. data/lib/ceradoc/markdown/parser/ast_processor.rb +565 -0
  64. data/lib/ceradoc/markdown/parser/attribute_lists.rb +48 -0
  65. data/lib/ceradoc/markdown/parser/block_parser.rb +735 -0
  66. data/lib/ceradoc/markdown/parser/frontmatter_parser.rb +23 -0
  67. data/lib/ceradoc/markdown/parser/grammar.rb +56 -0
  68. data/lib/ceradoc/markdown/parser/grammar_runtime.rb +164 -0
  69. data/lib/ceradoc/markdown/parser/html_entities.rb +2149 -0
  70. data/lib/ceradoc/markdown/parser/inline_parser.rb +314 -0
  71. data/lib/ceradoc/markdown/parser/parsanol_atoms.rb +36 -0
  72. data/lib/ceradoc/markdown/parser.rb +11 -0
  73. data/lib/ceradoc/markdown/parser_util.rb +90 -0
  74. data/lib/ceradoc/markdown/schema/definition.rb +81 -0
  75. data/lib/ceradoc/markdown/schema/loader.rb +68 -0
  76. data/lib/ceradoc/markdown/schema/parity.rb +101 -0
  77. data/lib/ceradoc/markdown/schema.rb +53 -0
  78. data/lib/ceradoc/markdown/serializer/builder.rb +57 -0
  79. data/lib/ceradoc/markdown/serializer/config.rb +71 -0
  80. data/lib/ceradoc/markdown/serializer/context.rb +66 -0
  81. data/lib/ceradoc/markdown/serializer/element_serializer.rb +46 -0
  82. data/lib/ceradoc/markdown/serializer/flavor.rb +82 -0
  83. data/lib/ceradoc/markdown/serializer/registrations.rb +113 -0
  84. data/lib/ceradoc/markdown/serializer/registry.rb +62 -0
  85. data/lib/ceradoc/markdown/serializer/runner.rb +84 -0
  86. data/lib/ceradoc/markdown/serializer/serializers/abbreviation.rb +19 -0
  87. data/lib/ceradoc/markdown/serializer/serializers/admonition.rb +20 -0
  88. data/lib/ceradoc/markdown/serializer/serializers/attribute_list.rb +25 -0
  89. data/lib/ceradoc/markdown/serializer/serializers/blockquote.rb +19 -0
  90. data/lib/ceradoc/markdown/serializer/serializers/code.rb +19 -0
  91. data/lib/ceradoc/markdown/serializer/serializers/code_block.rb +19 -0
  92. data/lib/ceradoc/markdown/serializer/serializers/comment.rb +31 -0
  93. data/lib/ceradoc/markdown/serializer/serializers/cross_reference.rb +19 -0
  94. data/lib/ceradoc/markdown/serializer/serializers/definition_list.rb +20 -0
  95. data/lib/ceradoc/markdown/serializer/serializers/document.rb +22 -0
  96. data/lib/ceradoc/markdown/serializer/serializers/emphasis.rb +19 -0
  97. data/lib/ceradoc/markdown/serializer/serializers/example_block.rb +48 -0
  98. data/lib/ceradoc/markdown/serializer/serializers/extension.rb +30 -0
  99. data/lib/ceradoc/markdown/serializer/serializers/footnote.rb +20 -0
  100. data/lib/ceradoc/markdown/serializer/serializers/footnote_reference.rb +19 -0
  101. data/lib/ceradoc/markdown/serializer/serializers/hard_line_break.rb +22 -0
  102. data/lib/ceradoc/markdown/serializer/serializers/heading.rb +19 -0
  103. data/lib/ceradoc/markdown/serializer/serializers/highlight.rb +19 -0
  104. data/lib/ceradoc/markdown/serializer/serializers/horizontal_rule.rb +19 -0
  105. data/lib/ceradoc/markdown/serializer/serializers/image.rb +19 -0
  106. data/lib/ceradoc/markdown/serializer/serializers/link.rb +29 -0
  107. data/lib/ceradoc/markdown/serializer/serializers/list.rb +43 -0
  108. data/lib/ceradoc/markdown/serializer/serializers/literal.rb +26 -0
  109. data/lib/ceradoc/markdown/serializer/serializers/math.rb +23 -0
  110. data/lib/ceradoc/markdown/serializer/serializers/open_block.rb +38 -0
  111. data/lib/ceradoc/markdown/serializer/serializers/paragraph.rb +23 -0
  112. data/lib/ceradoc/markdown/serializer/serializers/pass.rb +30 -0
  113. data/lib/ceradoc/markdown/serializer/serializers/sidebar.rb +33 -0
  114. data/lib/ceradoc/markdown/serializer/serializers/strikethrough.rb +19 -0
  115. data/lib/ceradoc/markdown/serializer/serializers/strong.rb +19 -0
  116. data/lib/ceradoc/markdown/serializer/serializers/subscript.rb +19 -0
  117. data/lib/ceradoc/markdown/serializer/serializers/superscript.rb +19 -0
  118. data/lib/ceradoc/markdown/serializer/serializers/table.rb +37 -0
  119. data/lib/ceradoc/markdown/serializer/serializers/text.rb +19 -0
  120. data/lib/ceradoc/markdown/serializer/serializers/underline.rb +19 -0
  121. data/lib/ceradoc/markdown/serializer/serializers/verse.rb +31 -0
  122. data/lib/ceradoc/markdown/serializer/strategies/admonition/base.rb +41 -0
  123. data/lib/ceradoc/markdown/serializer/strategies/admonition/container.rb +34 -0
  124. data/lib/ceradoc/markdown/serializer/strategies/admonition/gfm_alert.rb +28 -0
  125. data/lib/ceradoc/markdown/serializer/strategies/admonition/github.rb +27 -0
  126. data/lib/ceradoc/markdown/serializer/strategies/admonition/html.rb +25 -0
  127. data/lib/ceradoc/markdown/serializer/strategies/admonition/registry.rb +50 -0
  128. data/lib/ceradoc/markdown/serializer/strategies/autolink/angle.rb +35 -0
  129. data/lib/ceradoc/markdown/serializer/strategies/autolink/bare.rb +23 -0
  130. data/lib/ceradoc/markdown/serializer/strategies/autolink/base.rb +33 -0
  131. data/lib/ceradoc/markdown/serializer/strategies/autolink/none.rb +27 -0
  132. data/lib/ceradoc/markdown/serializer/strategies/autolink/registry.rb +58 -0
  133. data/lib/ceradoc/markdown/serializer/strategies/definition_list/base.rb +37 -0
  134. data/lib/ceradoc/markdown/serializer/strategies/definition_list/flat.rb +64 -0
  135. data/lib/ceradoc/markdown/serializer/strategies/definition_list/nested_html.rb +54 -0
  136. data/lib/ceradoc/markdown/serializer/strategies/definition_list/registry.rb +37 -0
  137. data/lib/ceradoc/markdown/serializer.rb +48 -0
  138. data/lib/ceradoc/markdown/toc_generator.rb +209 -0
  139. data/lib/ceradoc/markdown/transform/block_transformer.rb +163 -0
  140. data/lib/ceradoc/markdown/transform/from_core_model.rb +613 -0
  141. data/lib/ceradoc/markdown/transform/image_transformer.rb +21 -0
  142. data/lib/ceradoc/markdown/transform/inline_transformer.rb +78 -0
  143. data/lib/ceradoc/markdown/transform/list_transformer.rb +58 -0
  144. data/lib/ceradoc/markdown/transform/structural_transformer.rb +94 -0
  145. data/lib/ceradoc/markdown/transform/table_transformer.rb +40 -0
  146. data/lib/ceradoc/markdown/transform/text_extraction.rb +19 -0
  147. data/lib/ceradoc/markdown/transform/to_core_model.rb +304 -0
  148. data/lib/ceradoc/markdown/transformer.rb +48 -0
  149. data/lib/ceradoc/markdown/version.rb +7 -0
  150. data/lib/ceradoc/markdown.rb +218 -0
  151. data/schemas/blocks.yml +73 -0
  152. data/schemas/document.yml +55 -0
  153. data/schemas/inline.yml +57 -0
  154. data/schemas/lists.yml +35 -0
  155. data/schemas/tables.yml +11 -0
  156. metadata +284 -0
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Math (block or inline).
7
+ module Math
8
+ def self.prepended(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ # Constructors for the two math flavors.
13
+ module ClassMethods
14
+ # @param content [String] The math content
15
+ # @return [Ceradoc::Markdown::Math]
16
+ def inline(content)
17
+ new(content: content, inline: true)
18
+ end
19
+
20
+ def block(content)
21
+ new(content: content, inline: false)
22
+ end
23
+ end
24
+
25
+ # @return [Boolean]
26
+ def inline?
27
+ inline == true
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Markdown
5
+ module Behaviors
6
+ # Hand-written behavior for Text.
7
+ module Text
8
+ def to_s
9
+ content
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Markdown
5
+ # Hand-written behavior modules, one per model that needs methods beyond
6
+ # its attributes. Both the hand-written models and the schema-generated
7
+ # classes prepend the same module, so behavior has a single source.
8
+ module Behaviors
9
+ autoload :AttributeList, 'ceradoc/markdown/behaviors/attribute_list'
10
+ autoload :Admonition, 'ceradoc/markdown/behaviors/admonition'
11
+ autoload :Document, 'ceradoc/markdown/behaviors/document'
12
+ autoload :Extension, 'ceradoc/markdown/behaviors/extension'
13
+ autoload :Heading, 'ceradoc/markdown/behaviors/heading'
14
+ autoload :Math, 'ceradoc/markdown/behaviors/math'
15
+ autoload :Text, 'ceradoc/markdown/behaviors/text'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'strscan'
4
+
5
+ module Ceradoc
6
+ module Markdown
7
+ module Bindings
8
+ # Readers for the raw captures of the Markdown block grammar: the
9
+ # small, stateless text and attribute-list steps the binding hooks
10
+ # (and the residual Transformer) share.
11
+ module Captures
12
+ module_function
13
+
14
+ # ATX heading: level is the count of leading '#' chars.
15
+ # Setext heading: level is 1 for '=' underline, 2 for '-' underline.
16
+ def heading_level(heading)
17
+ s = heading.to_s
18
+ return s.length if s.start_with?('#')
19
+ return 1 if s.start_with?('=')
20
+ return 2 if s.start_with?('-')
21
+
22
+ s.length
23
+ end
24
+
25
+ # Normalize the various shapes a list-item node can take:
26
+ # Hash {p: ...} -> [{p: ...}]
27
+ # Hash {ul: ...} -> [{ul: ...}] (nested list, no text)
28
+ # Array [...] -> [...] (mixed paragraph + nested list nodes)
29
+ # String / Slice -> [s]
30
+ def normalize_list_item_nodes(item)
31
+ item.is_a?(Array) ? item : [item]
32
+ end
33
+
34
+ def extract_row_cells(row)
35
+ Array(row).map do |cell|
36
+ next cell.to_s.strip unless cell.is_a?(Hash)
37
+
38
+ cell[:cell].to_s.strip
39
+ end
40
+ end
41
+
42
+ # Parse IAL content string into components
43
+ # Delegates to shared IalParser for consistent parsing
44
+ def parse_ial_content(content)
45
+ ParserUtil::IalParser.parse_to_hash(content)
46
+ end
47
+
48
+ # Parse extension options string into hash
49
+ def parse_extension_options(content)
50
+ return {} if content.nil? || content.empty?
51
+
52
+ result = {}
53
+ scanner = StringScanner.new(content.strip)
54
+
55
+ until scanner.eos?
56
+ scanner.skip(/\s+/)
57
+ break if scanner.eos?
58
+
59
+ if scanner.scan(/(\w[\w-]*)\s*=\s*/)
60
+ key = scanner[1]
61
+ value = if scanner.scan(/"([^"\\]*)"/)
62
+ scanner[1]
63
+ elsif scanner.scan(/'([^'\\]*)'/)
64
+ scanner[1]
65
+ elsif scanner.scan(/(\S+)/)
66
+ scanner[1]
67
+ else
68
+ ''
69
+ end
70
+ result[key] = value
71
+ else
72
+ # Skip unrecognized character to avoid infinite loop
73
+ scanner.scan(/./)
74
+ end
75
+ end
76
+
77
+ result
78
+ end
79
+
80
+ # Extract text from paragraph structure
81
+ def extract_text_from_p(para)
82
+ case para
83
+ when Hash
84
+ para[:ln].to_s
85
+ when Array
86
+ para.map { |l| l.is_a?(Hash) ? l[:ln].to_s : l.to_s }.join("\n")
87
+ else
88
+ para.to_s
89
+ end
90
+ end
91
+
92
+ # Recursively flatten any inline subtree (arrays, nested
93
+ # strong/emph hashes, code spans, Parsanol slices) to its
94
+ # literal text. Inner style markers survive as characters so
95
+ # the round trip stays stable.
96
+ def flatten_inline_text(subtree)
97
+ case subtree
98
+ when Array
99
+ subtree.map { |n| flatten_inline_text(n) }.join
100
+ when Hash
101
+ if subtree.key?(:text)
102
+ subtree[:text].to_s
103
+ elsif subtree.key?(:code)
104
+ "`#{subtree[:code]}`"
105
+ elsif subtree.key?(:link) && subtree[:link].is_a?(Hash)
106
+ link = subtree[:link]
107
+ "[#{flatten_inline_text(link[:link_text])}](#{flatten_inline_text(link[:link_url])})"
108
+ elsif subtree.key?(:image) && subtree[:image].is_a?(Hash)
109
+ image = subtree[:image]
110
+ "![#{flatten_inline_text(image[:link_text])}](#{flatten_inline_text(image[:link_url])})"
111
+ else
112
+ subtree.values.map { |v| flatten_inline_text(v) }.join
113
+ end
114
+ else
115
+ subtree.to_s
116
+ end
117
+ end
118
+
119
+ # Extract code from code_block structure
120
+ def extract_code(code_block)
121
+ case code_block
122
+ when Array
123
+ code_block.map { |l| l.is_a?(Hash) ? l[:ln].to_s : l.to_s }.join("\n")
124
+ when Hash
125
+ code_block[:ln].to_s
126
+ else
127
+ code_block.to_s
128
+ end
129
+ end
130
+
131
+ # Extract text content from nested structures
132
+ def extract_text(content)
133
+ case content
134
+ when Array
135
+ content.map { |c| extract_text(c) }.join
136
+ when Hash
137
+ if content.key?(:text)
138
+ content[:text].to_s
139
+ elsif content.key?(:ln)
140
+ content[:ln].to_s
141
+ else
142
+ content.values.map { |v| extract_text(v) }.join
143
+ end
144
+ else
145
+ content.to_s
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Markdown
5
+ module Bindings
6
+ # Document-scoped state for the stateful rules: the Attribute List
7
+ # Definition table. One Scope per parsed document, handed to the
8
+ # scoped hooks by the BindingInflater.
9
+ class Scope
10
+ def initialize
11
+ @definitions = {}
12
+ end
13
+
14
+ # Register an ALD under its name.
15
+ def define(name, attrs)
16
+ @definitions[name] = Bindings.model('AttributeList').new(name: name, **attrs)
17
+ end
18
+
19
+ # An IAL naming a registered ALD resolves to it; anything else is
20
+ # a fresh attribute list.
21
+ def resolve(ial_content)
22
+ content = ial_content.strip
23
+ return @definitions[content] if content.match?(/\A\w+\z/) && @definitions.key?(content)
24
+
25
+ attrs = Captures.parse_ial_content(content)
26
+ Bindings.model('AttributeList').new(id: attrs[:id], classes: attrs[:classes], attributes: attrs[:attributes])
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
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
+ # Ceradoc::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/ceradoc-markdown.bindings.yml is its
21
+ # sidecar source), so the inflater needs no side file at runtime.
22
+ module Bindings
23
+ Binding = Ceradoc::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 CERADOC_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 link(attrs)
185
+ node = attrs[:link]
186
+ if node.is_a?(Hash)
187
+ model('Link').new(text: node[:link_text].to_s, url: node[:link_url].to_s)
188
+ else
189
+ { content: node.to_s }
190
+ end
191
+ end
192
+
193
+ def image(attrs)
194
+ node = attrs[:image]
195
+ if node.is_a?(Hash)
196
+ model('Image').new(alt: node[:link_text].to_s, src: node[:link_url].to_s)
197
+ else
198
+ { content: node.to_s }
199
+ end
200
+ end
201
+
202
+ def text(attrs)
203
+ { content: attrs[:text].to_s }
204
+ end
205
+
206
+ # Inline models for a paragraph's raw lines, each parsed by the
207
+ # InlineParser and inflated through the inline bindings.
208
+ def inline_models(paragraph)
209
+ lines = if paragraph.is_a?(Hash)
210
+ [paragraph[:ln]]
211
+ else
212
+ Array(paragraph).map { |l| l.is_a?(Hash) ? l[:ln] : l }
213
+ end
214
+ lines.compact.flat_map.with_index do |line, index|
215
+ models = Ceradoc::Markdown.parse_inline(line.to_s).flat_map { |node| inline_model(node) }
216
+ index.zero? ? models : [model('Text').new(content: "\n"), *models]
217
+ end
218
+ end
219
+
220
+ def inline_model(node)
221
+ Bindings.inline_inflater.inflate(node) do |unbound|
222
+ content = unbound.is_a?(Hash) ? Captures.flatten_inline_text([unbound]) : unbound.to_s
223
+ model('Text').new(content: content)
224
+ end
225
+ end
226
+ end
227
+
228
+ # One entry per rule: the model, its hook, and the raw captures the
229
+ # node carries. `optional` captures may be absent; `ial` rules may
230
+ # carry a trailing inline attribute list (bound as a second key set).
231
+ DECLARATIONS = [
232
+ { model: 'Heading', via: :heading, raw: %i[heading text], optional: %i[text], ial: true },
233
+ { model: 'CodeBlock', via: :code_block, raw: %i[code_block info], optional: %i[info], ial: true },
234
+ { model: 'Blockquote', via: :block_quote, raw: %i[block_quote], ial: true, scoped: true },
235
+ { model: 'Paragraph', via: :paragraph, raw: %i[p], ial: true },
236
+ { model: 'Paragraph', via: :line, raw: %i[ln] },
237
+ { model: 'List', via: :list, raw: %i[ul] },
238
+ { model: 'List', via: :list, raw: %i[ol] },
239
+ { model: 'DefinitionList', via: :definition_list, raw: %i[dl] },
240
+ { model: 'Table', via: :table, raw: %i[table_header table_separator table_body], optional: %i[table_body] },
241
+ { model: 'Footnote', via: :footnote, raw: %i[fn_id fn_content fn_content_continued],
242
+ optional: %i[fn_content_continued] },
243
+ { model: 'Abbreviation', via: :abbreviation, raw: %i[abbr_term abbr_def] },
244
+ { model: 'FootnoteReference', via: :footnote_reference, raw: %i[fn_ref] },
245
+ { model: 'Extension', via: :extension, raw: %i[extension] },
246
+ { model: 'Extension', via: :extension, raw: %i[ext_name ext_options ext_body], optional: %i[ext_body] },
247
+ { model: 'Math', via: :math, raw: %i[math_content] },
248
+ { model: 'Text', via: :text, raw: %i[text] },
249
+ { model: nil, via: :ald_definition, raw: %i[ald_name ial], scoped: true },
250
+ { model: nil, via: :ial_reference, raw: %i[ial], scoped: true }
251
+ ].freeze
252
+
253
+ # Inline rules, reached only through a paragraph's raw lines (the
254
+ # block parser never yields them as blocks), so they have their own
255
+ # inflater and a bare `{strong: ...}` block stays unbound.
256
+ INLINE_DECLARATIONS = [
257
+ { model: 'Strong', via: :strong, raw: %i[strong] },
258
+ { model: 'Emphasis', via: :emphasis, raw: %i[emph] },
259
+ { model: 'Code', via: :code, raw: %i[code] },
260
+ { model: 'Link', via: :link, raw: %i[link] },
261
+ { model: 'Image', via: :image, raw: %i[image] },
262
+ { model: 'Text', via: :text, raw: %i[text] }
263
+ ].freeze
264
+
265
+ class << self
266
+ # The active model class by short name (Ceradoc::Markdown::Heading).
267
+ # Hand-written by default; CERADOC_MARKDOWN_GENERATED=1 replaces the
268
+ # constants with the schema-generated classes, so bound output
269
+ # always matches what the rest of the gem dispatches on.
270
+ def model(name)
271
+ Ceradoc::Markdown.const_get(name, false)
272
+ end
273
+
274
+ # @return [Ceradoc::BindingInflater] the inflater for the bound rules
275
+ def inflater
276
+ @inflater ||= Ceradoc::BindingInflater.new(
277
+ bindings: [*table_bindings, *declared_bindings(DECLARATIONS)]
278
+ )
279
+ end
280
+
281
+ # @return [Ceradoc::BindingInflater] the inflater for inline nodes
282
+ def inline_inflater
283
+ @inline_inflater ||= Ceradoc::BindingInflater.new(bindings: declared_bindings(INLINE_DECLARATIONS))
284
+ end
285
+
286
+ private
287
+
288
+ # Rules whose attributes are plain captures and defaults (the thematic
289
+ # break) ship as a table inside the compiled artifact.
290
+ def table_bindings
291
+ Ceradoc::BindingInflater.from_table(
292
+ Parser::Grammar.model_bindings,
293
+ models: ->(name) { model(name) }
294
+ ).bindings
295
+ end
296
+
297
+ def declared_bindings(declarations)
298
+ declarations.flat_map do |declaration|
299
+ key_sets(declaration).map { |keys| declared_binding(declaration, keys) }
300
+ end
301
+ end
302
+
303
+ # Every key set the rule may arrive with: optional captures may
304
+ # be absent, an IAL may trail.
305
+ def key_sets(declaration)
306
+ optional = declaration.fetch(:optional, [])
307
+ required = declaration[:raw] - optional
308
+ sets = (0..optional.size).flat_map { |n| optional.combination(n).map { |picked| required + picked } }
309
+ declaration[:ial] ? sets.flat_map { |keys| [keys, [*keys, :ial]] } : sets
310
+ end
311
+
312
+ def declared_binding(declaration, keys)
313
+ name = declaration[:model]
314
+ Binding.new(
315
+ keys: keys,
316
+ model: name && -> { Bindings.model(name) },
317
+ captures: {},
318
+ defaults: {},
319
+ raw: keys.to_h { |key| [key, key] },
320
+ via: Hooks.method(declaration[:via]),
321
+ scoped: declaration[:scoped]
322
+ )
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ceradoc
4
+ module Markdown
5
+ module Errors
6
+ # Base error class for Markdown errors
7
+ # Inherits from Ceradoc::Error for unified error handling
8
+ class Error < Ceradoc::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 Ceradoc
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(Ceradoc::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 = Ceradoc::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 = Ceradoc::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 Ceradoc
4
+ module Markdown
5
+ # Markdown lint rules (#165 follow-up): model-driven checks over
6
+ # the Markdown model tree. Rules self-register into
7
+ # Ceradoc::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| Ceradoc::Lint.registry.register(rule) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ceradoc/markdown'
4
+
5
+ module Ceradoc
6
+ module Markdown
7
+ # Opt-in semantic inline grammar. Reuses the shipped emphasis/code parser,
8
+ # adding direct links and images that the generic inline grammar leaves text.
9
+ class MetanormaInlineParser < Parser::InlineParser
10
+ rule(:semantic_label) { (str(']').absent? >> any).repeat }
11
+ rule(:semantic_destination) { (match['\\s()'].absent? >> any).repeat(1) }
12
+ rule(:semantic_image) do
13
+ str('![').ignore >> semantic_label.as(:semantic_alt) >> str('](').ignore >>
14
+ semantic_destination.as(:semantic_src) >> str(')').ignore
15
+ end
16
+ rule(:semantic_link) do
17
+ str('[').ignore >> semantic_label.as(:semantic_label) >> str('](').ignore >>
18
+ semantic_destination.as(:semantic_target) >> str(')').ignore
19
+ end
20
+ rule(:element) { semantic_image | semantic_link | code_span | checked_delimiter_run }
21
+ end
22
+ end
23
+ end