rouge-carve 0.1.4 → 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: 300be9217acf879b361b092d5bd9f814858ba169272f98f730cdbfa86a2e75bf
4
- data.tar.gz: 1b4e02989bd311b9d0423e1021250d5ddac1982420c15016d80600ddb82386b0
3
+ metadata.gz: 80e4e45c2edbbcdd8245b1b786804a5e5649e973512778a37f90f2225ecc4ca2
4
+ data.tar.gz: 4f961773751f3bfdf37213a8dec97a915508d3037c74c37fa452fadebbf644d3
5
5
  SHA512:
6
- metadata.gz: 63d049afb99f11ac50d7b64a2829c8fec7bc220b1501b621cbbe2e6a87160e75cc326bc94f3724f3280650e12e9be2ce0a00bb160f1c9012e717fc2d538916b9
7
- data.tar.gz: d3dce00b0304ec63f85dc9e258f78e7c516d7a9048e1ac9e76e8745140c2931a8ee3e0de70abc7a7a804203b511707d901f7ecd44c152ad08bdaeaa3e22dd35d
6
+ metadata.gz: 28d9a4b21800d3b75ac930e10ac21d1e8e68c723b66ce0a2022a9641809df0db5aa5241098db6a0631f73977ec7e8e8a6e592df73acdaef8bbc70eda49196604
7
+ data.tar.gz: 2cfe337bfcc63cf4646369c60cfa5e336804d976a0df5426c4ea0ab4135e5fcb77cf8f640b61db0171d8ce1b1402731531da9a2c9a058587f9d6fe80681f4f74
data/CHANGELOG.md CHANGED
@@ -2,6 +2,32 @@
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
+
22
+ ## [0.1.5] - 2026-11-12
23
+
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).
25
+ - `->`, `<-`, `<->` and `=>` are text, and `-->` and `<--` need their exact hyphen run (#38).
26
+ - A mention or tag name needs an ASCII letter (#38).
27
+ - A first-line `---` opens frontmatter through the end of the document when no closer follows; a mid-document `---` never does (#38).
28
+ - A letter or roman marker with a dot needs two spaces, and a definition term is one line (#38).
29
+ - A code fence opener and a `%%%` comment span open without a closer lookahead, and a multiline attribute block continues only on lines indented past its `{` (#38).
30
+
5
31
  ## [0.1.4] - 2026-10-23
6
32
 
7
33
  - `csv` and `tsv` fence bodies are highlighted: every field has its own token type and the separator is an operator, colored apart from the fields in the default styles (#25, #26, #27, #28).
data/README.md CHANGED
@@ -1,84 +1,10 @@
1
1
  # rouge-carve
2
2
 
3
- A [Rouge](https://github.com/rouge-ruby/rouge) lexer for the
4
- [Carve](https://markup-carve.github.io/carve/) markup language.
5
-
6
- ```bash
7
- gem install rouge-carve
8
- ```
3
+ Rouge lexer for Carve 0.2 syntax. It registers `carve` and `crv` for `.crv` and `.carve` files.
9
4
 
10
5
  ```ruby
11
6
  require 'rouge-carve'
12
-
13
- Rouge::Lexer.find('carve') # => Rouge::Lexers::Carve
14
- Rouge::Lexer.guess(filename: 'notes.crv') # => Rouge::Lexers::Carve
7
+ Rouge::Lexer.find('carve')
15
8
  ```
16
9
 
17
- It needs Rouge 3.30 or later and is tested against Rouge 3, 4 and 5.
18
-
19
- Requiring the gem registers the lexer. Anything that already highlights with
20
- Rouge - GitLab, Jekyll, Redcarpet, most Ruby static site generators - then
21
- handles `.crv` and `.carve` files, and ` ```carve ` fenced blocks, without
22
- further configuration.
23
-
24
- ## Why not just use the Markdown lexer
25
-
26
- Because it is not close enough to be wrong quietly. Carve deliberately changed
27
- the delimiters Markdown got backwards, and three of them **invert**:
28
-
29
- | Carve | means | Markdown reads it as |
30
- | --- | --- | --- |
31
- | `*bold*` | strong | emphasis |
32
- | `/italic/` | emphasis | literal slashes |
33
- | `_under_` | underline | emphasis |
34
- | `~strike~` | strikethrough | subscript, or literal |
35
- | `{=mark=}` | highlight | literal braces |
36
- | `{^sup^}` | superscript | literal braces |
37
-
38
- Highlighting a Carve document as Markdown does not degrade to plain text. It
39
- shows bold where the author wrote emphasis.
40
-
41
- ## What it covers
42
-
43
- Every construct in the language: headings, containers (`::: note`), fenced and
44
- raw blocks, tables with alignment and per-row attributes, definition lists,
45
- footnotes, citations, cross-references, attribute blocks, task items, critic
46
- markup, symbol shortcodes, mentions, tags and the typographic runs.
47
-
48
- **Fenced code is delegated.** A ` ```ruby ` block inside a `.crv` file is lexed
49
- as Ruby, because that is what a reader expects. A ` ```=html ` raw block is not:
50
- its info string names an output format, not a language, so the payload stays
51
- opaque.
52
- CSV and TSV fences highlight separators, quoted fields, escaped quotes, and unquoted fields (the header record apart from the data rows).
53
-
54
- **Fence widths are respected.** A three-backtick line inside a four-backtick
55
- fence is content, not a closer. The width is closed over per block rather than
56
- approximated, which the sibling grammars in other ecosystems cannot do.
57
-
58
- ## What it cannot say
59
-
60
- Rouge has no underline token - the vocabulary stops at `Generic::Emph`,
61
- `Generic::Strong` and `Generic::EmphStrong`. Carve's `_x_` is underline, so its
62
- content shares `Generic::Emph` with italic. The delimiters stay `Punctuation`,
63
- so a consumer can still tell the two apart by the delimiter each carries.
64
-
65
- Block openers are matched at any indent. Carve opens a block at column 0 or at
66
- an enclosing container's content column and nowhere in between, which needs a
67
- container model this lexer does not carry. The trade-off is deliberate and
68
- shared with the Prism, highlight.js and Pygments grammars, so the four agree:
69
- it over-colours an indented-at-document-level opener rather than
70
- under-colouring the far more common indented construct inside a list item.
71
-
72
- ## Related
73
-
74
- - [carve](https://github.com/markup-carve/carve) - the language and its spec
75
- - [pygments-carve](https://github.com/markup-carve/pygments-carve) - the same
76
- grammar for Pygments, which this was ported from
77
- - [carve-grammars](https://github.com/markup-carve/carve-grammars) - TextMate,
78
- Prism and highlight.js
79
- - [carve-css](https://github.com/markup-carve/carve-css) - styles for the HTML
80
- Carve renders
81
-
82
- ## Development
83
-
84
- Contributor setup, testing, and maintenance notes are in the [development guide](docs/development.md).
10
+ See the [Carve documentation](https://markup-carve.github.io/carve/) for source syntax.
@@ -21,13 +21,13 @@ A word with no registered handler renders as a generic div, which is what
21
21
  makes a whole family of constructs possible without touching the parser.
22
22
  :::
23
23
 
24
- ``` ruby "example.rb"
24
+ ```ruby "example.rb"
25
25
  def greet(name) = "Hello, #{name}!"
26
26
  ```
27
27
 
28
- |= Engine |= Status |
29
- | carve-js | shipped |
30
- | carve-rs | shipped |
28
+ |= Engine |={~} Status |
29
+ | carve-js | shipped |
30
+ | carve-rs | shipped |
31
31
 
32
32
  ^ Table 1: the reference implementations.
33
33
 
@@ -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
@@ -158,6 +131,10 @@ module Rouge
158
131
  |#{LINE_BREAK}
159
132
  )*\}
160
133
  /x.freeze
134
+ # The same block as an interrupting line, whose continuations must be
135
+ # indented (carve#5080). Exact at column 0; an indented block is checked
136
+ # against its own `{` column in the :block rule.
137
+ ATTRS_INTERRUPT = Regexp.new(ATTRS_LINE.source.gsub(LINE_BREAK, '\n(?=[ \t]+[^ \t\n])'), Regexp::EXTENDED)
161
138
 
162
139
  # The prefix of a line inside a blockquote, nested markers included.
163
140
  # Each `>` needs a space or the line end after it: `>foo` and `>> x`
@@ -177,45 +154,44 @@ module Rouge
177
154
  end
178
155
 
179
156
  # An ordered marker: a number, one letter, or a roman numeral. `Body.`
180
- # is a word, not a marker.
181
- ORDERED = '(?:\\d+|[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)[.)]'
182
-
183
- # The states read a column-0 `+` with a flush-left block below as an
184
- # attachment. Whether a `+` is the marker is settled by CarveContinuation.
185
- PLUS_NEXT = /(?=\n[^ \t\n])/.freeze
157
+ # is a word, not a marker, and a letter with `.` needs two spaces, so
158
+ # `J. R. R. Tolkien` is prose (carve#4811).
159
+ ORDERED = "(?:\\d{1,9}[.)]|(?:[A-Za-z]|[ivxlcdm]+|[IVXLCDM]+)(?:\\)|\\.(?=(?:#{ATTRS})? )))"
186
160
 
187
161
  # A list marker at the start of a line, as the list rules below read it.
188
162
  LIST_MARKER = "(?:[-*]|#{ORDERED}|\\.)(?:#{ATTRS})?(?:[ \\t]|$)"
189
163
  OPENER_RUN = "(?:(?:[-*]|#{ORDERED}|\\.)(?:#{ATTRS})? +[ \\t]*|> )+"
190
164
 
165
+ # A cell's alignment symbols: horizontal, then an optional vertical one.
166
+ CELL_ALIGN = '(?:[<>~][\\^~v]?|\\?[\\^~v])'
167
+ # A cell's attribute block when it opens with alignment (carve#5091 D3),
168
+ # glued to its `|` or `|=` and followed by the cell's space.
169
+ CELL_BRACE = %(({)(#{CELL_ALIGN})((?:[ ]+(?:"(?:[^"\\\\\n]|\\\\.)*"|'(?:[^'\\\\\n]|\\\\.)*'|[^{}"'|\n])*)?)(})(?= ))
170
+
191
171
  # A table row needs its closing pipe: `| not a table` is a paragraph.
192
172
  ROW_END = /(?=(?:\\.|[^\\\n])*\|[ \t]*(?:#{ATTRS})?[ \t]*$)/.freeze
193
173
 
194
174
  # The block openers that interrupt a paragraph. A list marker and a
195
- # caption do not, and a fence does only when it is closed - that one is
196
- # checked in code, see the :paraline state.
175
+ # caption do not. A fence opener does without looking for its closer
176
+ # (carve#4018); a backtick in a backtick info string makes it a code span.
197
177
  INTERRUPT = %r/
198
- \#{1,6}[ \t] | >(?:[ \t]|$) | ::: | ::[ \t] | %%
199
- | (?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$
200
- | \|=?[<>^v~]*#{ROW_END}
201
- | (?:\[\^|\*\[|\[)[^\]\n]+\]:[ ]
202
- | #{ATTRS_LINE}[ \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]*$
203
182
  /x.freeze
204
183
 
205
184
  # Where a bracketed inline body must close, by the block it sits in: a
206
185
  # paragraph ends at a blank line or an interrupting block, a list item
207
186
  # also at the next marker, and a heading or table row at its line end.
208
- # A fence ends either only when it is closed, which is checked in code.
209
187
  BODY_ENDS = {
210
188
  para: /\n(?=[ \t]*(?:\n|\z)|#{MARGIN}(?:#{INTERRUPT}))/,
211
189
  item: /\n(?=[ \t]*(?:\n|\z)|#{MARGIN}(?:#{INTERRUPT}|#{LIST_MARKER}|:[ \t]))/,
212
190
  line: /\n/
213
191
  }.freeze
214
- FENCE_NEXT = /\n(?=#{MARGIN}(`{3,}|~{3,}))/.freeze
215
-
216
192
  # The block states that decide which of those ends applies.
217
193
  BODY_SCOPE = {
218
- paragraph: :para, listitem: :item, dlterm: :item, heading: :line,
194
+ paragraph: :para, listitem: :item, dlterm: :line, heading: :line,
219
195
  caption: :line, quoteline: :line, tablerow: :line, defline: :line
220
196
  }.freeze
221
197
 
@@ -225,10 +201,22 @@ module Rouge
225
201
  #{MARGIN}(?:!#{LABEL}(?:\([^)\n]*\)|#{LABEL})?|\$\$(`+)[^\n]*?\1)(?:#{ATTRS})?[ \t]*$
226
202
  /x.freeze
227
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
+
228
215
  # The braced inline families: delimiter, content token, state.
229
216
  BRACED = {
230
217
  '*' => [Generic::Strong, :forcedstrong], '/' => [Generic::Emph, :forcedemph],
231
- '_' => [Generic::Emph, :forcedunderline], '^' => [Generic::Emph, :superscript],
218
+ '_' => [Generic::Emph, :forcedunderline], '~' => [Generic::Deleted, :forcedstrike],
219
+ '^' => [Generic::Emph, :superscript],
232
220
  ',' => [Generic::Emph, :subscript], '=' => [Generic::Inserted, :bracedmark],
233
221
  '+' => [Generic::Inserted, :insertion], '-' => [Generic::Deleted, :deletion]
234
222
  }.freeze
@@ -238,10 +226,7 @@ module Rouge
238
226
  # non-space before it and no letter or digit after it.
239
227
  BARE = {
240
228
  '*' => [:strong, Generic::Strong, /[\w*]/, /[\s*]/],
241
- '/' => [:emph, Generic::Emph, /[\w\/]/, /[\s\/]/],
242
- '_' => [:underline, Generic::Emph, /[\w_\/]/, /[\s_]/],
243
- '~' => [:strike, Generic::Deleted, /[\w~]/, /[\s~]/],
244
- '=' => [:mark, Generic::Inserted, /[\w=<>!]/, /[\s>=]/]
229
+ '/' => [:emph, Generic::Emph, /[\w\/]/, /[\s\/]/]
245
230
  }.freeze
246
231
 
247
232
  # Characters that can begin an inline construct. A run of anything else is
@@ -259,8 +244,15 @@ module Rouge
259
244
  PLAIN_NO_PIPE = plain_run('|').freeze
260
245
  PLAIN_NO_BRACKET = plain_run('\\]').freeze
261
246
 
247
+ # The document start. Front matter opens only here: Rouge 3 and 4 scan
248
+ # without a fixed anchor, so \A alone would also match mid-document.
262
249
  state :root do
263
- mixin :block
250
+ rule %r/\A(?)(---)(?:( ?)([A-Za-z0-9]+))?([ \t]*(?:\n|\z))/ do
251
+ groups Text, Punctuation, Text, Keyword::Type, Text
252
+ goto :block
253
+ push :frontmatter
254
+ end
255
+ rule(//) { goto :block }
264
256
  end
265
257
 
266
258
  # ------------------------------------------------------------------
@@ -280,9 +272,9 @@ module Rouge
280
272
  # output format verbatim. Emitted as one token including the `=`,
281
273
  # because the format word without its sigil is not the construct.
282
274
  # The payload is never a language, so it is never delegated.
283
- 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|
284
276
  groups Text, Punctuation, Text, Keyword::Type
285
- token Text, m[5]
277
+ lex_info_string(m[5])
286
278
  push_fence(m[2], nil)
287
279
  end
288
280
 
@@ -290,16 +282,25 @@ module Rouge
290
282
  # and a lexer for it highlights the payload - the one place this
291
283
  # lexer looks past Carve, because a reader of a `.crv` file expects
292
284
  # an embedded PHP sample to look like PHP.
293
- 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|
294
286
  groups Text, Punctuation, Text, Name::Builtin
295
287
  lex_info_string(m[5])
296
288
  push_fence(m[2], m[4])
297
289
  end
298
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
+
299
299
  # Container divs. A reserved kind word (note, tip, figure, ...)
300
300
  # names a known container; `:::` followed by `|` is the layout form.
301
- rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)(\|)/ do
301
+ rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)([|>\\])([^\n]*)/ do |m|
302
302
  groups Text, Punctuation, Text, Operator
303
+ lex_info_string(m[5])
303
304
  open_block
304
305
  end
305
306
  rule %r/#{bol}(#{MARGIN})(:{3,})([ \t]*)([a-zA-Z][\w-]*)?([^\n]*)/ do |m|
@@ -309,20 +310,22 @@ module Rouge
309
310
  end
310
311
 
311
312
  # A caption line attaches to the block above or below it.
312
- 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
313
315
  groups Text, Punctuation, Text
314
316
  open_block :caption
315
317
  end
316
318
 
317
319
  # Headings. Carve has no setext form, so a `#` run is the only
318
320
  # spelling and a trailing `{...}` is NOT an attribute block here.
319
- 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
320
323
  groups Text, Punctuation, Text
321
324
  open_block :heading
322
325
  end
323
326
 
324
327
  # Thematic breaks, before the list rules so `---` is not a bullet.
325
- rule %r/#{bol}#{MARGIN}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/ do
328
+ rule %r/#{bol}#{MARGIN}\*{3,}[ \t]*$/ do
326
329
  token Punctuation
327
330
  open_block
328
331
  end
@@ -351,14 +354,6 @@ module Rouge
351
354
  remember_host(:term)
352
355
  open_block :dlterm, :heading
353
356
  end
354
- rule %r/#{bol}#{root_margin}(:)( +[ \t]*)(\+)()$#{PLUS_NEXT}/ do
355
- if definition_term_before?
356
- groups Text, Punctuation, Text, Operator, Text
357
- else
358
- groups Text, Punctuation, Text, Text, Text
359
- end
360
- open_block :listitem, :quoteline
361
- end
362
357
  # Without a term above, `: b` is paragraph text (a lazy line too).
363
358
  rule %r/#{bol}(#{MARGIN})(:)(?= +[ \t]*[^ \t\n])/ do
364
359
  # the scan from the last definition is short; the term scan runs once
@@ -393,30 +388,6 @@ module Rouge
393
388
  end
394
389
  end
395
390
 
396
- # A task can attach its first block on the marker line.
397
- rule %r/#{bol}#{root_margin}([-*](?:#{ATTRS})?)( +[ \t]*)(\[[^\]\n]\])( +[ \t]*)(\+)()$#{PLUS_NEXT}/ do
398
- groups Text, Punctuation, Text, Name::Constant, Text, Operator, Text
399
- open_block :listitem, :quoteline
400
- end
401
-
402
- rule %r/#{bol}#{root_margin}(#{OPENER_RUN})(\+)()$#{PLUS_NEXT}/ do |m|
403
- token Text, m[1]
404
- scanner = StringScanner.new(m[2])
405
- until scanner.eos?
406
- if (marker = scanner.scan(/(?:#{ORDERED}|\.)(?:#{ATTRS})?/))
407
- token Num::Integer, marker
408
- elsif (marker = scanner.scan(/[-*](?:#{ATTRS})?|>/))
409
- token Punctuation, marker
410
- else
411
- token Text, scanner.getch
412
- end
413
- end
414
- depth = quote_depth_on_line
415
- token(state?(:quotestart) && !quote_level_follows?(depth) ? Text : Operator, m[3])
416
- token Text, m[4]
417
- m[2].rstrip.end_with?('>') ? open_block(:quoteline) : open_block(:listitem, :quoteline)
418
- end
419
-
420
391
  # A quote opened on a list item's marker line (`- > q`): its `>` is a
421
392
  # marker, and a `+` behind it may be the quote's first block
422
393
  # (markup-carve/carve#3421).
@@ -436,8 +407,7 @@ module Rouge
436
407
  end
437
408
  if m[5]
438
409
  token Text, m[4]
439
- # CarveContinuation settles whether this `+` is the marker.
440
- token Operator, m[5]
410
+ token Text, m[5]
441
411
  end
442
412
  open_block(:quoteline)
443
413
  end
@@ -472,7 +442,12 @@ module Rouge
472
442
  # Tables. The header marker, the alignment run and the separator row
473
443
  # are their own tokens; cell content is lexed inline. A row needs its
474
444
  # closing pipe.
475
- rule %r/#{bol}(#{MARGIN})(\|=[<>^v~]*)#{ROW_END}/ do
445
+ rule %r/#{bol}(#{MARGIN})(\|=?)#{CELL_BRACE}#{ROW_END}/ do |m|
446
+ token Text, m[1]
447
+ braced_cell(m)
448
+ open_block :tablerow
449
+ end
450
+ rule %r/#{bol}(#{MARGIN})(\|=)#{ROW_END}/ do
476
451
  groups Text, Operator
477
452
  open_block :tablerow
478
453
  end
@@ -487,32 +462,22 @@ module Rouge
487
462
  state(:quoteopeners, &block_openers(''))
488
463
 
489
464
  state :block do
490
- # Front matter, only at the very start of the document. \A is what
491
- # keeps a `---yaml` line mid-document from opening one.
492
- rule %r/\A(?)(---)([a-zA-Z][\w-]*)?([ \t]*\n)(?=[\s\S]*?^---[ \t]*$)/ do
493
- groups Text, Punctuation, Keyword::Type, Text
494
- push :frontmatter
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
495
467
  end
496
-
497
468
  # A comment fence (%%% or longer) versus a one-line comment. The fence
498
469
  # has to be tried first: %%% also matches the one-line form. Only a
499
470
  # run of the SAME length closes it, so the width is closed over the
500
- # way #push_fence does it, and an opener with no such closer below it
501
- # is the one-line form. Text after either run is part of the comment.
471
+ # way #push_fence does it. No closer is looked for: an unclosed span
472
+ # ends at a line left of its opener, which misses the opener's owner,
473
+ # or at the end of the document. Text after either run is comment.
502
474
  rule %r/^(#{MARGIN})(%%%+)([^\n]*)(\n)/ do |m|
503
- closer = %r/^([ \t]*)(%{#{m[2].length}})(?!%)([^\n]*)$/
504
- unless closer_below?(closer, m[2].length)
505
- # The tokens of the one-line rule below. Rouge 3 has no
506
- # fallthrough! to hand the line on to it.
507
- token Text, m[1]
508
- token Comment::Preproc, '%%'
509
- token Comment, m[2][2..] + m[3]
510
- token Text, m[4]
511
- next
512
- end
475
+ closer = %r/^([ \t]*)(%{#{m[2].length},})([ \t]*)$/
476
+ outside = m[1].empty? ? nil : %r/^(?=[ \t]{0,#{m[1].length - 1}}[^ \t\n])/
513
477
 
514
478
  groups Text, Comment::Preproc, Comment, Text
515
479
  push do
480
+ rule(outside) { pop! } if outside
516
481
  rule closer do
517
482
  groups Text, Comment::Preproc, Comment
518
483
  pop!
@@ -525,28 +490,49 @@ module Rouge
525
490
  groups Text, Comment::Preproc, Comment
526
491
  end
527
492
 
528
- rule %r/^(\+)()$#{PLUS_NEXT}/ do |m|
529
- if continuation_host_before?
530
- groups Operator, Text
531
- else
532
- token Text, m[0]
533
- push :paragraph
534
- 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
535
500
  end
536
501
 
537
- 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
538
520
 
539
- # A standalone attribute block, which may span lines.
540
- rule %r/^(#{MARGIN})(#{ATTRS_LINE})/ do
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
524
+
525
+ # A standalone attribute block, which may span lines indented past its
526
+ # `{` (carve#5080). One that does not is a paragraph.
527
+ rule %r/^(#{MARGIN})(#{ATTRS})(?=[ \t]*\n(?![ \t]*\n)[ \t]*\S)/ do
541
528
  groups Text, Name::Attribute
542
529
  end
530
+ rule %r/^#{MARGIN}#{ATTRS}(?=[ \t]*(?:\n[ \t]*\n|\z))/ do |m|
531
+ token Text, m[0]
532
+ end
543
533
 
544
534
  # Any other line with content starts a paragraph.
545
- rule %r/^(?=#{MARGIN}\S)/ do
546
- @lone_image = !@current_stream.check(IMAGE_LINE).nil?
547
- @indented = @current_stream.check(/[ \t]/) ? true : false
548
- push :paragraph
549
- end
535
+ rule(%r/^(?=#{MARGIN}\S)/) { start_paragraph }
550
536
 
551
537
  mixin :inline
552
538
  end
@@ -563,11 +549,7 @@ module Rouge
563
549
 
564
550
  state :paraline do
565
551
  rule(%r/^(?=[ \t]*(?:\n|\z))/) { pop! }
566
- rule(%r/^(?=\+$#{PLUS_NEXT})/) do
567
- continuation_host_before? ? pop! : continue_paragraph(:paragraph)
568
- end
569
552
  rule(%r/^(?=#{MARGIN}(?:#{INTERRUPT}))/) { pop! }
570
- rule(%r/^(?=#{MARGIN}(`{3,}|~{3,}))/) { |m| fence_closed_below?(m[1]) ? pop! : continue_paragraph(:paragraph) }
571
553
  rule(%r/^(?=#{MARGIN}\^[ \t])/) { @lone_image ? pop! : continue_paragraph(:paragraph) }
572
554
  # An indented paragraph sits in a container, so a list marker below it
573
555
  # opens a sublist or the next item rather than continuing it.
@@ -580,26 +562,29 @@ module Rouge
580
562
  { listitem: Text, dlterm: Generic::Heading }.each do |name, tok|
581
563
  line = :"#{name}line"
582
564
  state name do
583
- rule(/\n/) { token Text; goto line }
565
+ if name == :dlterm
566
+ rule(/\n/) { token Text; goto :dltermline }
567
+ else
568
+ rule(/\n/) { token Text; goto line }
569
+ end
584
570
  mixin :inlinecontent
585
571
  rule PLAIN, tok
586
572
  rule(/./) { token plain_token }
587
573
  end
574
+ next if name == :dlterm
588
575
 
589
576
  state line do
590
577
  rule(%r/^(?=[ \t]*(?:\n|\z))/) { pop! }
591
- if name == :listitem
592
- rule %r/^(\+)()$#{PLUS_NEXT}/ do
593
- groups Operator, Text
594
- goto name
595
- end
596
- end
597
578
  rule(%r/^(?=#{MARGIN}(?:#{INTERRUPT}|#{LIST_MARKER}|:[ \t]))/) { pop! }
598
- rule(%r/^(?=#{MARGIN}(`{3,}|~{3,}))/) { |m| fence_closed_below?(m[1]) ? pop! : goto(name) }
599
579
  rule(//) { goto name }
600
580
  end
601
581
  end
602
582
 
583
+ state :dltermline do
584
+ rule(%r/^(?=[ \t]*(?:\n|\z)|#{MARGIN}(?:::? [^\n]|#{INTERRUPT}))/) { pop! }
585
+ rule(//) { goto :dlterm }
586
+ end
587
+
603
588
  # Right after a blockquote marker: a block opener, or quoted text.
604
589
  state :quotestart do
605
590
  mixin :quoteopeners
@@ -622,7 +607,25 @@ module Rouge
622
607
  rule %r/[^\n]/, Text
623
608
  end
624
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
+
625
627
  state :heading do
628
+ rule(LABEL_LINE) { |m| continue_label(m) }
626
629
  rule %r/$/, Text, :pop!
627
630
  mixin :lineverbatim
628
631
  mixin :inlinecontent
@@ -639,7 +642,9 @@ module Rouge
639
642
  end
640
643
 
641
644
  state :caption do
642
- 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
643
648
  mixin :lineverbatim
644
649
  mixin :inlinecontent
645
650
  rule PLAIN, Generic::Subheading
@@ -650,10 +655,10 @@ module Rouge
650
655
  rule %r/$/, Text, :pop!
651
656
  # A header cell marker, optionally carrying an alignment run. The pipe
652
657
  # is part of it, so `|=` reads as one construct.
653
- rule %r/\|=[<>^v~]*/, Operator
654
- # A separator row cell, in the native and the GFM spelling.
655
- rule %r/(?<=\|)[ \t]*:?-{2,}:?[ \t]*(?=\|)/, Punctuation
656
- rule %r/(?<=\|)[ \t]*[<>^v~]{1,2}[ \t]*(?=\|)/, Operator
658
+ # An escaped pipe is consumed by the escape rule first, so a `|` here
659
+ # always ends a cell.
660
+ rule(%r/(\|=?)#{CELL_BRACE}/) { |m| braced_cell(m) }
661
+ rule %r/\|=/, Operator
657
662
  rule %r/\|/, Punctuation
658
663
  mixin :inlinecontent
659
664
  rule PLAIN_NO_PIPE, Text
@@ -721,6 +726,13 @@ module Rouge
721
726
  groups Punctuation, Str::Backtick, Punctuation
722
727
  end
723
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
+
724
736
  # The reserved include directive `{{ path #section @key:value }}`
725
737
  # (PART 9 section 19, grammar.ebnf `include_directive`). The core
726
738
  # leaves it literal; a processor expands it only when a host supplies a
@@ -750,12 +762,6 @@ module Rouge
750
762
  #
751
763
  # CriticMarkup substitution and forced strike share `{~`; the first of
752
764
  # `~>` and `~}` decides which one it is.
753
- rule %r/\{(?=~(?!~\}))/ do
754
- next token(plain_token) unless closer_ahead?(:tilde, /~\}/)
755
-
756
- enter_braced(:tildebrace)
757
- end
758
-
759
765
  # A CriticMarkup comment. Without its closer, `{#id}` is an attribute
760
766
  # block.
761
767
  # The scanner's match is read before #check replaces it.
@@ -845,13 +851,15 @@ module Rouge
845
851
  groups Punctuation, Name::Namespace, Punctuation
846
852
  end
847
853
  # An autolink.
848
- rule %r/(<)([a-zA-Z][\w+.-]*:[^>\s]+|[^>\s@]+@[^>\s]+)(>)/ do
854
+ rule %r/(<)([a-zA-Z][\w+.-]*:[^<>\s]+|[^<>\s@]+@[^<>\s]+)(>)/ do
849
855
  groups Punctuation, Name::Tag, Punctuation
850
856
  end
851
857
 
852
858
  # The `:name[...]` extension form.
853
- rule %r/(:)([a-zA-Z][\w-]*)(\[)/ do
854
- 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?)
855
863
  groups Punctuation, Name::Function, Punctuation
856
864
  push :"rolebody_#{scope}"
857
865
  else
@@ -871,18 +879,18 @@ module Rouge
871
879
  # decided in #open_bare, from the characters on both sides and from
872
880
  # whether a closer follows on the line; what it encloses is lexed as
873
881
  # inline content. `==>` is left whole for the typographic rule.
874
- rule %r/[*\/_~]|=(?!=>)/ do |m|
882
+ rule %r/[*\/]/ do |m|
875
883
  open_bare(m[0])
876
884
  end
877
885
 
878
886
  # A mention and a tag, each one token: the sigil is part of the name
879
887
  # rather than punctuation beside it.
880
- rule %r/(?<![\w\/])@[\w][\w.-]*/, Name::Variable::Magic
881
- rule %r/(?<![\w&])\#[\w][\w-]*/, Name::Variable::Instance
888
+ rule %r/(?<![\w\/])@(?=[\w.-]*?[A-Za-z])[\w][\w.-]*/, Name::Variable::Magic
889
+ rule %r/(?<![\w&])\#(?=[\w-]*?[A-Za-z])[\w][\w-]*/, Name::Variable::Instance
882
890
 
883
891
  # Typographic runs, longest first: an arrow is not an en dash plus a
884
892
  # stray angle bracket, and `---` is not `--` plus `-`.
885
- rule %r/<-->|<==>|<=>|-->|<--|==>|<==|->|<-/, Operator
893
+ rule %r/<-->|<--(?!-)|(?<!-)-->|<=>|==>|<==/, Operator
886
894
  rule %r/(?<!-)---(?!-)|(?<!-)--(?!-)/, Punctuation
887
895
  rule %r/\.\.\./, Punctuation
888
896
  end
@@ -1004,15 +1012,6 @@ module Rouge
1004
1012
  state :"#{body}_#{scope}" do
1005
1013
  rule %r/\]/, Punctuation, :pop!
1006
1014
  rule(%r/(?=#{ends.source}|\n?\z)/) { pop! }
1007
- # The newline is handed back so the block reads the fence line.
1008
- rule(FENCE_NEXT) do |m|
1009
- if fence_closed_below?(m[1])
1010
- m.pos -= 1
1011
- pop!
1012
- else
1013
- token Text
1014
- end
1015
- end
1016
1015
  mixin :inlinecontent
1017
1016
  rule PLAIN_NO_BRACKET, tok
1018
1017
  rule %r/[^\]\n]/, tok
@@ -1026,17 +1025,7 @@ module Rouge
1026
1025
  # it names is the continuation operator, every other `+` the states
1027
1026
  # gave Operator is text.
1028
1027
  def stream_tokens(str, &block)
1029
- markers = CarveContinuation.markers(str).to_h { |offset| [offset, true] }
1030
- candidates = CarveContinuation.candidates(str).to_h { |offset| [offset, true] }
1031
- pos = 0
1032
- super(str) do |tok, val|
1033
- if val.include?('+') && candidates.any?
1034
- retag_plus(tok, val, pos, markers, candidates, &block)
1035
- else
1036
- yield tok, val
1037
- end
1038
- pos += val.length
1039
- end
1028
+ super(str, &block)
1040
1029
  end
1041
1030
 
1042
1031
  private
@@ -1100,13 +1089,6 @@ module Rouge
1100
1089
 
1101
1090
  limit = enclosing_closer(pos)
1102
1091
 
1103
- if char == '/' && after == '*' && text[pos + 1].to_s.match?(/\S/) &&
1104
- closer_ahead?(:bolditalic, COMBINED_CLOSERS[:bolditalic], pos + 2, masked: :combined, before: limit)
1105
- token Punctuation
1106
- push :bolditalic
1107
- return push(:punctchar)
1108
- end
1109
-
1110
1092
  if char == '*' && after == '/' && !before.to_s.match?(/[\w*\/]/) &&
1111
1093
  text[pos + 1].to_s.match?(/\S/) && !(text[pos + 1] == '*' && text[pos + 2].to_s.match?(/[\s*]/)) &&
1112
1094
  closer_ahead?(:italicbold, COMBINED_CLOSERS[:italicbold], pos + 2, masked: :combined, before: limit)
@@ -1230,6 +1212,27 @@ module Rouge
1230
1212
  # escapes and code spans follows before that block ends; nil otherwise.
1231
1213
  # The brackets of one block are found in one pass and reused, so a run
1232
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
+
1233
1236
  def bracket_ahead?
1234
1237
  stream = @current_stream
1235
1238
  pos = stream.charpos
@@ -1247,20 +1250,10 @@ module Rouge
1247
1250
  entry[1].bsearch { |at| at >= pos } ? scope : nil
1248
1251
  end
1249
1252
 
1250
- # Where a body opened at +pos+ must end: the first block end, or the
1251
- # first closed fence, after it.
1253
+ # Where a body opened at +pos+ must end: the first block end after it.
1252
1254
  def body_end(text, pos, scope)
1253
1255
  ends = BODY_ENDS[scope]
1254
- last = text.index(ends, pos) || text.length
1255
- return last if scope == :line
1256
-
1257
- at = pos
1258
- while (fence = text.index(FENCE_NEXT, at)) && fence < last
1259
- return fence if fence_closed_below?($~[1], fence + 1)
1260
-
1261
- at = fence + 1
1262
- end
1263
- last
1256
+ text.index(ends, pos) || text.length
1264
1257
  end
1265
1258
 
1266
1259
  BRACKET_SCAN = /\\.|(?<!`)(`+)(?!`).*?(?<!`)\1(?!`)|\]/.freeze
@@ -1407,9 +1400,9 @@ module Rouge
1407
1400
 
1408
1401
  lines.map! { |line| line.empty? ? line : line[prefix.length..] }
1409
1402
  end
1410
- return false if lines.count { |line| line.match?(/\A[ \t]*\z/) } > 1
1411
-
1412
- lines.none? { |line| line.match?(/\A(?:\#{1,6} +|:{3,}|[-*] +|> |\||`{3,}|~{3,}|(?:[-*_][ \t]*){3,}\z)/) }
1403
+ # A term is one line (carve#4894): only a blank may stand between it
1404
+ # and its `: ` line.
1405
+ lines.size <= 1 && lines.all? { |line| line.match?(/\A[ \t]*\z/) }
1413
1406
  end
1414
1407
 
1415
1408
  # A `:` left of the `::` column of every open term above it ends the
@@ -1499,13 +1492,20 @@ module Rouge
1499
1492
  goto name
1500
1493
  end
1501
1494
 
1502
- # Whether a fence opened by +run+ on the current line is closed further
1503
- # down. Only a closed fence interrupts a paragraph.
1504
- def fence_closed_below?(run, line_start = @current_stream.charpos)
1505
- text = @current_stream.string
1506
- line_end = text.index("\n", line_start) || text.length
1507
- closer = %r/^[ \t]*#{Regexp.escape(run[0])}{#{run.length},}[ \t]*$/
1508
- closer_below?(closer, run, line_end)
1495
+ # The tokens of a cell marker and its braced alignment block, from a
1496
+ # match whose last five groups are the marker and CELL_BRACE.
1497
+ def braced_cell(m)
1498
+ pipe, open, align, rest, close = m.captures.last(5)
1499
+ token pipe == '|' ? Punctuation : Operator, pipe
1500
+ token Name::Attribute, open
1501
+ token Operator, align
1502
+ token Name::Attribute, rest + close
1503
+ end
1504
+
1505
+ def start_paragraph
1506
+ @lone_image = !@current_stream.check(IMAGE_LINE).nil?
1507
+ @indented = @current_stream.check(/[ \t]/) ? true : false
1508
+ push :paragraph
1509
1509
  end
1510
1510
 
1511
1511
  # Lex the tail of a fence or container opener line without leaving the
@@ -1538,7 +1538,9 @@ module Rouge
1538
1538
  def push_fence(opener, language)
1539
1539
  char = Regexp.escape(opener[0])
1540
1540
  width = opener.length
1541
- 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]*$/
1542
1544
 
1543
1545
  sublexer = fence_lexer(language)
1544
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.4'
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.4
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-10-23 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