rouge-carve 0.1.5 → 0.2.0

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: 56fbae7b1785bf82049bdeb87ee240391843fd025ae86d383ad7b48b3b3e9562
4
- data.tar.gz: bc9a2e6b121c743dec62e18c059105fd2b3959a0d4fc09684849475c071a7e76
3
+ metadata.gz: 80e4e45c2edbbcdd8245b1b786804a5e5649e973512778a37f90f2225ecc4ca2
4
+ data.tar.gz: 4f961773751f3bfdf37213a8dec97a915508d3037c74c37fa452fadebbf644d3
5
5
  SHA512:
6
- metadata.gz: be5892326629840d6697bb02c14875b1b075b5220d393e88fd5f85af894af8ada36911dd81c90eb3bb8b4d2cb2c7bc182731704a18c6c2d0ef7f4ef459e360a5
7
- data.tar.gz: c14089a1b08264317473a18c84fdad82a946499096f5ef0143952b073a74a4d37da745a53d469a635bae2a752c15a94076c16a2abb4429f85c15504581cce2f5
6
+ metadata.gz: 28d9a4b21800d3b75ac930e10ac21d1e8e68c723b66ce0a2022a9641809df0db5aa5241098db6a0631f73977ec7e8e8a6e592df73acdaef8bbc70eda49196604
7
+ data.tar.gz: 2cfe337bfcc63cf4646369c60cfa5e336804d976a0df5426c4ea0ab4135e5fcb77cf8f640b61db0171d8ce1b1402731531da9a2c9a058587f9d6fe80681f4f74
data/CHANGELOG.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.2.0] - 2026-12-04
6
+
7
+ The `carve` lexer highlights Carve 0.2 syntax.
8
+
9
+ ### Breaking
10
+
11
+ - Replace the 0.1 `carve` lexer with the 0.2 rule graph. `carve` and `crv` (`*.crv`, `*.carve`) now highlight Carve 0.2 syntax (#40, #41, #42).
12
+
13
+ ### Fixes
14
+
15
+ - Documents with many unclosed openers highlight in linear time (#44).
16
+
17
+ ### Improvements
18
+
19
+ - Snippet definitions (`{$name}: value`) and uses (`{{ $name }}`) are highlighted (#47).
20
+ - Leaf extension lines, glued div names, continued heading and caption labels and typed metadata blocks after line 1 are highlighted (#48).
21
+
5
22
  ## [0.1.5] - 2026-11-12
6
23
 
7
24
  - Braced cell alignment (`|={>} Price |`, `|={<^ .x} Name |`, `|{~} 1 |`) lexes as an attribute with its alignment run as an operator, like the deprecated `|=>` prefix; cell pipes and escaped pipes stay outside it (#37).
data/README.md CHANGED
@@ -1,26 +1,10 @@
1
1
  # rouge-carve
2
2
 
3
- Rouge lexer for Carve 0.1 syntax. It registers `.crv`, `.carve`, and Carve fenced code for Rouge 3.30 or later. Read the [Carve documentation](https://markup-carve.github.io/carve/) for the language syntax.
4
-
5
- ## Install
6
-
7
- ```sh
8
- gem install rouge-carve
9
- ```
10
-
11
- ## Usage
3
+ Rouge lexer for Carve 0.2 syntax. It registers `carve` and `crv` for `.crv` and `.carve` files.
12
4
 
13
5
  ```ruby
14
6
  require 'rouge-carve'
15
7
  Rouge::Lexer.find('carve')
16
8
  ```
17
9
 
18
- ## Documentation
19
-
20
- - [Carve documentation](https://markup-carve.github.io/carve/)
21
- - [Documentation index](docs/README.md)
22
- - [Development](docs/development.md)
23
-
24
- ## License
25
-
26
- [MIT](LICENSE)
10
+ See the [Carve documentation](https://markup-carve.github.io/carve/) for source syntax.
@@ -1,54 +1,11 @@
1
1
  # -*- coding: utf-8 -*- #
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative 'carve_continuation'
5
4
 
6
5
  module Rouge
7
6
  module Lexers
8
- # Carve is a lightweight markup language for documents and the web. Its
9
- # inline
10
- # delimiters deliberately differ from Markdown's, which is why lexing a
11
- # Carve document as Markdown produces actively WRONG output rather than
12
- # merely plain text:
13
- #
14
- # Carve means Markdown would read it as
15
- # --------- ------------- -------------------------
16
- # *bold* strong emphasis
17
- # /italic/ emphasis literal slashes
18
- # _under_ underline emphasis
19
- # ~strike~ strikethrough subscript / literal
20
- # {=mark=} highlight literal braces
21
- # {^sup^} superscript literal braces
22
- #
23
- # WHY THE BLOCK OPENERS ARE NOT ANCHORED AT COLUMN 0. Carve opens a block at
24
- # column 0, or at an enclosing container's content column - nowhere in
25
- # between. So ` # H` at document level is a paragraph, while the same
26
- # opener at a list item's content column is a real heading. Telling those
27
- # apart needs a container model that tracks the item's content column. A
28
- # regex lexer's state stack could carry one, but the sibling grammars
29
- # (Prism, highlight.js, Pygments) do not, and this lexer keeps their
30
- # trade-off on purpose so they agree: block openers match at any indent and
31
- # knowingly over-colour the rare indented-at-document-level case, rather
32
- # than under-colouring the common valid shape of an indented construct
33
- # inside a list item.
34
- #
35
- # WHY AN ATTRIBUTE BLOCK IS ONE TOKEN. `{#id .cls key="v" :lang}` is emitted
36
- # whole as Name::Attribute rather than split into id, class, key, value and
37
- # language parts. Splitting reads better in isolation, but an attribute
38
- # block can carry a brace inside a quoted value, a language tag, and a bare
39
- # key that is not an attribute at all, and the sibling grammars treat the
40
- # block as one unit; a consumer asking "is this text inside an attribute
41
- # block" must get the same answer here as it does there.
42
- #
43
- # WHERE ROUGE CANNOT SAY WHAT CARVE MEANS. Carve's `_x_` is UNDERLINE, not
44
- # emphasis, and Rouge has no underline token - the vocabulary stops at
45
- # Generic::Emph, Generic::Strong and Generic::EmphStrong. The content takes
46
- # Generic::Emph so it is at least marked up, and the delimiters stay
47
- # Punctuation so a consumer can still tell underline from italic by the
48
- # delimiter it carries. Pygments, which has Generic.Underline, does make
49
- # the distinction.
50
- #
51
- # Spec: https://markup-carve.github.io/carve/
7
+ # Rouge emits a flat stream of tokens, so nested inline constructs can
8
+ # share a content scope while retaining their punctuation.
52
9
  class Carve < RegexLexer
53
10
  class DelimitedTable < RegexLexer
54
11
  # Unquoted fields are Name::Variable (colored in the default Rouge
@@ -118,7 +75,7 @@ module Rouge
118
75
  SEPARATOR = "\t".freeze
119
76
  end
120
77
 
121
- title 'Carve'
78
+ title 'Carve 0.2'
122
79
  desc 'Carve, a lightweight markup language for documents and the web'
123
80
 
124
81
  tag 'carve'
@@ -144,6 +101,22 @@ module Rouge
144
101
  /x.freeze
145
102
  ATTRS = /\{#{ATTRS_TAIL}/.freeze
146
103
 
104
+ TYPED_OPENER = /(---)( ?)([A-Za-z0-9]+)([ \t]*(?:\n|\z))/.freeze
105
+ ATTRS_ONLY = /\A(?:#{ATTRS}[ \t]*)+\z/.freeze
106
+
107
+ # An extension name never ends in `-` or `_` (C2-I-094).
108
+ EXT_NAME = /[a-zA-Z](?:[\w-]*[a-zA-Z0-9])?/.freeze
109
+
110
+ # The rest of a leaf extension line after its `[` (C2-B-238): a label
111
+ # whose brackets nest, where an escape or a code span hides a bracket,
112
+ # then glued attribute blocks and nothing else.
113
+ LEAF_REST = begin
114
+ inner = /`[^`\n]*`|[^\[\]\\\n]|\\./.source
115
+ label = "(?:#{inner})*"
116
+ 3.times { label = "(?:#{inner}|\\[#{label}\\])*" }
117
+ /#{label}\](?:#{ATTRS})*[ \t]*$/
118
+ end
119
+
147
120
  # The same block as a STANDALONE ATTRIBUTE LINE, which may span lines
148
121
  # but not a blank one: a blank line ends the block it would attach to.
149
122
  # The inline form cannot span lines at all, because an unclosed inline
@@ -183,11 +156,7 @@ module Rouge
183
156
  # An ordered marker: a number, one letter, or a roman numeral. `Body.`
184
157
  # is a word, not a marker, and a letter with `.` needs two spaces, so
185
158
  # `J. R. R. Tolkien` is prose (carve#4811).
186
- ORDERED = "(?:\\d+[.)]|(?:[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)(?:\\)|\\.(?=(?:#{ATTRS})? )))"
187
-
188
- # The states read a column-0 `+` with a flush-left block below as an
189
- # attachment. Whether a `+` is the marker is settled by CarveContinuation.
190
- PLUS_NEXT = /(?=\n[^ \t\n])/.freeze
159
+ ORDERED = "(?:\\d{1,9}[.)]|(?:[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)(?:\\)|\\.(?=(?:#{ATTRS})? )))"
191
160
 
192
161
  # A list marker at the start of a line, as the list rules below read it.
193
162
  LIST_MARKER = "(?:[-*]|#{ORDERED}|\\.)(?:#{ATTRS})?(?:[ \\t]|$)"
@@ -206,12 +175,10 @@ module Rouge
206
175
  # caption do not. A fence opener does without looking for its closer
207
176
  # (carve#4018); a backtick in a backtick info string makes it a code span.
208
177
  INTERRUPT = %r/
209
- \#{1,6}[ \t] | >(?:[ \t]|$) | ::: | ::[ \t] | %%
210
- | `{3,}[^`\n]*$ | ~{3,}
211
- | (?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$
212
- | \|=?[<>^v~]*#{ROW_END}
213
- | (?:\[\^|\*\[|\[)[^\]\n]+\]:[ ]
214
- | #{ATTRS_INTERRUPT}[ \t]*$
178
+ `{3,}[^`\n]*$ | :{3,} | %{3,}
179
+ | \*{3,}[ \t]*$
180
+ | \|#{ROW_END}
181
+ | %%[ \t]*\#(?:region[ \t]+[A-Za-z_][\w-]*|endregion(?:[ \t]+[A-Za-z_][\w-]*)?)[ \t]*$
215
182
  /x.freeze
216
183
 
217
184
  # Where a bracketed inline body must close, by the block it sits in: a
@@ -234,10 +201,22 @@ module Rouge
234
201
  #{MARGIN}(?:!#{LABEL}(?:\([^)\n]*\)|#{LABEL})?|\$\$(`+)[^\n]*?\1)(?:#{ATTRS})?[ \t]*$
235
202
  /x.freeze
236
203
 
204
+ # Snippets (C2-B-236, C2-B-237, C2-I-128). A use is `{{`, spaces, `$`
205
+ # and a name, spaces, `}}`; a tab or any other shape is an include or
206
+ # text. The marker of a definition holds attribute parts but no id.
207
+ SNIPPET_NAME = '[A-Za-z0-9_][A-Za-z0-9_-]*'
208
+ SNIPPET_USE = /(\{\{)( +)(\$#{SNIPPET_NAME})( +)(\}\})/.freeze
209
+ SNIPPET_PART = /(?:\.[A-Za-z0-9_-]+|:(?:[A-Za-z0-9]{1,8}(?:-[A-Za-z0-9]{1,8})*)?|[A-Za-z_][A-Za-z0-9_-]*=(?:"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|[^\s"'{}|\\]+)|[A-Za-z][A-Za-z0-9_-]*)/.freeze
210
+ SNIPPET_GLUED = /\{ *#{SNIPPET_PART}(?: +#{SNIPPET_PART})* *\}/.freeze
211
+ SNIPPET_DEF = %r/^(\{)(\$#{SNIPPET_NAME})((?: +#{SNIPPET_PART})* *)(\})((?:#{SNIPPET_GLUED})*)(:)( )(?=[ \t]*[^ \t\n])/.freeze
212
+ # A use alone on its line, which is a block of its own.
213
+ SNIPPET_BLOCK = /#{SNIPPET_USE}(?=[ \t]*$)/.freeze
214
+
237
215
  # The braced inline families: delimiter, content token, state.
238
216
  BRACED = {
239
217
  '*' => [Generic::Strong, :forcedstrong], '/' => [Generic::Emph, :forcedemph],
240
- '_' => [Generic::Emph, :forcedunderline], '^' => [Generic::Emph, :superscript],
218
+ '_' => [Generic::Emph, :forcedunderline], '~' => [Generic::Deleted, :forcedstrike],
219
+ '^' => [Generic::Emph, :superscript],
241
220
  ',' => [Generic::Emph, :subscript], '=' => [Generic::Inserted, :bracedmark],
242
221
  '+' => [Generic::Inserted, :insertion], '-' => [Generic::Deleted, :deletion]
243
222
  }.freeze
@@ -247,10 +226,7 @@ module Rouge
247
226
  # non-space before it and no letter or digit after it.
248
227
  BARE = {
249
228
  '*' => [:strong, Generic::Strong, /[\w*]/, /[\s*]/],
250
- '/' => [:emph, Generic::Emph, /[\w\/]/, /[\s\/]/],
251
- '_' => [:underline, Generic::Emph, /[\w_\/]/, /[\s_]/],
252
- '~' => [:strike, Generic::Deleted, /[\w~]/, /[\s~]/],
253
- '=' => [:mark, Generic::Inserted, /[\w=<>!]/, /[\s>=]/]
229
+ '/' => [:emph, Generic::Emph, /[\w\/]/, /[\s\/]/]
254
230
  }.freeze
255
231
 
256
232
  # Characters that can begin an inline construct. A run of anything else is
@@ -296,9 +272,9 @@ module Rouge
296
272
  # output format verbatim. Emitted as one token including the `=`,
297
273
  # because the format word without its sigil is not the construct.
298
274
  # The payload is never a language, so it is never delegated.
299
- rule %r/#{bol}(#{MARGIN})(`{3,}|~{3,})([ \t]*)(=[a-zA-Z][\w+.-]*)([^\n]*)/ do |m|
275
+ rule %r/#{bol}(#{MARGIN})(`{3,})([ \t]*)(=[a-zA-Z][\w+.-]*)([^\n]*)/ do |m|
300
276
  groups Text, Punctuation, Text, Keyword::Type
301
- token Text, m[5]
277
+ lex_info_string(m[5])
302
278
  push_fence(m[2], nil)
303
279
  end
304
280
 
@@ -306,16 +282,25 @@ module Rouge
306
282
  # and a lexer for it highlights the payload - the one place this
307
283
  # lexer looks past Carve, because a reader of a `.crv` file expects
308
284
  # an embedded PHP sample to look like PHP.
309
- rule %r/#{bol}(#{MARGIN})(`{3,}|~{3,})([ \t]*)([a-zA-Z][\w+#.-]*)?([^\n]*)/ do |m|
285
+ rule %r/#{bol}(#{MARGIN})(`{3,})([ \t]*)([a-zA-Z][\w+#.-]*)?([^\n]*)/ do |m|
310
286
  groups Text, Punctuation, Text, Name::Builtin
311
287
  lex_info_string(m[5])
312
288
  push_fence(m[2], m[4])
313
289
  end
314
290
 
291
+ # The leaf extension line `::name[label]{attrs}`, a block of one
292
+ # line (C2-B-238).
293
+ rule %r/#{bol}(#{MARGIN})(::)(#{EXT_NAME})(\[)(?=#{LEAF_REST})/ do
294
+ groups Text, Punctuation, Name::Function, Punctuation
295
+ open_block :leaftail
296
+ push :"rolebody_#{bracket_ahead? || :paragraph}"
297
+ end
298
+
315
299
  # Container divs. A reserved kind word (note, tip, figure, ...)
316
300
  # names a known container; `:::` followed by `|` is the layout form.
317
- rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)(\|)/ do
301
+ rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)([|>\\])([^\n]*)/ do |m|
318
302
  groups Text, Punctuation, Text, Operator
303
+ lex_info_string(m[5])
319
304
  open_block
320
305
  end
321
306
  rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)([a-zA-Z][\w-]*)?([^\n]*)/ do |m|
@@ -325,20 +310,22 @@ module Rouge
325
310
  end
326
311
 
327
312
  # A caption line attaches to the block above or below it.
328
- rule %r/#{bol}(#{MARGIN})(\^)( +)(?=[ \t]*[^ \t\n])/ do
313
+ rule %r/(?<!\n\n)#{bol}(#{MARGIN})(\^)( +)(?=[ \t]*[^ \t\n])/ do |m|
314
+ @label_indent = m[1].size
329
315
  groups Text, Punctuation, Text
330
316
  open_block :caption
331
317
  end
332
318
 
333
319
  # Headings. Carve has no setext form, so a `#` run is the only
334
320
  # spelling and a trailing `{...}` is NOT an attribute block here.
335
- rule %r/#{bol}(#{MARGIN})(\#{1,6})( +)(?=[ \t]*[^ \t\n])/ do
321
+ rule %r/#{bol}(#{MARGIN})((?:\#{1,6}|={1,6}))( )(?=[^ \t\n])/ do |m|
322
+ @label_indent = m[1].size
336
323
  groups Text, Punctuation, Text
337
324
  open_block :heading
338
325
  end
339
326
 
340
327
  # Thematic breaks, before the list rules so `---` is not a bullet.
341
- rule %r/#{bol}#{MARGIN}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/ do
328
+ rule %r/#{bol}#{MARGIN}\*{3,}[ \t]*$/ do
342
329
  token Punctuation
343
330
  open_block
344
331
  end
@@ -367,14 +354,6 @@ module Rouge
367
354
  remember_host(:term)
368
355
  open_block :dlterm, :heading
369
356
  end
370
- rule %r/#{bol}#{root_margin}(:)( +[ \t]*)(\+)()$#{PLUS_NEXT}/ do
371
- if definition_term_before?
372
- groups Text, Punctuation, Text, Operator, Text
373
- else
374
- groups Text, Punctuation, Text, Text, Text
375
- end
376
- open_block :listitem, :quoteline
377
- end
378
357
  # Without a term above, `: b` is paragraph text (a lazy line too).
379
358
  rule %r/#{bol}(#{MARGIN})(:)(?= +[ \t]*[^ \t\n])/ do
380
359
  # the scan from the last definition is short; the term scan runs once
@@ -409,30 +388,6 @@ module Rouge
409
388
  end
410
389
  end
411
390
 
412
- # A task can attach its first block on the marker line.
413
- rule %r/#{bol}#{root_margin}([-*](?:#{ATTRS})?)( +[ \t]*)(\[[^\]\n]\])( +[ \t]*)(\+)()$#{PLUS_NEXT}/ do
414
- groups Text, Punctuation, Text, Name::Constant, Text, Operator, Text
415
- open_block :listitem, :quoteline
416
- end
417
-
418
- rule %r/#{bol}#{root_margin}(#{OPENER_RUN})(\+)()$#{PLUS_NEXT}/ do |m|
419
- token Text, m[1]
420
- scanner = StringScanner.new(m[2])
421
- until scanner.eos?
422
- if (marker = scanner.scan(/(?:#{ORDERED}|\.)(?:#{ATTRS})?/))
423
- token Num::Integer, marker
424
- elsif (marker = scanner.scan(/[-*](?:#{ATTRS})?|>/))
425
- token Punctuation, marker
426
- else
427
- token Text, scanner.getch
428
- end
429
- end
430
- depth = quote_depth_on_line
431
- token(state?(:quotestart) && !quote_level_follows?(depth) ? Text : Operator, m[3])
432
- token Text, m[4]
433
- m[2].rstrip.end_with?('>') ? open_block(:quoteline) : open_block(:listitem, :quoteline)
434
- end
435
-
436
391
  # A quote opened on a list item's marker line (`- > q`): its `>` is a
437
392
  # marker, and a `+` behind it may be the quote's first block
438
393
  # (markup-carve/carve#3421).
@@ -452,8 +407,7 @@ module Rouge
452
407
  end
453
408
  if m[5]
454
409
  token Text, m[4]
455
- # CarveContinuation settles whether this `+` is the marker.
456
- token Operator, m[5]
410
+ token Text, m[5]
457
411
  end
458
412
  open_block(:quoteline)
459
413
  end
@@ -493,7 +447,7 @@ module Rouge
493
447
  braced_cell(m)
494
448
  open_block :tablerow
495
449
  end
496
- rule %r/#{bol}(#{MARGIN})(\|=[<>^v~]*)#{ROW_END}/ do
450
+ rule %r/#{bol}(#{MARGIN})(\|=)#{ROW_END}/ do
497
451
  groups Text, Operator
498
452
  open_block :tablerow
499
453
  end
@@ -508,6 +462,9 @@ module Rouge
508
462
  state(:quoteopeners, &block_openers(''))
509
463
 
510
464
  state :block do
465
+ rule %r/^(#{MARGIN})(%%[ \t]*\#(?:region[ \t]+[A-Za-z_][\w-]*|endregion(?:[ \t]+[A-Za-z_][\w-]*)?))([ \t]*)$/ do
466
+ groups Text, Comment::Special, Text
467
+ end
511
468
  # A comment fence (%%% or longer) versus a one-line comment. The fence
512
469
  # has to be tried first: %%% also matches the one-line form. Only a
513
470
  # run of the SAME length closes it, so the width is closed over the
@@ -515,7 +472,7 @@ module Rouge
515
472
  # ends at a line left of its opener, which misses the opener's owner,
516
473
  # or at the end of the document. Text after either run is comment.
517
474
  rule %r/^(#{MARGIN})(%%%+)([^\n]*)(\n)/ do |m|
518
- closer = %r/^([ \t]*)(%{#{m[2].length}})(?!%)([^\n]*)$/
475
+ closer = %r/^([ \t]*)(%{#{m[2].length},})([ \t]*)$/
519
476
  outside = m[1].empty? ? nil : %r/^(?=[ \t]{0,#{m[1].length - 1}}[^ \t\n])/
520
477
 
521
478
  groups Text, Comment::Preproc, Comment, Text
@@ -533,27 +490,45 @@ module Rouge
533
490
  groups Text, Comment::Preproc, Comment
534
491
  end
535
492
 
536
- rule %r/^(\+)()$#{PLUS_NEXT}/ do |m|
537
- if continuation_host_before?
538
- groups Operator, Text
539
- else
540
- token Text, m[0]
541
- push :paragraph
542
- end
493
+ mixin :blockopeners
494
+
495
+ # A typed metadata block after line 1 (C2-B-164, C2-B-232): a column-0
496
+ # opener below a blank line or an attribute line. The line above is
497
+ # read in #typed_opener?, since a rule cannot look behind.
498
+ rule %r/^(?=#{TYPED_OPENER})/ do
499
+ typed_opener? ? push(:typedopener) : start_paragraph
543
500
  end
544
501
 
545
- mixin :blockopeners
502
+ # A snippet definition is a one-line block at column 0. Inside a `:::`
503
+ # div it is text, which a per-line rule cannot see.
504
+ rule SNIPPET_DEF do |m|
505
+ token Punctuation, m[1]
506
+ token Name::Variable, m[2]
507
+ token Name::Attribute, m[3] unless m[3].empty?
508
+ token Punctuation, m[4]
509
+ token Name::Attribute, m[5] unless m[5].empty?
510
+ token Punctuation, m[6]
511
+ token Text, m[7]
512
+ push :defline
513
+ end
514
+
515
+ # A use alone on its line is a block of one line, so the line below
516
+ # starts a new block.
517
+ rule %r/^(#{MARGIN})#{SNIPPET_BLOCK}/ do
518
+ groups Text, Punctuation, Text, Name::Variable, Text, Punctuation
519
+ end
520
+
521
+ # A lone snippet use takes no attribute line, so the line above it is
522
+ # a paragraph of its own (carve#6896).
523
+ rule %r/^#{MARGIN}(?:#{ATTRS})+[ \t]*(?=\n#{MARGIN}#{SNIPPET_BLOCK})/, Text
546
524
 
547
525
  # A standalone attribute block, which may span lines indented past its
548
526
  # `{` (carve#5080). One that does not is a paragraph.
549
- rule %r/^(#{MARGIN})(#{ATTRS_LINE})/ do |m|
550
- width = m[1].length
551
- if m[2].split("\n").drop(1).all? { |line| line[/\A[ \t]*/].length > width }
552
- groups Text, Name::Attribute
553
- else
554
- @current_stream.unscan
555
- start_paragraph
556
- end
527
+ rule %r/^(#{MARGIN})(#{ATTRS})(?=[ \t]*\n(?![ \t]*\n)[ \t]*\S)/ do
528
+ groups Text, Name::Attribute
529
+ end
530
+ rule %r/^#{MARGIN}#{ATTRS}(?=[ \t]*(?:\n[ \t]*\n|\z))/ do |m|
531
+ token Text, m[0]
557
532
  end
558
533
 
559
534
  # Any other line with content starts a paragraph.
@@ -574,9 +549,6 @@ module Rouge
574
549
 
575
550
  state :paraline do
576
551
  rule(%r/^(?=[ \t]*(?:\n|\z))/) { pop! }
577
- rule(%r/^(?=\+$#{PLUS_NEXT})/) do
578
- continuation_host_before? ? pop! : continue_paragraph(:paragraph)
579
- end
580
552
  rule(%r/^(?=#{MARGIN}(?:#{INTERRUPT}))/) { pop! }
581
553
  rule(%r/^(?=#{MARGIN}\^[ \t])/) { @lone_image ? pop! : continue_paragraph(:paragraph) }
582
554
  # An indented paragraph sits in a container, so a list marker below it
@@ -591,7 +563,7 @@ module Rouge
591
563
  line = :"#{name}line"
592
564
  state name do
593
565
  if name == :dlterm
594
- rule(/\n/) { token Text; pop! }
566
+ rule(/\n/) { token Text; goto :dltermline }
595
567
  else
596
568
  rule(/\n/) { token Text; goto line }
597
569
  end
@@ -603,17 +575,16 @@ module Rouge
603
575
 
604
576
  state line do
605
577
  rule(%r/^(?=[ \t]*(?:\n|\z))/) { pop! }
606
- if name == :listitem
607
- rule %r/^(\+)()$#{PLUS_NEXT}/ do
608
- groups Operator, Text
609
- goto name
610
- end
611
- end
612
578
  rule(%r/^(?=#{MARGIN}(?:#{INTERRUPT}|#{LIST_MARKER}|:[ \t]))/) { pop! }
613
579
  rule(//) { goto name }
614
580
  end
615
581
  end
616
582
 
583
+ state :dltermline do
584
+ rule(%r/^(?=[ \t]*(?:\n|\z)|#{MARGIN}(?:::? [^\n]|#{INTERRUPT}))/) { pop! }
585
+ rule(//) { goto :dlterm }
586
+ end
587
+
617
588
  # Right after a blockquote marker: a block opener, or quoted text.
618
589
  state :quotestart do
619
590
  mixin :quoteopeners
@@ -636,7 +607,25 @@ module Rouge
636
607
  rule %r/[^\n]/, Text
637
608
  end
638
609
 
610
+ # A label continues on each line directly below it that is indented
611
+ # past its marker (C2-B-047).
612
+ LABEL_LINE = /\n(?=([ \t]+)[^ \t\n])/.freeze
613
+
614
+ state :typedopener do
615
+ rule TYPED_OPENER do
616
+ groups Punctuation, Text, Keyword::Type, Text
617
+ goto :frontmatter
618
+ end
619
+ end
620
+
621
+ state :leaftail do
622
+ rule %r/$/, Text, :pop!
623
+ rule ATTRS, Name::Attribute
624
+ rule %r/[ \t]+/, Text
625
+ end
626
+
639
627
  state :heading do
628
+ rule(LABEL_LINE) { |m| continue_label(m) }
640
629
  rule %r/$/, Text, :pop!
641
630
  mixin :lineverbatim
642
631
  mixin :inlinecontent
@@ -653,7 +642,9 @@ module Rouge
653
642
  end
654
643
 
655
644
  state :caption do
656
- rule %r/$/, Text, :pop!
645
+ rule(LABEL_LINE) { |m| continue_label(m) }
646
+ rule %r/$(?!\n[ \t]*\^[ \t])/, Text, :pop!
647
+ rule %r/\n[ \t]*\^[ \t]/, Generic::Subheading
657
648
  mixin :lineverbatim
658
649
  mixin :inlinecontent
659
650
  rule PLAIN, Generic::Subheading
@@ -667,10 +658,7 @@ module Rouge
667
658
  # An escaped pipe is consumed by the escape rule first, so a `|` here
668
659
  # always ends a cell.
669
660
  rule(%r/(\|=?)#{CELL_BRACE}/) { |m| braced_cell(m) }
670
- rule %r/\|=[<>^v~]*/, Operator
671
- # A separator row cell, in the native and the GFM spelling.
672
- rule %r/(?<=\|)[ \t]*:?-{2,}:?[ \t]*(?=\|)/, Punctuation
673
- rule %r/(?<=\|)[ \t]*[<>^v~]{1,2}[ \t]*(?=\|)/, Operator
661
+ rule %r/\|=/, Operator
674
662
  rule %r/\|/, Punctuation
675
663
  mixin :inlinecontent
676
664
  rule PLAIN_NO_PIPE, Text
@@ -738,6 +726,13 @@ module Rouge
738
726
  groups Punctuation, Str::Backtick, Punctuation
739
727
  end
740
728
 
729
+ # A snippet use, before the include directive, which would read its
730
+ # name as a path. Attribute blocks glued to it are text (C2-I-128).
731
+ rule %r/#{SNIPPET_USE}((?:#{ATTRS})*)/ do |m|
732
+ groups Punctuation, Text, Name::Variable, Text, Punctuation
733
+ token plain_token, m[6] unless m[6].empty?
734
+ end
735
+
741
736
  # The reserved include directive `{{ path #section @key:value }}`
742
737
  # (PART 9 section 19, grammar.ebnf `include_directive`). The core
743
738
  # leaves it literal; a processor expands it only when a host supplies a
@@ -767,12 +762,6 @@ module Rouge
767
762
  #
768
763
  # CriticMarkup substitution and forced strike share `{~`; the first of
769
764
  # `~>` and `~}` decides which one it is.
770
- rule %r/\{(?=~(?!~\}))/ do
771
- next token(plain_token) unless closer_ahead?(:tilde, /~\}/)
772
-
773
- enter_braced(:tildebrace)
774
- end
775
-
776
765
  # A CriticMarkup comment. Without its closer, `{#id}` is an attribute
777
766
  # block.
778
767
  # The scanner's match is read before #check replaces it.
@@ -862,13 +851,15 @@ module Rouge
862
851
  groups Punctuation, Name::Namespace, Punctuation
863
852
  end
864
853
  # An autolink.
865
- rule %r/(<)([a-zA-Z][\w+.-]*:[^>\s]+|[^>\s@]+@[^>\s]+)(>)/ do
854
+ rule %r/(<)([a-zA-Z][\w+.-]*:[^<>\s]+|[^<>\s@]+@[^<>\s]+)(>)/ do
866
855
  groups Punctuation, Name::Tag, Punctuation
867
856
  end
868
857
 
869
858
  # The `:name[...]` extension form.
870
- rule %r/(:)([a-zA-Z][\w-]*)(\[)/ do
871
- if (scope = bracket_ahead?)
859
+ # A colon directly behind another one starts none, unless that one is
860
+ # escaped (C2-I-129); #after_colon? reads the character before.
861
+ rule %r/(:)(#{EXT_NAME})(\[)/ do
862
+ if !after_colon? && (scope = bracket_ahead?)
872
863
  groups Punctuation, Name::Function, Punctuation
873
864
  push :"rolebody_#{scope}"
874
865
  else
@@ -888,7 +879,7 @@ module Rouge
888
879
  # decided in #open_bare, from the characters on both sides and from
889
880
  # whether a closer follows on the line; what it encloses is lexed as
890
881
  # inline content. `==>` is left whole for the typographic rule.
891
- rule %r/[*\/_~]|=(?!=>)/ do |m|
882
+ rule %r/[*\/]/ do |m|
892
883
  open_bare(m[0])
893
884
  end
894
885
 
@@ -1034,17 +1025,7 @@ module Rouge
1034
1025
  # it names is the continuation operator, every other `+` the states
1035
1026
  # gave Operator is text.
1036
1027
  def stream_tokens(str, &block)
1037
- markers = CarveContinuation.markers(str).to_h { |offset| [offset, true] }
1038
- candidates = CarveContinuation.candidates(str).to_h { |offset| [offset, true] }
1039
- pos = 0
1040
- super(str) do |tok, val|
1041
- if val.include?('+') && candidates.any?
1042
- retag_plus(tok, val, pos, markers, candidates, &block)
1043
- else
1044
- yield tok, val
1045
- end
1046
- pos += val.length
1047
- end
1028
+ super(str, &block)
1048
1029
  end
1049
1030
 
1050
1031
  private
@@ -1108,13 +1089,6 @@ module Rouge
1108
1089
 
1109
1090
  limit = enclosing_closer(pos)
1110
1091
 
1111
- if char == '/' && after == '*' && text[pos + 1].to_s.match?(/\S/) &&
1112
- closer_ahead?(:bolditalic, COMBINED_CLOSERS[:bolditalic], pos + 2, masked: :combined, before: limit)
1113
- token Punctuation
1114
- push :bolditalic
1115
- return push(:punctchar)
1116
- end
1117
-
1118
1092
  if char == '*' && after == '/' && !before.to_s.match?(/[\w*\/]/) &&
1119
1093
  text[pos + 1].to_s.match?(/\S/) && !(text[pos + 1] == '*' && text[pos + 2].to_s.match?(/[\s*]/)) &&
1120
1094
  closer_ahead?(:italicbold, COMBINED_CLOSERS[:italicbold], pos + 2, masked: :combined, before: limit)
@@ -1238,6 +1212,27 @@ module Rouge
1238
1212
  # escapes and code spans follows before that block ends; nil otherwise.
1239
1213
  # The brackets of one block are found in one pass and reused, so a run
1240
1214
  # of unclosed openers does not rescan it once each.
1215
+ def after_colon?
1216
+ text = @current_stream.string
1217
+ at = @current_stream.charpos - @current_stream[0].length
1218
+ at.positive? && text[at - 1] == ':' && !(at > 1 && text[at - 2] == '\\')
1219
+ end
1220
+
1221
+ def typed_opener?
1222
+ text = @current_stream.string
1223
+ at = @current_stream.charpos
1224
+ return false unless at > 1 && text[at - 1] == "\n"
1225
+ return true if text[at - 2] == "\n"
1226
+
1227
+ start = text.rindex("\n", at - 2)
1228
+ text[(start ? start + 1 : 0)...(at - 1)].match?(ATTRS_ONLY)
1229
+ end
1230
+
1231
+ def continue_label(match)
1232
+ token Text, match[0]
1233
+ pop! unless match[1].size > @label_indent.to_i
1234
+ end
1235
+
1241
1236
  def bracket_ahead?
1242
1237
  stream = @current_stream
1243
1238
  pos = stream.charpos
@@ -1543,7 +1538,9 @@ module Rouge
1543
1538
  def push_fence(opener, language)
1544
1539
  char = Regexp.escape(opener[0])
1545
1540
  width = opener.length
1546
- closer = %r/^[ \t]*#{char}{#{width},}[ \t]*$/
1541
+ line_start = (@current_stream.string.rindex("\n", @current_stream.charpos - 1) || -1) + 1
1542
+ margin = @current_stream.string[line_start...@current_stream.charpos][/^[ \t]*/]
1543
+ closer = %r/^#{Regexp.escape(margin)}#{char}{#{width},}[ \t]*$/
1547
1544
 
1548
1545
  sublexer = fence_lexer(language)
1549
1546
  sublexer&.reset!
@@ -3,5 +3,5 @@
3
3
  # Kept apart from the entry file so the gemspec can read it without loading
4
4
  # Rouge, which is not installed yet when Bundler evaluates the gemspec.
5
5
  module RougeCarve
6
- VERSION = '0.1.5'
6
+ VERSION = '0.2.0'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rouge-carve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Scherer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-12 00:00:00.000000000 Z
11
+ date: 2026-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rouge
@@ -47,7 +47,6 @@ files:
47
47
  - lib/rouge-carve/version.rb
48
48
  - lib/rouge/demos/carve
49
49
  - lib/rouge/lexers/carve.rb
50
- - lib/rouge/lexers/carve_continuation.rb
51
50
  homepage: https://github.com/markup-carve/rouge-carve
52
51
  licenses:
53
52
  - MIT
@@ -1,357 +0,0 @@
1
- # -*- coding: utf-8 -*- #
2
- # frozen_string_literal: true
3
-
4
- module Rouge
5
- module Lexers
6
- # Decides which `+` characters of a Carve document are continuation
7
- # markers (Part 9 §17 L3, CARVE-P9-031). The marker is relative: it sits at
8
- # an open container's marker column, so the answer needs the container
9
- # stack the line-based lexer states do not carry. This pass walks the
10
- # document once and returns the character offsets of the markers.
11
- #
12
- # It is a reduced block model: list items, quotes, footnotes, definitions
13
- # and fences, with lazy paragraph lines. Shapes it cannot place (tables,
14
- # divs, attribute blocks) read as text.
15
- class CarveContinuation
16
- TAB = 4
17
- ORDERED = /\A(?:\d+|[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)[.)]/.freeze
18
- BULLET = /\A[-*](?=[ \t]|\z)/.freeze
19
- FENCE = /\A(`{3,}|~{3,})/.freeze
20
-
21
- Container = Struct.new(:kind, :col, :content, :quotes, keyword_init: true)
22
-
23
- # A line whose only content after quote prefixes and openers is `+`:
24
- # the shapes where the `+` is a marker or a refused marker.
25
- CANDIDATE = /
26
- \A(?:[ \t]|>(?=[ \t])|(?:[-*]|(?:\d+|[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)[.)])(?:\{[^}\n]*\})?(?=[ \t])
27
- |\[[^\]\n]\](?=[ \t])|::?(?=[ \t])|\[\^[^\]\n]+\]:(?=[ ]))*\+[ \t]*\r?\z
28
- /x.freeze
29
-
30
- def self.markers(source)
31
- new(source).markers
32
- end
33
-
34
- # Offsets of every `+` that stands where a marker could.
35
- def self.candidates(source)
36
- offset = 0
37
- source.split("\n", -1).each_with_object([]) do |line, found|
38
- found << offset + line.rindex('+') if line.match?(CANDIDATE)
39
- offset += line.length + 1
40
- end
41
- end
42
-
43
- def initialize(source)
44
- @lines = source.split("\n", -1)
45
- @markers = []
46
- end
47
-
48
- def markers
49
- stack = []
50
- fence = nil
51
- para = nil
52
- force = 0
53
- offset = 0
54
- blank = false
55
- @lines.each_with_index do |raw, index|
56
- line = raw.chomp("\r")
57
- cols = columns(line)
58
- matched, pos, quotes = match(stack, line, cols, force)
59
- rest = line[pos..] || ''
60
-
61
- if fence && (fence[:forced] || matched >= fence[:depth])
62
- fence = nil if rest.match?(/\A[ \t]*#{Regexp.escape(fence[:char])}{#{fence[:width]},}[ \t]*\z/)
63
- offset += raw.length + 1
64
- force = 0
65
- next
66
- end
67
- fence = nil
68
-
69
- lone = rest.match(/\A([ \t]*)\+[ \t]*\z/)
70
- attached = 0
71
- if rest.match?(/\A[ \t]*\z/)
72
- # a blank line ends every quote it does not prefix
73
- stack.slice!(matched..)
74
- para = nil
75
- elsif lone
76
- col = cols[pos + lone[1].length]
77
- # A blank line above closes a description or a footnote for a `+`.
78
- target = stack.rindex do |c|
79
- c.col == col && c.quotes == quotes && c.kind != :term &&
80
- !(blank && %i[definition note].include?(c.kind))
81
- end
82
- # Only an item refuses a list below it (the sibling rule).
83
- if target && follower?(index + 1, quotes, col, quote: quotes.zero?,
84
- list: stack[target].kind != :item)
85
- @markers << offset + pos + lone[1].length
86
- stack.slice!((target + 1)..)
87
- attached = stack.length
88
- para = nil
89
- else
90
- stack.slice!(matched..) unless para
91
- para ||= stack.length
92
- end
93
- elsif para && (!opener?(rest) || lazy_list?(stack, rest, quotes) || unclosed_fence?(rest, index))
94
- # a lazy line keeps every container open
95
- else
96
- stack.slice!(matched..)
97
- para, fence, plus = open_blocks(stack, line, cols, pos, quotes, para, index)
98
- fence[:forced] = true if fence && force.positive?
99
- if plus
100
- @markers << offset + plus
101
- attached = stack.length
102
- end
103
- end
104
- force = attached
105
- blank = rest.match?(/\A[ \t]*\z/)
106
- offset += raw.length + 1
107
- end
108
- @markers
109
- end
110
-
111
- private
112
-
113
- # The visual column of every character position, tabs expanded.
114
- def columns(line)
115
- cols = []
116
- col = 0
117
- line.each_char do |ch|
118
- cols << col
119
- col = ch == "\t" ? (col / TAB + 1) * TAB : col + 1
120
- end
121
- cols << col
122
- cols
123
- end
124
-
125
- # How many containers the line continues, where its content starts, and
126
- # how many quote prefixes it passed.
127
- # The containers up to +force+ are the ones an attached line enters.
128
- def match(stack, line, cols, force)
129
- pos = 0
130
- quotes = 0
131
- matched = 0
132
- stack.each do |c|
133
- blank = line[pos..].to_s.match?(/\A[ \t]*\z/)
134
- if c.kind == :quote
135
- ws = line[pos..][/\A[ \t]*/]
136
- at = pos + ws.length
137
- unless line[at] == '>' && (line[at + 1].nil? || line[at + 1].match?(/[ \t]/))
138
- break unless matched < force
139
-
140
- matched += 1
141
- next
142
- end
143
-
144
- pos = at + 1
145
- pos += 1 if line[pos]&.match?(/[ \t]/)
146
- quotes += 1
147
- elsif blank
148
- # a blank line keeps an item open
149
- else
150
- ws = line[pos..][/\A[ \t]*/]
151
- if cols[pos + ws.length] >= c.content || matched < force
152
- pos = advance(line, cols, pos, c.content)
153
- else
154
- break
155
- end
156
- end
157
- matched += 1
158
- end
159
- [matched, pos, quotes]
160
- end
161
-
162
- # Move +pos+ over whitespace up to +col+.
163
- def advance(line, cols, pos, col)
164
- pos += 1 while line[pos]&.match?(/[ \t]/) && cols[pos + 1] <= col
165
- pos
166
- end
167
-
168
- def opener?(rest)
169
- text = rest.lstrip
170
- text.match?(BULLET) || text.match?(ORDERED) && text.match?(/\A\S+[ \t]/) ||
171
- text.start_with?('>', '|') || text.match?(FENCE) ||
172
- text.match?(/\A(?:\#{1,6}[ \t]|:::|::?[ \t]|\[\^[^\]]+\]:[ ])/)
173
- end
174
-
175
- # A list marker cannot interrupt a paragraph unless it continues a list
176
- # already open in the line's own quote context.
177
- def lazy_list?(stack, rest, quotes)
178
- text = rest.lstrip
179
- return false unless list_marker?(text)
180
-
181
- stack.none? { |c| c.kind == :item && c.quotes == quotes }
182
- end
183
-
184
- # Only a closed fence interrupts a paragraph.
185
- def unclosed_fence?(rest, index)
186
- f = rest.lstrip.match(FENCE)
187
- return false unless f
188
-
189
- @last_closer ||= {}
190
- last = @last_closer[f[1]] ||= begin
191
- closer = /\A[ \t>]*#{Regexp.escape(f[1][0])}{#{f[1].length},}[ \t]*\r?\z/
192
- @lines.rindex { |l| l.match?(closer) } || -1
193
- end
194
- last <= index
195
- end
196
-
197
- def list_marker?(text)
198
- text.match?(BULLET) || text.match?(/#{ORDERED.source}(?=[ \t]|\z)/)
199
- end
200
-
201
- # The block a line at +col+ opens is the one the marker attaches.
202
- # Inside a quote, `> +` does not attach a quote right after the prefix.
203
- def follower?(index, quotes, col, quote: true, list: false)
204
- line = @lines[index]
205
- return false if line.nil?
206
-
207
- line = line.chomp("\r")
208
- cols = columns(line)
209
- pos = 0
210
- quotes.times do
211
- ws = line[pos..][/\A[ \t]*/]
212
- return false unless line[pos + ws.length] == '>'
213
-
214
- pos += ws.length + 1
215
- pos += 1 if line[pos]&.match?(/[ \t]/)
216
- end
217
- ws = line[pos..][/\A[ \t]*/]
218
- text = line[(pos + ws.length)..]
219
- return false if text.nil? || text.empty?
220
- return false unless cols[pos + ws.length] == col
221
- return false if !quote && ws.empty? && text.start_with?('>')
222
-
223
- list || !list_marker?(text)
224
- end
225
-
226
- # Push the containers a line opens. Returns the paragraph depth, an open
227
- # fence and the offset of a first-block marker.
228
- def open_blocks(stack, line, cols, pos, quotes, para, index)
229
- base = pos
230
- start = stack.length
231
- first = true
232
- last = nil
233
- loop do
234
- ws = line[pos..][/\A[ \t]*/]
235
- at = pos + ws.length
236
- text = line[at..]
237
- col = cols[at]
238
- # `>+`, `>>+` and `>> +` open no quote: each `>` needs a space after it.
239
- if text.start_with?('>') && (text[1].nil? || text[1] == ' ')
240
- break if first && at != base
241
-
242
- stack << Container.new(kind: :quote, col: col, content: col + 2, quotes: quotes)
243
- quotes += 1
244
- pos = at + 1
245
- pos += 1 if line[pos] == ' '
246
- last = :quote
247
- elsif (m = text.match(/\A(?:[-*]|#{ORDERED.source})(?:\{[^}\n]*\})?(?= |\z)/))
248
-
249
- after = at + m[0].length
250
- gap = line[after..][/\A[ \t]*/]
251
- content = gap.empty? || cols[after + gap.length] - cols[after] > 4 ? cols[after] + 1 : cols[after + gap.length]
252
- # attributes glued to the marker take no columns
253
- content -= cols[after] - cols[at + m[0].index('{').to_i] if m[0].include?('{')
254
- stack << Container.new(kind: :item, col: col, content: content, quotes: quotes)
255
- pos = after + gap.length
256
- last = m[0].start_with?('-', '*') ? :bullet : :ordered
257
- elsif last == :bullet && (m = text.match(/\A\[[^\]\n]\](?= )/))
258
- pos = at + m[0].length
259
- pos += line[pos..][/\A[ \t]*/].length
260
- last = :task
261
- elsif (first || last == :quote) && text.match?(/\A\[\^[^\]\n]+\]: /)
262
- m = text.match(/\A\[\^[^\]\n]+\]: /)
263
- stack << Container.new(kind: :note, col: col, content: col + 2, quotes: quotes)
264
- pos = at + m[0].length
265
- last = :note
266
- elsif (first || last == :quote) && text.match?(/\A:: /)
267
- @term_open = stack.length
268
- stack << Container.new(kind: :term, col: col, content: col + 2, quotes: quotes)
269
- return [nil, nil, nil]
270
- elsif (first || last == :quote) && @term_open == stack.length && text.match?(/\A: /)
271
- stack << Container.new(kind: :definition, col: col, content: col + 2, quotes: quotes)
272
- pos = at + 2
273
- last = :definition
274
- else
275
- break
276
- end
277
- first = false
278
- end
279
-
280
- ws = line[pos..][/\A[ \t]*/]
281
- text = line[(pos + ws.length)..] || ''
282
- @term_open = last == :definition ? stack.length - 1 : nil
283
- if last && text.match?(/\A\+[ \t]*\z/)
284
- case last
285
- when :quote
286
- hosted = (start...stack.length).find { |i| stack[i].kind == :item }
287
- spaced = !hosted || !line[base...pos].match?(/\S[ \t]{2,}/)
288
- return [nil, nil, pos] if spaced && ws.empty? && first_block_follower?(index + 1, stack, cols[base], hosted, start)
289
- when :note
290
- return [nil, nil, pos + ws.length] if first_block_follower?(index + 1, stack, cols[base])
291
- else
292
- return [nil, nil, pos + ws.length]
293
- end
294
- end
295
- return [nil, nil, nil] if text.empty?
296
-
297
- # at document level a fence opens at column 0 only
298
- if (f = text.match(FENCE)) && (stack.any? || ws.empty? || pos != base)
299
- return [nil, { char: f[1][0], width: f[1].length, depth: stack.length }, nil]
300
- end
301
-
302
- [stack.length, nil, nil]
303
- end
304
-
305
- # A first-block marker after a quote or a footnote attaches a non-list
306
- # block at document column 0 or at the column where the line's openers
307
- # began; right after an enclosing quote's prefix counts as column 0 of
308
- # that quote. A line that re-enters the opened container is its text.
309
- # A quote opened behind a list item on the same line (+hosted+, the
310
- # stack index of the first item the line opens) reads the follower at
311
- # its raw column: the containers from that item on are not entered, and
312
- # the item's and the owning quote's columns are eligible too
313
- # (markup-carve/carve#3421, corpus 649 to 656). Its markers are one space
314
- # apart: `- > +` is text.
315
- def first_block_follower?(index, stack, base_col, hosted = nil, start = nil)
316
- line = @lines[index]
317
- return false if line.nil?
318
-
319
- line = line.chomp("\r")
320
- cols = columns(line)
321
- pos = 0
322
- reached = 0
323
- after_quote = true
324
- (hosted ? stack[0...hosted] : stack).each do |c|
325
- ws = line[pos..][/\A[ \t]*/]
326
- at = pos + ws.length
327
- if c.kind == :quote
328
- break unless line[at] == '>' && (line[at + 1].nil? || line[at + 1] == ' ')
329
-
330
- pos = at + 1
331
- pos += 1 if line[pos] == ' '
332
- after_quote = true
333
- else
334
- break unless line[at] && cols[at] >= c.content
335
-
336
- pos = advance(line, cols, pos, c.content)
337
- after_quote = false
338
- end
339
- reached += 1
340
- end
341
- return false if !hosted && reached == stack.length
342
-
343
- ws = line[pos..][/\A[ \t]*/]
344
- at = pos + ws.length
345
- text = line[at..] || ''
346
- return false if text.empty? || list_marker?(text)
347
- return false if hosted && text.match?(/\A\+[ \t]*\z/)
348
-
349
- # Stacked quotes opened on one line (`> > +`) take the first one's column.
350
- own = hosted || start.nil? ? stack.last.col : stack[start].col
351
- return cols[at].zero? || cols[at] == own if reached.zero?
352
-
353
- cols[at] == base_col || (after_quote && ws.empty?) || (hosted && [stack[hosted].col, stack.last.col].include?(cols[at]))
354
- end
355
- end
356
- end
357
- end