Fix RST/Markdown false heading detection (#21685)

This commit is contained in:
Andrey Makarov
2023-04-18 11:37:47 +03:00
committed by GitHub
parent 2f547afb0f
commit 24b6378382
3 changed files with 57 additions and 14 deletions

View File

@@ -2124,17 +2124,20 @@ proc isAdornmentHeadline(p: RstParser, adornmentIdx: int): bool =
while p.tok[i].kind notin {tkEof, tkIndent}:
headlineLen += p.tok[i].symbol.len
inc i
result = p.tok[adornmentIdx].symbol.len >= headlineLen and
headlineLen != 0
if result:
result = result and p.tok[i].kind == tkIndent and
p.tok[i+1].kind == tkAdornment and
p.tok[i+1].symbol == p.tok[adornmentIdx].symbol
if not result:
failure = "(underline '" & p.tok[i+1].symbol & "' does not match " &
"overline '" & p.tok[adornmentIdx].symbol & "')"
else:
failure = "(overline '" & p.tok[adornmentIdx].symbol & "' is too short)"
if p.tok[i].kind == tkIndent and
p.tok[i+1].kind == tkAdornment and
p.tok[i+1].symbol[0] == p.tok[adornmentIdx].symbol[0]:
result = p.tok[adornmentIdx].symbol.len >= headlineLen and
headlineLen != 0
if result:
result = p.tok[i+1].symbol == p.tok[adornmentIdx].symbol
if not result:
failure = "(underline '" & p.tok[i+1].symbol & "' does not match " &
"overline '" & p.tok[adornmentIdx].symbol & "')"
else:
failure = "(overline '" & p.tok[adornmentIdx].symbol & "' is too short)"
else: # it's not overline/underline section, not reporting error
return false
if not result:
rstMessage(p, meNewSectionExpected, failure)
@@ -2261,10 +2264,11 @@ proc whichSection(p: RstParser): RstNodeKind =
result = rnLineBlock
elif roSupportMarkdown in p.s.options and isMarkdownBlockQuote(p):
result = rnMarkdownBlockQuote
elif match(p, p.idx + 1, "i") and isAdornmentHeadline(p, p.idx):
elif (match(p, p.idx + 1, "i") and not match(p, p.idx + 2, "I")) and
isAdornmentHeadline(p, p.idx):
result = rnOverline
else:
result = rnLeaf
result = rnParagraph
of tkPunct:
if isMarkdownHeadline(p):
result = rnMarkdownHeadline

View File

@@ -67,6 +67,45 @@ proc toAst(input: string,
result = e.msg
suite "RST parsing":
test "Standalone punctuation is not parsed as heading overlines":
check(dedent"""
Paragraph
!""".toAst ==
dedent"""
rnInner
rnParagraph
rnLeaf 'Paragraph'
rnParagraph
rnLeaf '!'
""")
check(dedent"""
Paragraph1
...
Paragraph2""".toAst ==
dedent"""
rnInner
rnParagraph
rnLeaf 'Paragraph1'
rnParagraph
rnLeaf '...'
rnParagraph
rnLeaf 'Paragraph2'
""")
check(dedent"""
---
Paragraph""".toAst ==
dedent"""
rnInner
rnLeaf '---'
rnLeaf ' '
rnLeaf 'Paragraph'
""")
test "References are whitespace-neutral and case-insensitive":
# refname is 'lexical-analysis', the same for all the 3 variants:
check(dedent"""

View File

@@ -617,8 +617,8 @@ context2
This is too short to be a transition:
---
context2
---
"""
var error2 = new string
let output2 = input2.toHtml(error=error2)