coradoc-markdown 1.0.12 → 1.0.14

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 232eab66f6d33edae8a3cca57f5ebd16cca11ed96c4dd9a51611b9669410595b
4
- data.tar.gz: 1b0f0051729a0e226826c66d49bed0c4227732491b0bbf6148f42df524b1e95f
3
+ metadata.gz: 97e2e902cb727d13b70afa875d8dc11d43c993ed9bf776e1a531ade9d4da91eb
4
+ data.tar.gz: c2a5e5ea362bb459ca1fad91355542dc5b2038d6b3493595280c415470b5f822
5
5
  SHA512:
6
- metadata.gz: d20d1132ed4f527557207c11736d3ad10289fecf47a480341ed3b768bee9b2e812195fcf77d377a374b9c82804044c8c2aa701688c7e08706070ef62f957545c
7
- data.tar.gz: 24547cfb89cbe67a61f0bb63cee59495d2520d61df54ed03141963f5245e2cd5b1c537b80b2903a55b9cfa36d51639fc85f170a963686a6b71faff2a95845b11
6
+ metadata.gz: 953cdfa792f3ef195ed0349b771f6f4b75235e5da771ad94120dd4e2cad127fe691b98b4a7732e93e9c509670d2a9d7dadf0102960032f1e82459deec8030007
7
+ data.tar.gz: f89b2852646869758f31aaa3ce794e05955ed466e7210544f9325fa138a06c8f5fe6a9fa23df05a1a8da4e38283b5fc9f113900663ea6d690036bfedc34f7684
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Lint
6
+ # MD024 — headings at the same level must have unique text.
7
+ # Duplicate headings produce ambiguous outlines.
8
+ class DuplicateHeadings < Coradoc::Lint::Rule
9
+ rule_id 'MD024'
10
+ applies_to :markdown
11
+
12
+ def check(document, path)
13
+ seen = Hash.new(0)
14
+ Array(document.blocks).each do |block|
15
+ next unless block.is_a?(Coradoc::Markdown::Heading)
16
+
17
+ seen[[block.level, block.text]] += 1
18
+ end
19
+ seen.select { |_key, count| count > 1 }
20
+ .map do |(level, text), count|
21
+ build_violation(
22
+ path: path,
23
+ message: "duplicate heading (level #{level}): #{text.inspect} used #{count} times"
24
+ )
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Lint
6
+ # MD001 — heading levels must not skip: a heading may be at most
7
+ # one level deeper than the previous heading (markdownlint MD001).
8
+ # Headings are top-level blocks in the Markdown model.
9
+ class HeadingLevels < Coradoc::Lint::Rule
10
+ rule_id 'MD001'
11
+ applies_to :markdown
12
+
13
+ def check(document, path)
14
+ headings = Array(document.blocks).filter_map do |block|
15
+ [block.level, block.text] if block.is_a?(Coradoc::Markdown::Heading)
16
+ end
17
+ headings.each_cons(2).filter_map do |(prev, _), (level, text)|
18
+ next unless level > prev + 1
19
+
20
+ build_violation(path: path,
21
+ message: "heading level skips from #{prev} to #{level}: #{text}")
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ # Markdown lint rules (#165 follow-up): model-driven checks over
6
+ # the Markdown model tree. Rules self-register into
7
+ # Coradoc::Lint.registry when this file is required.
8
+ module Lint
9
+ require_relative 'lint/heading_levels'
10
+ require_relative 'lint/duplicate_headings'
11
+
12
+ RULES = [HeadingLevels, DuplicateHeadings].freeze
13
+
14
+ RULES.each { |rule| Coradoc::Lint.registry.register(rule) }
15
+ end
16
+ end
17
+ end
@@ -40,17 +40,15 @@ module Coradoc
40
40
  elements = []
41
41
  remaining = text
42
42
 
43
- # Pattern for footnote reference: [^name]
44
- fn_pattern = /\[\^([^\]]+)\]/
45
-
43
+ # Footnote reference scan: [^name] — linear index search
44
+ # instead of a regex (CodeQL rb/polynomial-redos).
46
45
  until remaining.empty?
47
- match = remaining.match(fn_pattern)
48
- if match
49
- # Add text before the match
50
- elements << match.pre_match unless match.pre_match.empty?
51
- # Add the footnote reference
52
- elements << { fn_ref: match[1] }
53
- remaining = match.post_match
46
+ open_idx = remaining.index('[^')
47
+ close_idx = open_idx && remaining.index(']', open_idx + 2)
48
+ if close_idx
49
+ elements << remaining[0...open_idx] unless open_idx.zero?
50
+ elements << { fn_ref: remaining[(open_idx + 2)...close_idx] }
51
+ remaining = remaining[(close_idx + 1)..]
54
52
  else
55
53
  # No more matches - add remaining text
56
54
  elements << remaining
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coradoc
4
+ module Markdown
5
+ module Parser
6
+ # Kramdown attribute-list rules shared by block and inline
7
+ # elements: IAL ({:.class #id key="value"}) and ALD
8
+ # ({:name: #id .class key="value"}).
9
+ module AttributeLists
10
+ def ial_class
11
+ str('.') >> match['\\w\\-'].repeat(1)
12
+ end
13
+
14
+ def ial_id
15
+ str('#') >> match['\\w\\-'].repeat(1)
16
+ end
17
+
18
+ def ial_key_value
19
+ match['\\w\\-'].repeat(1) >> str('=') >>
20
+ (
21
+ (str('"') >> match['^"'].repeat(0) >> str('"')) |
22
+ (str("'") >> match["^'"].repeat(0) >> str("'")) |
23
+ match['^\\s\\}'].repeat(1)
24
+ )
25
+ end
26
+
27
+ def ial_content
28
+ (
29
+ whitespace.repeat >>
30
+ (ial_class | ial_id | ial_key_value)
31
+ ).repeat(1)
32
+ end
33
+
34
+ def ial
35
+ str('{:') >> ial_content.as(:ial) >> str('}')
36
+ end
37
+
38
+ def ald_name
39
+ match['\\w'].repeat(1) >> str(':')
40
+ end
41
+
42
+ def ald
43
+ str('{:') >> ald_name.as(:ald_name) >> whitespace.repeat(1) >> ial_content.as(:ial) >> str('}')
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -7,6 +7,21 @@ module Coradoc
7
7
  require_relative 'parsanol_atoms'
8
8
 
9
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
+
10
25
  # NOTE: Debug method for parser development. Outputs current parse position
11
26
  # and capture context. Only called during parser debugging sessions.
12
27
  def debug(msg)
@@ -63,7 +78,7 @@ module Coradoc
63
78
  cont_rule
64
79
  end
65
80
  caps[:block] = kind
66
- any.present? | any.absent?
81
+ str('')
67
82
  end
68
83
  end
69
84
 
@@ -71,7 +86,8 @@ module Coradoc
71
86
  whitespace.repeat(1) >>
72
87
  str('#').repeat >>
73
88
  whitespace.repeat >>
74
- (line_ending.present? | any.absent?)
89
+ # zero-width "at line end or EOF" — negative lookahead
90
+ match('[^\n\r]').absent?
75
91
  end
76
92
 
77
93
  # Escaped hash - not a heading
@@ -174,8 +190,13 @@ module Coradoc
174
190
  consume_fenced_indent >>
175
191
  (line_verbatim | blank_line_verbatim.output(ln: ''))
176
192
  ).repeat.as(:code_block) >>
193
+ # tail: close the fence inside a continuation, or assert
194
+ # "not in a continuation with more input" — the former
195
+ # continuation.absent? | any.absent? chain put zero-width
196
+ # branches in non-last slots (shadowed-alternative lint,
197
+ # parsanol 1.3.55+); (A >> any).absent? == A.absent? | any.absent?
177
198
  (
178
- (continuation >> code_fence_close) | continuation.absent? | any.absent?
199
+ (continuation >> code_fence_close) | (continuation >> any).absent?
179
200
  )
180
201
  end
181
202
 
@@ -277,46 +298,7 @@ module Coradoc
277
298
  end
278
299
 
279
300
  # ===== KRAMDOWN EXTENSIONS =====
280
-
281
- # Inline Attribute List (IAL): {:.class #id key="value"}
282
- # Can appear after any block element to add attributes
283
- rule(:ial_class) do
284
- str('.') >> match['\\w\\-'].repeat(1)
285
- end
286
-
287
- rule(:ial_id) do
288
- str('#') >> match['\\w\\-'].repeat(1)
289
- end
290
-
291
- rule(:ial_key_value) do
292
- match['\\w\\-'].repeat(1) >> str('=') >>
293
- (
294
- (str('"') >> match['^"'].repeat(0) >> str('"')) |
295
- (str("'") >> match["^'"].repeat(0) >> str("'")) |
296
- match['^\\s\\}'].repeat(1)
297
- )
298
- end
299
-
300
- rule(:ial_content) do
301
- (
302
- whitespace.repeat >>
303
- (ial_class | ial_id | ial_key_value)
304
- ).repeat(1)
305
- end
306
-
307
- rule(:ial) do
308
- str('{:') >> ial_content.as(:ial) >> str('}')
309
- end
310
-
311
- # Attribute List Definition (ALD): {:name: #id .class key="value"}
312
- # Defines a named attribute list that can be referenced
313
- rule(:ald_name) do
314
- match['\\w'].repeat(1) >> str(':')
315
- end
316
-
317
- rule(:ald) do
318
- str('{:') >> ald_name.as(:ald_name) >> whitespace.repeat(1) >> ial_content.as(:ial) >> str('}')
319
- end
301
+ # Attribute-list rules (ial/ald) live in AttributeLists.
320
302
 
321
303
  # Block-level extension: {::extension_name options /}
322
304
  # Common extensions: {::toc}, {::options ... /}
@@ -9,7 +9,6 @@ module Coradoc
9
9
  autoload :HtmlEntities, "#{__dir__}/html_entities"
10
10
 
11
11
  class InlineParser < Parsanol::Parser
12
-
13
12
  rule(:line_ending) { (str("\n") | str("\r\n") | str("\r")).ignore }
14
13
  rule(:line_ending_or_eof) { line_ending | any.absent? }
15
14
  rule(:whitespace) { match[" \t"] }
@@ -47,14 +46,13 @@ module Coradoc
47
46
 
48
47
  rule(:escape) { str('\\').ignore >> match["!\"#$%&'\\(\\)*+,\\-./:;<=>?@\\[\\\\\\]\\^_`\\{\\|\\}~"] }
49
48
  rule(:dec_entity) do
50
- str('&#').ignore >> match['0-9'].repeat(1, 7).dynamic_output(method(:unicode_dec)) >> str(';').ignore
49
+ str('&#').ignore >> match['0-9'].repeat(1, 7).as(:dec_entity) >> str(';').ignore
51
50
  end
52
51
  rule(:hex_entity) do
53
- str('&#').ignore >> match['xX'].ignore >> match['A-Fa-f0-9'].repeat(1,
54
- 6).dynamic_output(method(:unicode_hex)) >> str(';').ignore
52
+ str('&#').ignore >> match['xX'].ignore >> match['A-Fa-f0-9'].repeat(1, 6).as(:hex_entity) >> str(';').ignore
55
53
  end
56
54
  rule(:entity) do
57
- str('&').ignore >> match['A-Za-z0-9'].repeat(1).dynamic_output(method(:lookup_entity)) >> str(';').ignore
55
+ str('&').ignore >> match['A-Za-z0-9'].repeat(1).as(:entity_name) >> str(';').ignore
58
56
  end
59
57
  rule(:nul_byte) { str("\0").output("\uFFFD") }
60
58
  rule(:special_char) { escape | dec_entity | hex_entity | entity | nul_byte }
@@ -64,11 +62,11 @@ module Coradoc
64
62
  end
65
63
 
66
64
  rule(:code_span) do
67
- str('`').does_not_precede? >>
65
+ Parsanol::Atoms::Lookbehind.new(1, '`', positive: false) >>
68
66
  str('`').repeat(1).capture(:code_opener).ignore >>
69
67
  dynamic do |_src, ctx|
70
- ending = (str('`').does_not_precede? >> str(ctx.captures[:code_opener]).ignore >> str('`').absent?)
71
- (ending.absent? >> any).repeat(1).dynamic_output(method(:process_code)).as(:code) >> ending
68
+ ending = (Parsanol::Atoms::Lookbehind.new(1, '`', positive: false) >> str(ctx.captures[:code_opener]).ignore >> str('`').absent?)
69
+ (ending.absent? >> any).repeat(1).as(:code_raw) >> ending
72
70
  end
73
71
  end
74
72
 
@@ -77,18 +75,18 @@ module Coradoc
77
75
  end
78
76
 
79
77
  rule(:both_flanking_delimiter_run) do
80
- any.precedes? >>
81
- unicode_whitespace.does_not_precede? >> (
82
- (
83
- unicode_punctuation.precedes? >>
84
- delimiter_run.as(:bfdr) >>
85
- unicode_punctuation.present?
86
- ) | (
87
- unicode_punctuation.does_not_precede? >>
88
- delimiter_run.as(:bfdr) >>
89
- unicode_punctuation.absent?
90
- )
91
- ) >> unicode_whitespace.absent?
78
+ Parsanol::Atoms::Lookbehind.regex('[\\s\\S]') >>
79
+ Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f]', positive: false) >> (
80
+ (
81
+ Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >>
82
+ delimiter_run.as(:bfdr) >>
83
+ unicode_punctuation.present?
84
+ ) | (
85
+ Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]', positive: false) >>
86
+ delimiter_run.as(:bfdr) >>
87
+ unicode_punctuation.absent?
88
+ )
89
+ ) >> unicode_whitespace.absent?
92
90
  end
93
91
 
94
92
  rule(:left_flanking_delimiter_run) do
@@ -97,24 +95,24 @@ module Coradoc
97
95
  delimiter_run.as(:lfdr) >>
98
96
  unicode_punctuation.absent?
99
97
  ) | (
100
- ((unicode_whitespace | unicode_punctuation).precedes? | any.does_not_precede?) >>
98
+ (Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f\\p{P}\\p{S}]') | Parsanol::Atoms::Lookbehind.regex('[\\s\\S]', positive: false)) >>
101
99
  delimiter_run.as(:lfdr)
102
100
  )
103
101
  ) >> unicode_whitespace.absent?
104
102
  end
105
103
 
106
104
  rule(:right_flanking_delimiter_run) do
107
- any.precedes? >>
108
- unicode_whitespace.does_not_precede? >> (
109
- (
110
- unicode_punctuation.precedes? >>
111
- delimiter_run.as(:rfdr) >>
112
- (unicode_whitespace | unicode_punctuation).present?
113
- ) | (
114
- unicode_punctuation.does_not_precede? >>
115
- delimiter_run.as(:rfdr)
105
+ Parsanol::Atoms::Lookbehind.regex('[\\s\\S]') >>
106
+ Parsanol::Atoms::Lookbehind.regex('[\\p{Zs}\\t\\r\\n\\f]', positive: false) >> (
107
+ (
108
+ Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >>
109
+ delimiter_run.as(:rfdr) >>
110
+ (unicode_whitespace | unicode_punctuation).present?
111
+ ) | (
112
+ Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]', positive: false) >>
113
+ delimiter_run.as(:rfdr)
114
+ )
116
115
  )
117
- )
118
116
  end
119
117
 
120
118
  rule(:non_flanking_delimiter_run) do
@@ -126,11 +124,11 @@ module Coradoc
126
124
  end
127
125
 
128
126
  rule(:run_surrounded_by_punctuation) do
129
- (unicode_punctuation.precedes? >> flanking_delimiter_run >> unicode_punctuation.present?).as(:rsp)
127
+ (Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >> flanking_delimiter_run >> unicode_punctuation.present?).as(:rsp)
130
128
  end
131
129
 
132
130
  rule(:run_preceded_by_punctuation) do
133
- (unicode_punctuation.precedes? >> flanking_delimiter_run).as(:rpp)
131
+ (Parsanol::Atoms::Lookbehind.regex('[\\p{P}\\p{S}]') >> flanking_delimiter_run).as(:rpp)
134
132
  end
135
133
 
136
134
  rule(:run_followed_by_punctuation) do
@@ -266,8 +264,31 @@ module Coradoc
266
264
  # pp x
267
265
  end
268
266
 
267
+ # Grammar-phase Ruby callables (parsanol-ruby#163: not
268
+ # wire-expressible) are replaced by raw captures, decoded here
269
+ # so parse() output is unchanged: {dec_entity: "65"} -> "A",
270
+ # {code_raw: s} -> {code: process_code(s)}.
271
+ def normalize_dynamic_captures(node)
272
+ case node
273
+ when Array
274
+ node.map { |child| normalize_dynamic_captures(child) }
275
+ when Hash
276
+ if node.size == 1
277
+ case node.keys.first
278
+ when :dec_entity then return unicode_dec(node.values.first)
279
+ when :hex_entity then return unicode_hex(node.values.first)
280
+ when :entity_name then return lookup_entity(node.values.first)
281
+ when :code_raw then return { code: process_code(node.values.first.to_s) }
282
+ end
283
+ end
284
+ node.transform_values { |v| normalize_dynamic_captures(v) }
285
+ else
286
+ node
287
+ end
288
+ end
289
+
269
290
  def parse(io, options = {})
270
- process_emphasis(super)
291
+ process_emphasis(normalize_dynamic_captures(super))
271
292
  end
272
293
  end
273
294
  end
@@ -5,130 +5,31 @@ require 'parsanol'
5
5
  module Coradoc
6
6
  module Markdown
7
7
  module Parser
8
- # Custom grammar atoms and DSL surface for the Markdown parser,
9
- # built on Parsanol::Atoms::Custom — the sanctioned extension
10
- # point. Replaces the former parslet_extras module, which
11
- # reached into Parslet internals via refinements.
8
+ # DSL surface for the Markdown parser. The custom atoms this
9
+ # module once carried (Output, DynamicOutput, Lookbehind) are
10
+ # retired: fixed-yield sites use Atoms::Constant, callable
11
+ # transforms moved to the AST phase, and guards use the
12
+ # built-in Atoms::Lookbehind — the grammar is fully
13
+ # wire-expressible (parsanol-ruby#163).
12
14
  module ParsanolAtoms
13
- # Matches +parslet+ but yields a fixed +value+.
14
- class Output < Parsanol::Atoms::Custom
15
- attr_reader :parslet, :value
16
-
17
- def initialize(parslet, value)
18
- @parslet = parslet
19
- @value = value
20
- super()
21
- end
22
-
23
- def try_match(source, context, consume_all)
24
- success, = parslet.apply(source, context, consume_all)
25
- [success, success ? value : nil]
26
- end
27
- end
28
-
29
- # Matches +parslet+ and yields the callable applied to the
30
- # flattened match value.
31
- class DynamicOutput < Parsanol::Atoms::Custom
32
- include Parsanol::Atoms::CanFlatten
33
-
34
- attr_reader :parslet, :callable
35
-
36
- def initialize(parslet, callable)
37
- @parslet = parslet
38
- @callable = callable
39
- super()
40
- end
41
-
42
- def try_match(source, context, consume_all)
43
- success, value = parslet.apply(source, context, consume_all)
44
- return [false, nil] unless success
45
-
46
- [true, callable.call(flatten(value))]
47
- end
48
- end
49
-
50
- # Positive/negative lookbehind: succeeds (zero-width) when the
51
- # preceding +number+ characters are/are not matched by
52
- # +bound_parslet+. Character-accurate for multibyte input —
53
- # rewinds over UTF-8 continuation bytes.
54
- class Lookbehind < Parsanol::Atoms::Custom
55
- attr_reader :positive, :number, :bound_parslet
56
-
57
- def initialize(bound_parslet, number, positive: true)
58
- @positive = positive
59
- @number = number
60
- @bound_parslet = bound_parslet
61
- super()
62
- end
63
-
64
- def error_msgs
65
- @error_msgs ||= {
66
- positive: ['Input should be preceded by ', bound_parslet],
67
- negative: ['Input should not be preceded by ', bound_parslet]
68
- }
69
- end
70
-
71
- def try_match(source, context, _consume_all)
72
- rewind_pos = source.bytepos
73
- if rewind_pos.zero?
74
- return [true, nil] unless positive
75
-
76
- return context.err_at(self, source, error_msgs[:positive], source.pos)
77
- end
78
-
79
- source.bytepos = rewind_chars(source, number)
80
- error_pos = source.pos
81
- success, = bound_parslet.apply(source, context, false)
82
-
83
- if positive
84
- return [true, nil] if success
85
-
86
- context.err_at(self, source, error_msgs[:positive], error_pos)
87
- elsif success
88
- context.err_at(self, source, error_msgs[:negative], error_pos)
89
- else
90
- [true, nil]
91
- end
92
- ensure
93
- source.bytepos = rewind_pos
94
- end
95
-
96
- private
97
-
98
- def rewind_chars(source, nchars)
99
- input = source.input
100
- pos = source.bytepos
101
- nchars.times do
102
- pos -= 1
103
- pos -= 1 while pos.positive? && (input.getbyte(pos) & 0xC0) == 0x80
104
- end
105
- [pos, 0].max
106
- end
107
- end
108
15
  end
109
16
  end
110
17
  end
111
18
  end
112
19
 
113
- # Additive DSL surface (no overrides): exposes the custom atoms on
114
- # every atom the way the former parslet_extras refinements did.
20
+ # Additive DSL surface (no overrides): exposes the remaining custom
21
+ # DSL on every atom. The custom DynamicOutput/Lookbehind atoms are
22
+ # GONE — raw captures decode in the AST phase and guards use the
23
+ # built-in Parsanol::Atoms::Lookbehind (literal + regex forms,
24
+ # parsanol >= 1.3.77) — parsanol-ruby#163.
115
25
  module Parsanol
116
26
  module Atoms
117
27
  module DSL
28
+ # Matches self (discarding its captures) and yields a fixed
29
+ # value — built on Atoms::Constant so the shape is
30
+ # wire-expressible for the native engine (rs#137 follow-up).
118
31
  def output(value)
119
- Coradoc::Markdown::Parser::ParsanolAtoms::Output.new(self, value)
120
- end
121
-
122
- def dynamic_output(callable)
123
- Coradoc::Markdown::Parser::ParsanolAtoms::DynamicOutput.new(self, callable)
124
- end
125
-
126
- def precedes?(num = 1)
127
- Coradoc::Markdown::Parser::ParsanolAtoms::Lookbehind.new(self, num, positive: true)
128
- end
129
-
130
- def does_not_precede?(num = 1)
131
- Coradoc::Markdown::Parser::ParsanolAtoms::Lookbehind.new(self, num, positive: false)
32
+ ignore >> Constant.new(value)
132
33
  end
133
34
  end
134
35
  end
@@ -12,6 +12,7 @@ require_relative 'serializers/image'
12
12
  require_relative 'serializers/horizontal_rule'
13
13
  require_relative 'serializers/table'
14
14
  require_relative 'serializers/emphasis'
15
+ require_relative 'serializers/text'
15
16
  require_relative 'serializers/strong'
16
17
  require_relative 'serializers/code'
17
18
  require_relative 'serializers/strikethrough'
@@ -58,6 +59,7 @@ module Coradoc
58
59
  Serializers::HorizontalRule,
59
60
  Serializers::Table,
60
61
  Serializers::Emphasis,
62
+ Serializers::Text,
61
63
  Serializers::Strong,
62
64
  Serializers::Code,
63
65
  Serializers::Strikethrough,
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../element_serializer'
4
+
5
+ module Coradoc
6
+ module Markdown
7
+ class Serializer
8
+ module Serializers
9
+ class Text < ElementSerializer
10
+ handles_type ::Coradoc::Markdown::Text
11
+
12
+ def call(element, _ctx)
13
+ element.content.to_s
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -93,6 +93,7 @@ module Coradoc
93
93
  def transform_document(doc)
94
94
  blocks, frontmatter = extract_frontmatter(Array(doc.children))
95
95
  blocks = blocks.flat_map { |child| flatten_result(transform(child)) }
96
+ blocks = prepend_title_heading(doc.title, blocks)
96
97
 
97
98
  Coradoc::Markdown::Document.new(
98
99
  id: doc.id,
@@ -101,6 +102,23 @@ module Coradoc
101
102
  )
102
103
  end
103
104
 
105
+ # The document title renders as a leading "# {title}" H1. Skipped
106
+ # when the body already opens with a heading of the same text —
107
+ # a Markdown→CoreModel pass keeps its first heading (of any
108
+ # level) both as the title and as a section, and emitting the
109
+ # title again would duplicate it.
110
+ def prepend_title_heading(title, blocks)
111
+ return blocks if title.nil? || title.to_s.strip.empty?
112
+ return blocks if title_heading_duplicated?(title, blocks)
113
+
114
+ [Coradoc::Markdown::Heading.new(level: 1, text: title.to_s), *blocks]
115
+ end
116
+
117
+ def title_heading_duplicated?(title, blocks)
118
+ first = blocks.first
119
+ first.is_a?(Coradoc::Markdown::Heading) && first.text == title.to_s
120
+ end
121
+
104
122
  # If the first CoreModel child is a FrontmatterBlock, serialize
105
123
  # it to YAML text via Codec (single source of truth) and pop
106
124
  # it from the children list. Returns [remaining_children,
@@ -38,6 +38,8 @@ module Coradoc
38
38
  transform_image(model)
39
39
  when Coradoc::Markdown::Link
40
40
  transform_link(model)
41
+ when Coradoc::Markdown::Text
42
+ Coradoc::CoreModel::TextContent.new(text: model.content)
41
43
  when Coradoc::Markdown::Emphasis
42
44
  transform_inline(model, 'italic')
43
45
  when Coradoc::Markdown::Strong
@@ -64,8 +66,6 @@ module Coradoc
64
66
  transform_attribute_list(model)
65
67
  when Coradoc::Markdown::Comment
66
68
  Coradoc::CoreModel::CommentBlock.new(content: model.text.to_s)
67
- when Coradoc::Markdown::Text
68
- model.content.to_s
69
69
  when Array
70
70
  model.map { |item| transform(item) }
71
71
  else
@@ -114,7 +114,8 @@ module Coradoc
114
114
  content = extract_text(para.text)
115
115
 
116
116
  Coradoc::CoreModel::ParagraphBlock.new(
117
- content: content
117
+ content: content,
118
+ children: para.children.map { |child| transform(child) }
118
119
  )
119
120
  end
120
121
 
@@ -137,7 +138,8 @@ module Coradoc
137
138
  Coradoc::CoreModel::ListItem.new(
138
139
  content: extract_text(item.text),
139
140
  marker: list.ordered ? '1.' : '*',
140
- nested_list: nested
141
+ nested_list: nested,
142
+ children: inline_children(item)
141
143
  )
142
144
  end
143
145
 
@@ -147,13 +149,20 @@ module Coradoc
147
149
  )
148
150
  end
149
151
 
152
+ # Carry inline models (bold, italic, code) parsed from the item
153
+ # text through to the CoreModel item as styled children.
154
+ def inline_children(item)
155
+ Array(item.children).map { |child| transform(child) }
156
+ end
157
+
150
158
  def transform_table(table)
151
159
  # Convert Markdown table to CoreModel table
152
160
  rows = []
153
161
 
154
- # Add header row if present
162
+ # Add header row if present (GFM tables always lead with one)
155
163
  if table.headers.any?
156
164
  rows << Coradoc::CoreModel::TableRow.new(
165
+ header: true,
157
166
  cells: table.headers.map do |h|
158
167
  Coradoc::CoreModel::TableCell.new(content: h.to_s, header: true)
159
168
  end
@@ -279,7 +279,8 @@ module Coradoc
279
279
 
280
280
  if element.key?(:p)
281
281
  text = extract_text_from_p(element[:p])
282
- paragraph = Paragraph.new(text: text)
282
+ paragraph = Paragraph.new(text: text,
283
+ children: inline_models_for(element[:p]))
283
284
  apply_ial_to_element(paragraph, element[:ial]) if element.key?(:ial)
284
285
  return paragraph
285
286
  end
@@ -345,12 +346,14 @@ module Coradoc
345
346
  def transform_list_item(li)
346
347
  nodes = normalize_list_item_nodes(li)
347
348
  text_parts = []
349
+ inline_children = []
348
350
  sublist = nil
349
351
  nodes.each do |n|
350
352
  case n
351
353
  when Hash
352
354
  if n.key?(:p)
353
355
  text_parts << extract_text_from_p(n[:p])
356
+ inline_children.concat(inline_models_for(n[:p]))
354
357
  elsif n.key?(:ul)
355
358
  sublist = transform_list(n[:ul], ordered: false)
356
359
  elsif n.key?(:ol)
@@ -360,7 +363,8 @@ module Coradoc
360
363
  text_parts << n.to_s
361
364
  end
362
365
  end
363
- item = ListItem.new(text: text_parts.join(' ').strip)
366
+ item = ListItem.new(text: text_parts.join(' ').strip,
367
+ children: inline_children)
364
368
  item.sublist = sublist if sublist
365
369
  item
366
370
  end
@@ -511,6 +515,73 @@ module Coradoc
511
515
  end
512
516
  end
513
517
 
518
+ # Inline models for a paragraph's raw lines: runs the InlineParser
519
+ # over each line so emphasis/code/link survive to CoreModel as
520
+ # InlineElements instead of flattened marker text.
521
+ def inline_models_for(p)
522
+ lines = if p.is_a?(Hash)
523
+ [p[:ln]]
524
+ else
525
+ Array(p).map { |l| l.is_a?(Hash) ? l[:ln] : l }
526
+ end
527
+ lines.compact.flat_map.with_index do |line, idx|
528
+ models = build_inline_models(line.to_s)
529
+ if idx.zero?
530
+ models
531
+ else
532
+ [Text.new(content: "\n"), *models]
533
+ end
534
+ end
535
+ end
536
+
537
+ def build_inline_models(line)
538
+ Coradoc::Markdown.parse_inline(line).flat_map { |el| inline_model(el) }
539
+ end
540
+
541
+ # Build Markdown model(s) for one inline-parser node. Emphasis
542
+ # subtrees (nested strong/emph/code/text) collapse to the
543
+ # outermost style with their literal text — CoreModel inline
544
+ # nodes carry flat content, matching the DOCX-side shape where
545
+ # each run becomes a single InlineElement.
546
+ def inline_model(el)
547
+ return Text.new(content: el.to_s) unless el.is_a?(Hash)
548
+
549
+ if el.key?(:strong)
550
+ Strong.new(text: flatten_inline_text(el[:strong]))
551
+ elsif el.key?(:emph)
552
+ Emphasis.new(text: flatten_inline_text(el[:emph]))
553
+ elsif el.key?(:code)
554
+ Code.new(text: el[:code].to_s)
555
+ elsif el.key?(:text)
556
+ Text.new(content: el[:text].to_s)
557
+ else
558
+ Text.new(content: flatten_inline_text([el]))
559
+ end
560
+ end
561
+
562
+ # Recursively flatten any inline subtree (arrays, nested
563
+ # strong/emph hashes, code spans, Parsanol slices) to its
564
+ # literal text. Inner style markers survive as characters so
565
+ # the round trip stays stable.
566
+ def flatten_inline_text(subtree)
567
+ case subtree
568
+ when Array
569
+ subtree.map { |n| flatten_inline_text(n) }.join
570
+ when Hash
571
+ if subtree.key?(:text)
572
+ subtree[:text].to_s
573
+ elsif subtree.key?(:code)
574
+ "`#{subtree[:code]}`"
575
+ else
576
+ subtree.values.map { |v| flatten_inline_text(v) }.join
577
+ end
578
+ when Parsanol::Slice
579
+ subtree.to_s
580
+ else
581
+ subtree.to_s
582
+ end
583
+ end
584
+
514
585
  # Extract code from code_block structure
515
586
  def extract_code(code_block)
516
587
  case code_block
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Coradoc
4
4
  module Markdown
5
- VERSION = '1.0.12'
5
+ VERSION = '1.0.14'
6
6
  end
7
7
  end
@@ -35,6 +35,9 @@ module Coradoc
35
35
  autoload :Heading, 'coradoc/markdown/model/heading'
36
36
  autoload :Paragraph, 'coradoc/markdown/model/paragraph'
37
37
  autoload :Text, 'coradoc/markdown/model/text'
38
+
39
+ # Registration side effect: loading registers rules into Coradoc::Lint.registry.
40
+ autoload :Lint, 'coradoc/markdown/lint'
38
41
  autoload :List, 'coradoc/markdown/model/list'
39
42
  autoload :ListItem, 'coradoc/markdown/model/list_item'
40
43
  autoload :CodeBlock, 'coradoc/markdown/model/code_block'
@@ -90,6 +93,7 @@ module Coradoc
90
93
 
91
94
  # Parser module namespace
92
95
  module Parser
96
+ autoload :AttributeLists, 'coradoc/markdown/parser/attribute_lists'
93
97
  autoload :BlockParser, 'coradoc/markdown/parser/block_parser'
94
98
  autoload :InlineParser, 'coradoc/markdown/parser/inline_parser'
95
99
  autoload :ParsanolAtoms, 'coradoc/markdown/parser/parsanol_atoms'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coradoc-markdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.12
4
+ version: 1.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
@@ -41,16 +41,16 @@ dependencies:
41
41
  name: parsanol
42
42
  requirement: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - ">="
44
+ - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '0'
46
+ version: 1.3.79
47
47
  type: :runtime
48
48
  prerelease: false
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 1.3.79
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: rspec
56
56
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +105,9 @@ files:
105
105
  - LICENSE.txt
106
106
  - lib/coradoc/markdown.rb
107
107
  - lib/coradoc/markdown/errors.rb
108
+ - lib/coradoc/markdown/lint.rb
109
+ - lib/coradoc/markdown/lint/duplicate_headings.rb
110
+ - lib/coradoc/markdown/lint/heading_levels.rb
108
111
  - lib/coradoc/markdown/model/abbreviation.rb
109
112
  - lib/coradoc/markdown/model/admonition.rb
110
113
  - lib/coradoc/markdown/model/attribute_list.rb
@@ -148,6 +151,7 @@ files:
148
151
  - lib/coradoc/markdown/model/verse.rb
149
152
  - lib/coradoc/markdown/parser.rb
150
153
  - lib/coradoc/markdown/parser/ast_processor.rb
154
+ - lib/coradoc/markdown/parser/attribute_lists.rb
151
155
  - lib/coradoc/markdown/parser/block_parser.rb
152
156
  - lib/coradoc/markdown/parser/frontmatter_parser.rb
153
157
  - lib/coradoc/markdown/parser/html_entities.rb
@@ -196,6 +200,7 @@ files:
196
200
  - lib/coradoc/markdown/serializer/serializers/subscript.rb
197
201
  - lib/coradoc/markdown/serializer/serializers/superscript.rb
198
202
  - lib/coradoc/markdown/serializer/serializers/table.rb
203
+ - lib/coradoc/markdown/serializer/serializers/text.rb
199
204
  - lib/coradoc/markdown/serializer/serializers/underline.rb
200
205
  - lib/coradoc/markdown/serializer/serializers/verse.rb
201
206
  - lib/coradoc/markdown/serializer/strategies/admonition/base.rb