Don't throw errors on RST tables in Markdown and RstMarkdown modes (#22165)

* Don't throw errors on RST tables in Markdown and RstMarkdown modes

Additions to RST simple tables (#19859) made their parsing more
restrictive, which can introduce problems with of some old
nimforum posts, which have tables with sloppily aligned columns
(like this one:
https://github.com/nim-lang/nimforum/issues/330#issuecomment-1376039966).
Also this strictness contradicts to Markdown style of not getting
in the way (ignoring errors).

So this PR proposes a new strategy of dealing with errors:
* In Markdown and legacy (old default) RstMarkdown we try to
  continue parsing, emitting only warnings
* And only in pure RST mode we throw a error

I expect that this strategy will be applied to more parts of markup code
in the future.

* Don't return anything in `checkColumns`
This commit is contained in:
Andrey Makarov
2023-06-28 14:38:54 -06:00
committed by GitHub
parent b35942ef83
commit 57de460437
2 changed files with 62 additions and 15 deletions

View File

@@ -29,7 +29,9 @@ import os
import std/[assertions, syncio]
const preferMarkdown = {roPreferMarkdown, roSupportMarkdown, roNimFile, roSandboxDisabled}
# legacy nimforum / old default mode:
const preferRst = {roSupportMarkdown, roNimFile, roSandboxDisabled}
const pureRst = {roNimFile, roSandboxDisabled}
proc toAst(input: string,
rstOptions: RstParseOptions = preferMarkdown,
@@ -917,10 +919,31 @@ suite "RST tables":
====== ======
Inputs Output
====== ======
""".toAst(error=error) == "")
""".toAst(rstOptions = pureRst, error = error) == "")
check(error[] == "input(2, 2) Error: Illformed table: " &
"this word crosses table column from the right")
# In nimforum compatibility mode & Markdown we raise a warning instead:
let expected = dedent"""
rnTable colCount=2
rnTableRow
rnTableDataCell
rnLeaf 'Inputs'
rnTableDataCell
rnLeaf 'Output'
"""
for opt in [preferRst, preferMarkdown]:
var warnings = new seq[string]
check(
dedent"""
====== ======
Inputs Output
====== ======
""".toAst(rstOptions = opt, warnings = warnings) == expected)
check(warnings[] == @[
"input(2, 2) Warning: RST style: this word crosses table column from the right"])
test "tables with slightly overflowed cells cause an error (2)":
var error = new string
check("" == dedent"""
@@ -929,7 +952,7 @@ suite "RST tables":
===== ===== ======
False False False
===== ===== ======
""".toAst(error=error))
""".toAst(rstOptions = pureRst, error = error))
check(error[] == "input(2, 8) Error: Illformed table: " &
"this word crosses table column from the right")
@@ -941,7 +964,7 @@ suite "RST tables":
===== ===== ======
False False False
===== ===== ======
""".toAst(error=error))
""".toAst(rstOptions = pureRst, error = error))
check(error[] == "input(2, 7) Error: Illformed table: " &
"this word crosses table column from the left")
@@ -954,7 +977,7 @@ suite "RST tables":
===== ======
False False
===== =======
""".toAst(error=error))
""".toAst(rstOptions = pureRst, error = error))
check(error[] == "input(5, 14) Error: Illformed table: " &
"end of table column #2 should end at position 13")
@@ -966,7 +989,7 @@ suite "RST tables":
===== =======
False False
===== ======
""".toAst(error=error))
""".toAst(rstOptions = pureRst, error = error))
check(error[] == "input(3, 14) Error: Illformed table: " &
"end of table column #2 should end at position 13")