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.
Files changed (151) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/lib/coridoc/markdown/behaviors/admonition.rb +21 -0
  4. data/lib/coridoc/markdown/behaviors/attribute_list.rb +81 -0
  5. data/lib/coridoc/markdown/behaviors/document.rb +34 -0
  6. data/lib/coridoc/markdown/behaviors/extension.rb +69 -0
  7. data/lib/coridoc/markdown/behaviors/heading.rb +33 -0
  8. data/lib/coridoc/markdown/behaviors/math.rb +32 -0
  9. data/lib/coridoc/markdown/behaviors/text.rb +14 -0
  10. data/lib/coridoc/markdown/behaviors.rb +18 -0
  11. data/lib/coridoc/markdown/bindings/captures.rb +145 -0
  12. data/lib/coridoc/markdown/bindings/scope.rb +31 -0
  13. data/lib/coridoc/markdown/bindings.rb +307 -0
  14. data/lib/coridoc/markdown/errors.rb +28 -0
  15. data/lib/coridoc/markdown/lint/registrations.rb +38 -0
  16. data/lib/coridoc/markdown/lint.rb +16 -0
  17. data/lib/coridoc/markdown/model/abbreviation.rb +27 -0
  18. data/lib/coridoc/markdown/model/admonition.rb +33 -0
  19. data/lib/coridoc/markdown/model/attribute_list.rb +25 -0
  20. data/lib/coridoc/markdown/model/base.rb +54 -0
  21. data/lib/coridoc/markdown/model/blockquote.rb +16 -0
  22. data/lib/coridoc/markdown/model/code.rb +11 -0
  23. data/lib/coridoc/markdown/model/code_block.rb +18 -0
  24. data/lib/coridoc/markdown/model/comment.rb +14 -0
  25. data/lib/coridoc/markdown/model/cross_reference.rb +10 -0
  26. data/lib/coridoc/markdown/model/definition_item.rb +26 -0
  27. data/lib/coridoc/markdown/model/definition_list.rb +35 -0
  28. data/lib/coridoc/markdown/model/definition_term.rb +29 -0
  29. data/lib/coridoc/markdown/model/document.rb +33 -0
  30. data/lib/coridoc/markdown/model/emphasis.rb +11 -0
  31. data/lib/coridoc/markdown/model/example_block.rb +21 -0
  32. data/lib/coridoc/markdown/model/extension.rb +23 -0
  33. data/lib/coridoc/markdown/model/footnote.rb +31 -0
  34. data/lib/coridoc/markdown/model/footnote_reference.rb +17 -0
  35. data/lib/coridoc/markdown/model/hard_line_break.rb +18 -0
  36. data/lib/coridoc/markdown/model/heading.rb +17 -0
  37. data/lib/coridoc/markdown/model/highlight.rb +13 -0
  38. data/lib/coridoc/markdown/model/horizontal_rule.rb +11 -0
  39. data/lib/coridoc/markdown/model/image.rb +19 -0
  40. data/lib/coridoc/markdown/model/link.rb +19 -0
  41. data/lib/coridoc/markdown/model/list.rb +22 -0
  42. data/lib/coridoc/markdown/model/list_item.rb +14 -0
  43. data/lib/coridoc/markdown/model/literal.rb +16 -0
  44. data/lib/coridoc/markdown/model/math.rb +21 -0
  45. data/lib/coridoc/markdown/model/named_value.rb +18 -0
  46. data/lib/coridoc/markdown/model/open_block.rb +17 -0
  47. data/lib/coridoc/markdown/model/paragraph.rb +15 -0
  48. data/lib/coridoc/markdown/model/pass.rb +16 -0
  49. data/lib/coridoc/markdown/model/sidebar.rb +18 -0
  50. data/lib/coridoc/markdown/model/strikethrough.rb +13 -0
  51. data/lib/coridoc/markdown/model/strong.rb +11 -0
  52. data/lib/coridoc/markdown/model/subscript.rb +9 -0
  53. data/lib/coridoc/markdown/model/superscript.rb +9 -0
  54. data/lib/coridoc/markdown/model/table.rb +13 -0
  55. data/lib/coridoc/markdown/model/text.rb +13 -0
  56. data/lib/coridoc/markdown/model/underline.rb +9 -0
  57. data/lib/coridoc/markdown/model/verse.rb +20 -0
  58. data/lib/coridoc/markdown/parser/ast_processor.rb +541 -0
  59. data/lib/coridoc/markdown/parser/attribute_lists.rb +48 -0
  60. data/lib/coridoc/markdown/parser/block_parser.rb +735 -0
  61. data/lib/coridoc/markdown/parser/frontmatter_parser.rb +23 -0
  62. data/lib/coridoc/markdown/parser/grammar.rb +56 -0
  63. data/lib/coridoc/markdown/parser/grammar_runtime.rb +164 -0
  64. data/lib/coridoc/markdown/parser/html_entities.rb +2149 -0
  65. data/lib/coridoc/markdown/parser/inline_parser.rb +296 -0
  66. data/lib/coridoc/markdown/parser/parsanol_atoms.rb +36 -0
  67. data/lib/coridoc/markdown/parser.rb +11 -0
  68. data/lib/coridoc/markdown/parser_util.rb +90 -0
  69. data/lib/coridoc/markdown/schema/definition.rb +81 -0
  70. data/lib/coridoc/markdown/schema/loader.rb +68 -0
  71. data/lib/coridoc/markdown/schema/parity.rb +101 -0
  72. data/lib/coridoc/markdown/schema.rb +53 -0
  73. data/lib/coridoc/markdown/serializer/builder.rb +57 -0
  74. data/lib/coridoc/markdown/serializer/config.rb +71 -0
  75. data/lib/coridoc/markdown/serializer/context.rb +66 -0
  76. data/lib/coridoc/markdown/serializer/element_serializer.rb +46 -0
  77. data/lib/coridoc/markdown/serializer/flavor.rb +82 -0
  78. data/lib/coridoc/markdown/serializer/registrations.rb +113 -0
  79. data/lib/coridoc/markdown/serializer/registry.rb +62 -0
  80. data/lib/coridoc/markdown/serializer/runner.rb +84 -0
  81. data/lib/coridoc/markdown/serializer/serializers/abbreviation.rb +19 -0
  82. data/lib/coridoc/markdown/serializer/serializers/admonition.rb +20 -0
  83. data/lib/coridoc/markdown/serializer/serializers/attribute_list.rb +25 -0
  84. data/lib/coridoc/markdown/serializer/serializers/blockquote.rb +19 -0
  85. data/lib/coridoc/markdown/serializer/serializers/code.rb +19 -0
  86. data/lib/coridoc/markdown/serializer/serializers/code_block.rb +19 -0
  87. data/lib/coridoc/markdown/serializer/serializers/comment.rb +31 -0
  88. data/lib/coridoc/markdown/serializer/serializers/cross_reference.rb +19 -0
  89. data/lib/coridoc/markdown/serializer/serializers/definition_list.rb +20 -0
  90. data/lib/coridoc/markdown/serializer/serializers/document.rb +22 -0
  91. data/lib/coridoc/markdown/serializer/serializers/emphasis.rb +19 -0
  92. data/lib/coridoc/markdown/serializer/serializers/example_block.rb +48 -0
  93. data/lib/coridoc/markdown/serializer/serializers/extension.rb +30 -0
  94. data/lib/coridoc/markdown/serializer/serializers/footnote.rb +20 -0
  95. data/lib/coridoc/markdown/serializer/serializers/footnote_reference.rb +19 -0
  96. data/lib/coridoc/markdown/serializer/serializers/hard_line_break.rb +22 -0
  97. data/lib/coridoc/markdown/serializer/serializers/heading.rb +19 -0
  98. data/lib/coridoc/markdown/serializer/serializers/highlight.rb +19 -0
  99. data/lib/coridoc/markdown/serializer/serializers/horizontal_rule.rb +19 -0
  100. data/lib/coridoc/markdown/serializer/serializers/image.rb +19 -0
  101. data/lib/coridoc/markdown/serializer/serializers/link.rb +29 -0
  102. data/lib/coridoc/markdown/serializer/serializers/list.rb +43 -0
  103. data/lib/coridoc/markdown/serializer/serializers/literal.rb +26 -0
  104. data/lib/coridoc/markdown/serializer/serializers/math.rb +23 -0
  105. data/lib/coridoc/markdown/serializer/serializers/open_block.rb +38 -0
  106. data/lib/coridoc/markdown/serializer/serializers/paragraph.rb +23 -0
  107. data/lib/coridoc/markdown/serializer/serializers/pass.rb +30 -0
  108. data/lib/coridoc/markdown/serializer/serializers/sidebar.rb +33 -0
  109. data/lib/coridoc/markdown/serializer/serializers/strikethrough.rb +19 -0
  110. data/lib/coridoc/markdown/serializer/serializers/strong.rb +19 -0
  111. data/lib/coridoc/markdown/serializer/serializers/subscript.rb +19 -0
  112. data/lib/coridoc/markdown/serializer/serializers/superscript.rb +19 -0
  113. data/lib/coridoc/markdown/serializer/serializers/table.rb +37 -0
  114. data/lib/coridoc/markdown/serializer/serializers/text.rb +19 -0
  115. data/lib/coridoc/markdown/serializer/serializers/underline.rb +19 -0
  116. data/lib/coridoc/markdown/serializer/serializers/verse.rb +31 -0
  117. data/lib/coridoc/markdown/serializer/strategies/admonition/base.rb +41 -0
  118. data/lib/coridoc/markdown/serializer/strategies/admonition/container.rb +34 -0
  119. data/lib/coridoc/markdown/serializer/strategies/admonition/gfm_alert.rb +28 -0
  120. data/lib/coridoc/markdown/serializer/strategies/admonition/github.rb +27 -0
  121. data/lib/coridoc/markdown/serializer/strategies/admonition/html.rb +25 -0
  122. data/lib/coridoc/markdown/serializer/strategies/admonition/registry.rb +50 -0
  123. data/lib/coridoc/markdown/serializer/strategies/autolink/angle.rb +35 -0
  124. data/lib/coridoc/markdown/serializer/strategies/autolink/bare.rb +23 -0
  125. data/lib/coridoc/markdown/serializer/strategies/autolink/base.rb +33 -0
  126. data/lib/coridoc/markdown/serializer/strategies/autolink/none.rb +27 -0
  127. data/lib/coridoc/markdown/serializer/strategies/autolink/registry.rb +58 -0
  128. data/lib/coridoc/markdown/serializer/strategies/definition_list/base.rb +37 -0
  129. data/lib/coridoc/markdown/serializer/strategies/definition_list/flat.rb +64 -0
  130. data/lib/coridoc/markdown/serializer/strategies/definition_list/nested_html.rb +54 -0
  131. data/lib/coridoc/markdown/serializer/strategies/definition_list/registry.rb +37 -0
  132. data/lib/coridoc/markdown/serializer.rb +48 -0
  133. data/lib/coridoc/markdown/toc_generator.rb +209 -0
  134. data/lib/coridoc/markdown/transform/block_transformer.rb +163 -0
  135. data/lib/coridoc/markdown/transform/from_core_model.rb +613 -0
  136. data/lib/coridoc/markdown/transform/image_transformer.rb +21 -0
  137. data/lib/coridoc/markdown/transform/inline_transformer.rb +78 -0
  138. data/lib/coridoc/markdown/transform/list_transformer.rb +58 -0
  139. data/lib/coridoc/markdown/transform/structural_transformer.rb +94 -0
  140. data/lib/coridoc/markdown/transform/table_transformer.rb +40 -0
  141. data/lib/coridoc/markdown/transform/text_extraction.rb +19 -0
  142. data/lib/coridoc/markdown/transform/to_core_model.rb +304 -0
  143. data/lib/coridoc/markdown/transformer.rb +48 -0
  144. data/lib/coridoc/markdown/version.rb +7 -0
  145. data/lib/coridoc/markdown.rb +218 -0
  146. data/schemas/blocks.yml +73 -0
  147. data/schemas/document.yml +55 -0
  148. data/schemas/inline.yml +57 -0
  149. data/schemas/lists.yml +35 -0
  150. data/schemas/tables.yml +11 -0
  151. metadata +279 -0
@@ -0,0 +1,735 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coridoc
4
+ module Markdown
5
+ module Parser
6
+ # Load-time DSL side effect (sanctioned require_relative exception)
7
+ require_relative 'parsanol_atoms'
8
+
9
+ class BlockParser < Parsanol::Parser
10
+ include AttributeLists
11
+ # Native engine is the default surface for the block grammar.
12
+ # Full tree parity incl. scope/continuation and lazy
13
+ # continuation (engine_differential_spec, all shapes always-on;
14
+ # Atoms::Constant 1.3.75, rs#160 bridge writeback 1.3.76,
15
+ # rs#199 map-key capture iteration 1.3.78, and the rs#201
16
+ # GC-marked host-registry fix in 1.3.79 that ended the linux
17
+ # segfault in scope-shape replay — the gemspec floor).
18
+ # InlineParser parses native-when-expressible (its grammar is
19
+ # wire-expressible since the #163 restructure).
20
+ def parse(input, mode_or_opts = {}, **kwargs)
21
+ opts = mode_or_opts.is_a?(Hash) ? mode_or_opts.merge(kwargs) : {}
22
+ super(input, **opts, mode: opts.fetch(:mode, :native))
23
+ end
24
+
25
+ # NOTE: Debug method for parser development. Outputs current parse position
26
+ # and capture context. Only called during parser debugging sessions.
27
+ def debug(msg)
28
+ dynamic do |src, ctx|
29
+ puts "#{msg} @ #{src.line_and_column}:"
30
+ pp ctx.captures
31
+ any.present? | any.absent?
32
+ end
33
+ end
34
+
35
+ rule(:line_ending) { (str("\n") | str("\r\n") | str("\r")).ignore }
36
+ rule(:line_ending_or_eof) { line_ending | any.absent? }
37
+
38
+ rule(:whitespace) { match[" \t"] }
39
+ # NOTE: repeat(1) before EOF (any.absent?) because infinite loop otherwise
40
+ rule(:blank_line) { ((whitespace.repeat(1) >> any.absent?) | (whitespace.repeat >> line_ending)).ignore }
41
+ rule(:blank_line_verbatim) do
42
+ (whitespace.repeat(1).as(:ln) >> any.absent?) | (whitespace.repeat.as(:ln) >> line_ending)
43
+ end
44
+ rule(:line_char) { match["^\r\n"] }
45
+ rule(:line_verbatim) { line_char.repeat(1).as(:ln) >> line_ending_or_eof }
46
+
47
+ rule(:non_indent_space) { str(' ').repeat(0, 3) }
48
+
49
+ # Block nesting is the tricky part, but Parsanol's `dynamic` and `scope`
50
+ # make it possible to be aware of what blocks we're already in, and implement
51
+ # a check for whether we're still inside of those blocks on the beginning of
52
+ # every line. The rules that match the line run inside of the innermost
53
+ # parser expression, but this way they are aware of where they're nested at runtime.
54
+ #
55
+ # `continuation` MUST NOT be a `rule`, otherwise gets cached in a failure state
56
+ # and prevents nested alternatives from working
57
+ def continuation
58
+ dynamic do |_src, ctx|
59
+ # puts "parsing continuation at #{src.line_and_column} (#{src.bytepos}) with #{ctx.captures[:cont]}"
60
+ if ctx.captures.key?(:cont)
61
+ ctx.captures[:cont].ignore
62
+ else
63
+ any.present?
64
+ end
65
+ end
66
+ end
67
+
68
+ def open_block(kind, cont_rule)
69
+ dynamic do |_src, ctx|
70
+ caps = ctx.captures
71
+ # Chain-read before the write: picks up the enclosing
72
+ # block's continuation rule when one is in scope (Parsanol's
73
+ # Scope#[] searches parent frames; #key? is chain-aware).
74
+ caps[:cont] =
75
+ if caps.key?(:cont)
76
+ caps[:cont] >> cont_rule
77
+ else
78
+ cont_rule
79
+ end
80
+ caps[:block] = kind
81
+ str('')
82
+ end
83
+ end
84
+
85
+ rule(:atx_ending_seq) do
86
+ whitespace.repeat(1) >>
87
+ str('#').repeat >>
88
+ whitespace.repeat >>
89
+ # zero-width "at line end or EOF" — negative lookahead
90
+ match('[^\n\r]').absent?
91
+ end
92
+
93
+ # Escaped hash - not a heading
94
+ rule(:escaped_hash) do
95
+ str('\\') >> str('#')
96
+ end
97
+
98
+ rule(:atx_heading) do
99
+ non_indent_space >>
100
+ escaped_hash.absent? >>
101
+ str('#').repeat(1, 6).as(:heading) >>
102
+ str('#').absent? >>
103
+ (
104
+ # first, check to catch the case with only one space
105
+ # (that would be consumed with the repeat(1)) until ending seq
106
+ atx_ending_seq.absent? >>
107
+ str(' ').repeat(1) >>
108
+ (
109
+ atx_ending_seq.absent? >> line_char
110
+ ).repeat(1).as(:text)
111
+ ).maybe >>
112
+ atx_ending_seq.maybe >>
113
+ line_ending_or_eof
114
+ end
115
+
116
+ def thematic_break_char(c)
117
+ (str(c) >> whitespace.repeat).repeat(3)
118
+ end
119
+
120
+ rule(:thematic_break) do
121
+ non_indent_space >>
122
+ (
123
+ thematic_break_char('-') | thematic_break_char('_') | thematic_break_char('*')
124
+ ).output(hr: true) >>
125
+ line_ending_or_eof
126
+ end
127
+
128
+ rule(:indented_code_line) do
129
+ str(' ') >> line_verbatim
130
+ end
131
+
132
+ rule(:indented_code_blank_line) do
133
+ blank_line_verbatim.output(ln: '') >>
134
+ (
135
+ continuation >>
136
+ (str(' ') | blank_line_verbatim)
137
+ ).present?
138
+ end
139
+
140
+ rule(:indented_code_block) do
141
+ (
142
+ indented_code_line >>
143
+ (
144
+ continuation >>
145
+ (indented_code_line | indented_code_blank_line)
146
+ ).repeat
147
+ ).as(:code_block)
148
+ end
149
+
150
+ def code_fence_info
151
+ # NOTE: Uses dynamic block for context-dependent fence character detection
152
+ # This handles both backtick (`) and tilde (~) fenced code blocks
153
+ dynamic do |_src, ctx|
154
+ char = line_char
155
+ char = str('`').absent? >> char if ctx.captures[:fence].to_s.chr == '`'
156
+ char.repeat(1).as(:info).maybe
157
+ end
158
+ end
159
+
160
+ rule(:code_fence_open) do
161
+ non_indent_space.capture(:fence_indent) >>
162
+ (str('`').repeat(3) | str('~').repeat(3)).capture(:fence).ignore >>
163
+ code_fence_info >>
164
+ line_ending_or_eof
165
+ end
166
+
167
+ rule(:code_fence_close) do
168
+ non_indent_space >> dynamic do |_src, ctx|
169
+ str(ctx.captures[:fence]) >>
170
+ str(ctx.captures[:fence].to_s.chr).repeat
171
+ end.ignore >> line_ending_or_eof
172
+ end
173
+
174
+ def consume_fenced_indent
175
+ dynamic do |_src, ctx|
176
+ # A zero-width capture comes back from the native engine as an
177
+ # empty Array, whose #to_s is "[]" (length 2): read it as 0.
178
+ captured = ctx.captures[:fence_indent]
179
+ indent = captured.is_a?(Array) ? 0 : captured.to_s.length
180
+ if indent.positive?
181
+ str(' ').repeat(0, indent)
182
+ else
183
+ any.present?
184
+ end
185
+ end
186
+ end
187
+
188
+ rule(:fenced_code_block) do
189
+ code_fence_open >>
190
+ (
191
+ continuation >>
192
+ code_fence_close.absent? >>
193
+ consume_fenced_indent >>
194
+ (line_verbatim | blank_line_verbatim.output(ln: ''))
195
+ ).repeat.as(:code_block) >>
196
+ # tail: close the fence inside a continuation, or assert
197
+ # "not in a continuation with more input" — the former
198
+ # continuation.absent? | any.absent? chain put zero-width
199
+ # branches in non-last slots (shadowed-alternative lint,
200
+ # parsanol 1.3.55+); (A >> any).absent? == A.absent? | any.absent?
201
+ (
202
+ (continuation >> code_fence_close) | (continuation >> any).absent?
203
+ )
204
+ end
205
+
206
+ rule(:block_quote_marker) do
207
+ non_indent_space >>
208
+ str('>') >>
209
+ str(' ').maybe
210
+ end
211
+
212
+ # This implements laziness, which is context-sensitive:
213
+ # "only applies to lines that would have been continuations of
214
+ # paragraphs had they been prepended with block quote markers"
215
+ # means we *actually* must be inside of a continueable paragraph.
216
+ #
217
+ # Cannot be a `rule` as usual with `dynamic`.
218
+ def block_quote_cont
219
+ dynamic do |_src, ctx|
220
+ # puts "BQDYN in #{ctx.captures[:block]}"
221
+ block_quote_marker | if ctx.captures[:block] == :paragraph
222
+ paragraph_interrupt.absent? >> paragraph_continued_line.present?
223
+ else
224
+ any.absent? >> any.present? # never match
225
+ end
226
+ end
227
+ end
228
+
229
+ rule(:block_quote) do
230
+ block_quote_marker >> scope do
231
+ open_block(:block_quote, block_quote_cont) >>
232
+ (
233
+ (block | any.absent?.output('')) >>
234
+ (
235
+ continuation >>
236
+ (block | any.absent?.output(''))
237
+ ).repeat
238
+ ).as(:block_quote)
239
+ end
240
+ end
241
+
242
+ # IAL that appears on its own line (applies to next block or as ALD)
243
+ rule(:ial_block) do
244
+ whitespace.repeat(0, 3) >> (ial | ald) >> line_ending_or_eof
245
+ end
246
+
247
+ rule(:paragraph_interrupt) do
248
+ blank_line | atx_heading | thematic_break |
249
+ code_fence_open | block_quote | ial_block | extension |
250
+ unordered_list_marker | ordered_list_marker | definition_marker
251
+ end
252
+
253
+ rule(:paragraph_line) do
254
+ (line_char.repeat(1).as(:ln) >> any.absent?) | (line_char.repeat.as(:ln) >> line_ending)
255
+ end
256
+
257
+ rule(:paragraph_continued_line) do
258
+ whitespace.repeat.ignore >>
259
+ paragraph_line
260
+ end
261
+
262
+ rule(:paragraph) do
263
+ # Tempting to not use `scope` here as `paragraph` is a leaf block,
264
+ # but laziness rules for block quotes and lists need to know
265
+ # whether we are actually in a paragraph that could be continued
266
+ non_indent_space >> scope do
267
+ open_block(:paragraph, paragraph_interrupt.absent?) >>
268
+ (
269
+ paragraph_line >>
270
+ (
271
+ continuation >>
272
+ paragraph_continued_line
273
+ ).repeat
274
+ ).as(:p)
275
+ end
276
+ end
277
+
278
+ rule(:setext_underline) do
279
+ non_indent_space >>
280
+ (
281
+ str('-').repeat(1) | str('=').repeat(1)
282
+ ).as(:heading) >>
283
+ whitespace.repeat.ignore >>
284
+ line_ending_or_eof
285
+ end
286
+
287
+ rule(:setext_heading) do
288
+ check = paragraph_interrupt.absent? >> setext_underline.absent?
289
+ check >>
290
+ non_indent_space >>
291
+ (
292
+ paragraph_line >>
293
+ (
294
+ continuation >>
295
+ check >>
296
+ paragraph_continued_line
297
+ ).repeat
298
+ ).as(:text) >>
299
+ continuation >>
300
+ setext_underline
301
+ end
302
+
303
+ # ===== KRAMDOWN EXTENSIONS =====
304
+ # Attribute-list rules (ial/ald) live in AttributeLists.
305
+
306
+ # Block-level extension: {::extension_name options /}
307
+ # Common extensions: {::toc}, {::options ... /}
308
+ rule(:extension_name) do
309
+ match['a-z'].repeat(1)
310
+ end
311
+
312
+ rule(:extension_option) do
313
+ match['\\w\\-'].repeat(1) >> str('=') >>
314
+ (
315
+ (str('"') >> match['^"'].repeat(0) >> str('"')) |
316
+ (str("'") >> match["^'"].repeat(0) >> str("'")) |
317
+ match['^\\s/\\}'].repeat(1)
318
+ )
319
+ end
320
+
321
+ rule(:extension_options) do
322
+ (whitespace.repeat(1) >> extension_option).repeat
323
+ end
324
+
325
+ rule(:extension_self_closing) do
326
+ str('{::') >> extension_name.as(:ext_name) >> extension_options.as(:ext_options) >> whitespace.repeat >> str('/}')
327
+ end
328
+
329
+ rule(:extension_with_body) do
330
+ str('{::') >> extension_name.as(:ext_name) >> extension_options.as(:ext_options) >> str('}') >>
331
+ (str('{:/').absent? >> any).repeat.as(:ext_body) >>
332
+ str('{:/}')
333
+ end
334
+
335
+ rule(:extension) do
336
+ (extension_self_closing | extension_with_body).as(:extension)
337
+ end
338
+
339
+ # Block math: $$...$$ on its own line(s)
340
+ rule(:block_math) do
341
+ str('$$') >> line_ending >>
342
+ (str('$$').absent? >> any).repeat.as(:math_content) >>
343
+ str('$$')
344
+ end
345
+
346
+ # ===== GFM TABLE PARSING RULES =====
347
+
348
+ # Table cell: any characters except | and newline
349
+ rule(:table_cell) do
350
+ (str('|').absent? >> line_char).repeat.as(:cell)
351
+ end
352
+
353
+ # Table row: handles both | cell | cell | and cell | cell formats
354
+ # Pattern: optional leading pipe, then (cell pipe)+ cell, optional trailing pipe
355
+ # Or: cell | cell without any leading/trailing pipes
356
+ rule(:table_row) do
357
+ # Format with leading pipe: | cell | cell | or | cell | cell
358
+ (str('|') >> whitespace.maybe >>
359
+ (
360
+ table_cell >>
361
+ whitespace.maybe >>
362
+ str('|') >>
363
+ whitespace.maybe
364
+ ).repeat(1).as(:row)) |
365
+ # Format without leading pipe: cell | cell | or cell | cell
366
+ (table_cell >>
367
+ whitespace.maybe >>
368
+ str('|') >>
369
+ whitespace.maybe >>
370
+ (
371
+ table_cell >>
372
+ whitespace.maybe >>
373
+ str('|') >>
374
+ whitespace.maybe
375
+ ).repeat.as(:row_rest) >>
376
+ table_cell.maybe.as(:last_cell)).as(:row)
377
+ end
378
+
379
+ # Table separator cell: dashes with optional colons
380
+ rule(:table_separator_cell) do
381
+ str(':').maybe >>
382
+ str('-').repeat(1) >>
383
+ str(':').maybe
384
+ end
385
+
386
+ # Separator row: handles both |---|---| and ---|---| formats
387
+ rule(:table_separator_row) do
388
+ # Format with leading pipe: |---|---| or |---|---|
389
+ (str('|') >> whitespace.maybe >>
390
+ (
391
+ table_separator_cell.as(:sep) >>
392
+ whitespace.maybe >>
393
+ str('|') >>
394
+ whitespace.maybe
395
+ ).repeat(1)) |
396
+ # Format without leading pipe: ---|---| or ---|---|
397
+ (table_separator_cell.as(:sep) >>
398
+ whitespace.maybe >>
399
+ str('|') >>
400
+ whitespace.maybe >>
401
+ (
402
+ table_separator_cell.as(:sep) >>
403
+ whitespace.maybe >>
404
+ str('|') >>
405
+ whitespace.maybe
406
+ ).repeat >>
407
+ table_separator_cell.as(:sep).maybe)
408
+ end
409
+
410
+ # GFM Table: header row, separator row, body rows
411
+ rule(:table) do
412
+ table_row.as(:table_header) >>
413
+ line_ending >>
414
+ table_separator_row.as(:table_separator) >>
415
+ line_ending >>
416
+ (
417
+ table_row.as(:table_body_row) >> (line_ending | any.absent?)
418
+ ).repeat(1).as(:table_body)
419
+ end
420
+
421
+ rule(:block) do
422
+ blank_line | eob_marker | atx_heading | thematic_break |
423
+ indented_code_block | fenced_code_block |
424
+ block_quote | setext_heading |
425
+ unordered_list | ordered_list | definition_list |
426
+ footnote_definition | abbreviation_definition |
427
+ ial_block | extension | block_math |
428
+ table | paragraph
429
+ end
430
+
431
+ # ===== LIST PARSING RULES =====
432
+
433
+ # List interrupt - blocks that can interrupt a list
434
+ rule(:list_interrupt) do
435
+ blank_line.repeat(1) | atx_heading | thematic_break |
436
+ code_fence_open | block_quote |
437
+ unordered_list_marker | ordered_list_marker
438
+ end
439
+
440
+ # Unordered list marker: -, *, or + followed by 1+ spaces
441
+ rule(:unordered_list_marker) do
442
+ non_indent_space >>
443
+ match['-*+'] >>
444
+ str(' ').repeat(1)
445
+ end
446
+
447
+ # Ordered list marker: 1-9 digits followed by . or ) and 1+ spaces
448
+ rule(:ordered_list_marker) do
449
+ non_indent_space >>
450
+ match['1-9'] >>
451
+ match['0-9'].repeat >>
452
+ match['\\.)'] >>
453
+ str(' ').repeat(1)
454
+ end
455
+
456
+ # List item continuation line (indented content that's not a block)
457
+ # Excludes lines that look like nested list markers
458
+ rule(:list_continuation_line) do
459
+ ((str(' ') | str("\t")) >>
460
+ nested_list_marker.absent? >>
461
+ line_verbatim) |
462
+ (nested_list_marker.absent? >>
463
+ line_verbatim)
464
+ end
465
+
466
+ # Nested list marker detection (for 4-space indented lists)
467
+ rule(:nested_list_marker) do
468
+ (str(' ') | str("\t")) >>
469
+ (
470
+ (match['-*+'] >> str(' ').repeat(1)) |
471
+ (match['1-9'] >> match['0-9'].repeat >> match['\\.)'] >> str(' ').repeat(1))
472
+ )
473
+ end
474
+
475
+ # Thematic break as list item content (e.g., "- * * *")
476
+ rule(:thematic_break_in_list) do
477
+ (
478
+ (str('*') >> whitespace.repeat >> str('*') >> whitespace.repeat >> str('*')) |
479
+ (str('-') >> whitespace.repeat >> str('-') >> whitespace.repeat >> str('-')) |
480
+ (str('_') >> whitespace.repeat >> str('_') >> whitespace.repeat >> str('_'))
481
+ ) >> whitespace.repeat >> line_ending_or_eof
482
+ end
483
+
484
+ # Unordered list item with content
485
+ # Can contain thematic break or paragraph with continuation lines
486
+ rule(:unordered_list_item) do
487
+ unordered_list_marker.capture(:list_marker) >>
488
+ (
489
+ thematic_break_in_list.output(hr: true).as(:li) |
490
+ list_item_content.as(:li)
491
+ )
492
+ end
493
+
494
+ # List item content - paragraph first, then optional nested blocks
495
+ rule(:list_item_content) do
496
+ list_item_paragraph >>
497
+ (
498
+ continuation >>
499
+ list_interrupt.absent? >>
500
+ nested_block
501
+ ).repeat
502
+ end
503
+
504
+ # Nested block (indented list, etc.)
505
+ rule(:nested_block) do
506
+ ((str(' ') | str("\t")) >> nested_unordered_list) |
507
+ ((str(' ') | str("\t")) >> nested_ordered_list)
508
+ end
509
+
510
+ # Nested unordered list (4-space indented)
511
+ rule(:nested_unordered_list) do
512
+ (
513
+ nested_unordered_list_item >>
514
+ (
515
+ continuation >>
516
+ (str(' ') | str("\t")) >>
517
+ nested_unordered_list_item
518
+ ).repeat
519
+ ).as(:ul)
520
+ end
521
+
522
+ # Nested unordered list item (simpler format - just text content)
523
+ rule(:nested_unordered_list_item) do
524
+ unordered_list_marker >> line_verbatim
525
+ end
526
+
527
+ # Nested ordered list (4-space indented)
528
+ rule(:nested_ordered_list) do
529
+ (
530
+ nested_ordered_list_item >>
531
+ (
532
+ continuation >>
533
+ (str(' ') | str("\t")) >>
534
+ nested_ordered_list_item
535
+ ).repeat
536
+ ).as(:ol)
537
+ end
538
+
539
+ # Nested unordered list item (simpler format - just text content)
540
+ rule(:nested_unordered_list_item) do
541
+ unordered_list_marker >> line_text.as(:li)
542
+ end
543
+
544
+ # Line text without the :ln wrapper
545
+ rule(:line_text) do
546
+ line_char.repeat(1) >> line_ending_or_eof
547
+ end
548
+
549
+ # Nested ordered list item (simpler format - just text content)
550
+ rule(:nested_ordered_list_item) do
551
+ ordered_list_marker >> line_text.as(:li)
552
+ end
553
+
554
+ # List item paragraph - first line plus any continuation lines
555
+ rule(:list_item_paragraph) do
556
+ (
557
+ line_verbatim >>
558
+ (
559
+ continuation >>
560
+ list_interrupt.absent? >>
561
+ nested_list_marker.absent? >>
562
+ list_continuation_line
563
+ ).repeat
564
+ ).as(:p)
565
+ end
566
+
567
+ # Ordered list item with content (wraps content in p structure)
568
+ rule(:ordered_list_item) do
569
+ ordered_list_marker >>
570
+ list_item_content.as(:li)
571
+ end
572
+
573
+ # Unordered list: sequence of items (thematic break interrupts)
574
+ rule(:unordered_list) do
575
+ (
576
+ unordered_list_item >>
577
+ (
578
+ continuation >>
579
+ thematic_break.absent? >>
580
+ blank_line.maybe >>
581
+ unordered_list_item
582
+ ).repeat
583
+ ).as(:ul)
584
+ end
585
+
586
+ # Ordered list: sequence of numbered items (thematic break interrupts)
587
+ rule(:ordered_list) do
588
+ (
589
+ ordered_list_item >>
590
+ (
591
+ continuation >>
592
+ thematic_break.absent? >>
593
+ blank_line.maybe >>
594
+ ordered_list_item
595
+ ).repeat
596
+ ).as(:ol)
597
+ end
598
+
599
+ # ===== KRAMDOWN DEFINITION LIST PARSING RULES =====
600
+
601
+ # Definition list marker: colon followed by space
602
+ rule(:definition_marker) do
603
+ non_indent_space >>
604
+ str(':') >>
605
+ str(' ').repeat(1)
606
+ end
607
+
608
+ # Definition term: line(s) not starting with colon
609
+ # Can span multiple lines if next line doesn't start with :
610
+ rule(:definition_term_line) do
611
+ non_indent_space >>
612
+ str(':').absent? >>
613
+ line_verbatim
614
+ end
615
+
616
+ # Definition term with continuation
617
+ rule(:definition_term) do
618
+ (
619
+ definition_term_line >>
620
+ (
621
+ continuation >>
622
+ definition_marker.absent? >>
623
+ blank_line.absent? >>
624
+ definition_term_line
625
+ ).repeat
626
+ ).as(:def_term)
627
+ end
628
+
629
+ # Definition item content (after the :)
630
+ rule(:definition_content) do
631
+ (
632
+ line_verbatim >>
633
+ (
634
+ continuation >>
635
+ definition_marker.absent? >>
636
+ blank_line.absent? >>
637
+ (str(' ') | str("\t")).maybe >>
638
+ line_verbatim
639
+ ).repeat
640
+ ).as(:def_content)
641
+ end
642
+
643
+ # Definition item: : followed by content
644
+ rule(:definition_item) do
645
+ definition_marker >> definition_content
646
+ end
647
+
648
+ # Definition list item: term followed by one or more definitions
649
+ rule(:definition_list_item) do
650
+ definition_term >>
651
+ (
652
+ continuation >>
653
+ definition_item
654
+ ).repeat(1)
655
+ end
656
+
657
+ # Definition list: sequence of term+definition groups
658
+ rule(:definition_list) do
659
+ (
660
+ definition_list_item >>
661
+ (
662
+ continuation >>
663
+ blank_line.maybe >>
664
+ definition_list_item
665
+ ).repeat
666
+ ).as(:dl)
667
+ end
668
+
669
+ # ===== KRAMDOWN FOOTNOTE PARSING RULES =====
670
+
671
+ # Footnote definition: [^name]: content
672
+ rule(:footnote_id) do
673
+ str('[^') >> match['^\]'].repeat(1).as(:fn_id) >> str(']')
674
+ end
675
+
676
+ rule(:footnote_definition) do
677
+ non_indent_space >>
678
+ footnote_id >>
679
+ str(':') >>
680
+ whitespace.repeat >>
681
+ line_verbatim.as(:fn_content) >>
682
+ (
683
+ continuation >>
684
+ (str(' ') | str("\t")).repeat(1, 4) >>
685
+ line_verbatim
686
+ ).repeat.as(:fn_content_continued)
687
+ end
688
+
689
+ # ===== KRAMDOWN ABBREVIATION PARSING RULES =====
690
+
691
+ # Abbreviation definition: *[TERM]: definition
692
+ rule(:abbreviation_term) do
693
+ str('*[') >> match['^\]'].repeat(1).as(:abbr_term) >> str(']')
694
+ end
695
+
696
+ rule(:abbreviation_definition) do
697
+ non_indent_space >>
698
+ abbreviation_term >>
699
+ str(':') >>
700
+ whitespace.repeat >>
701
+ line_char.repeat.as(:abbr_def) >>
702
+ line_ending_or_eof
703
+ end
704
+
705
+ # ===== KRAMDOWN EOB (End of Block) MARKER =====
706
+
707
+ # EOB marker: ^ on its own line (terminates blocks explicitly)
708
+ rule(:eob_marker) do
709
+ whitespace.repeat >> str('^') >> whitespace.repeat >> line_ending_or_eof
710
+ end
711
+
712
+ root :document
713
+ rule(:document) do
714
+ block.repeat
715
+ end
716
+
717
+ def self.parse(filename)
718
+ content = File.read(filename)
719
+ new.parse(content)
720
+ rescue Parsanol::ParseFailed => e
721
+ puts e.parse_failure_cause.ascii_tree
722
+ end
723
+
724
+ # Parse with AST post-processing (escape sequences, etc.)
725
+ def self.parse_with_processing(content)
726
+ ast = new.parse(content)
727
+ AstProcessor.process(ast)
728
+ rescue Parsanol::ParseFailed => e
729
+ puts e.parse_failure_cause.ascii_tree
730
+ nil
731
+ end
732
+ end
733
+ end
734
+ end
735
+ end