Skip to content

Commit

Permalink
fix #17260 render \ properly in nim doc, rst2html (#17315)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour authored Mar 24, 2021
1 parent e94aec2 commit 1590d14
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
21 changes: 16 additions & 5 deletions lib/packages/docutils/rst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ proc isInlineMarkupEnd(p: RstParser, markup: string): bool =
if not result: return
# Rule 4:
if p.idx > 0:
# see bug #17260; for now `\` must be written ``\``, likewise with sequences
# ending in an un-escaped `\`; `\\` is legal but not `\\\` for example;
# for this reason we can't use `["``", "`"]` here.
if markup != "``" and prevTok(p).symbol == "\\":
result = false

Expand Down Expand Up @@ -1089,11 +1092,19 @@ proc parseUntil(p: var RstParser, father: PRstNode, postfix: string,
if isInlineMarkupEnd(p, postfix):
inc p.idx
break
elif interpretBackslash:
parseBackslash(p, father)
else:
father.add(newLeaf(p))
inc p.idx
if postfix == "`":
if prevTok(p).symbol == "\\" and currentTok(p).symbol == "`":
father.sons[^1] = newLeaf(p) # instead, we should use lookahead
else:
father.add(newLeaf(p))
inc p.idx
else:
if interpretBackslash:
parseBackslash(p, father)
else:
father.add(newLeaf(p))
inc p.idx
of tkAdornment, tkWord, tkOther:
father.add(newLeaf(p))
inc p.idx
Expand Down Expand Up @@ -1243,7 +1254,7 @@ proc parseInline(p: var RstParser, father: PRstNode) =
father.add(n)
elif isInlineMarkupStart(p, "`"):
var n = newRstNode(rnInterpretedText)
parseUntil(p, n, "`", true)
parseUntil(p, n, "`", false) # bug #17260
n = parsePostfix(p, n)
father.add(n)
elif isInlineMarkupStart(p, "|"):
Expand Down
4 changes: 2 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2827,7 +2827,7 @@ when declared(initDebugger):
proc addEscapedChar*(s: var string, c: char) {.noSideEffect, inline.} =
## Adds a char to string `s` and applies the following escaping:
##
## * replaces any `\` by `\\`
## * replaces any ``\`` by `\\`
## * replaces any `'` by `\'`
## * replaces any `"` by `\"`
## * replaces any `\a` by `\\a`
Expand All @@ -2838,7 +2838,7 @@ proc addEscapedChar*(s: var string, c: char) {.noSideEffect, inline.} =
## * replaces any `\f` by `\\f`
## * replaces any `\r` by `\\r`
## * replaces any `\e` by `\\e`
## * replaces any other character not in the set `{'\21..'\126'}
## * replaces any other character not in the set `{\21..\126}`
## by `\xHH` where `HH` is its hexadecimal value.
##
## The procedure has been designed so that its output is usable for many
Expand Down
2 changes: 1 addition & 1 deletion nimdoc/rst2html/expected/rst_examples.html
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ <h1><a class="toc-backref" id="introduction" href="#introduction">Introduction</
<p>However, this does not work. The problem is that the procedure should not only <tt class="docutils literal"><span class="pre">return</span></tt>, but return and <strong>continue</strong> after an iteration has finished. This <em>return and continue</em> is called a <tt class="docutils literal"><span class="pre">yield</span></tt> statement. Now the only thing left to do is to replace the <tt class="docutils literal"><span class="pre">proc</span></tt> keyword by <tt class="docutils literal"><span class="pre">iterator</span></tt> and here it is - our first iterator:</p>
<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
<tr><td>D1 <tt class="docutils literal"><span class="pre">code \|</span></tt></td><td>D2</td></tr>
<tr><td>E1 | text</td><td></td></tr>
<tr><td></td><td>F2 without pipe</td></tr>
</table><p>not in table </p>
Expand Down
22 changes: 20 additions & 2 deletions tests/stdlib/trstgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,15 @@ suite "RST/Markdown general":
| | F2 without pipe
not in table"""
let output1 = input1.toHtml
doAssert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
#[
TODO: `\|` inside a table cell should render as `|`
`|` outside a table cell should render as `\|`
consistently with markdown, see https://stackoverflow.com/a/66557930/1426932
]#
doAssert output1 == """
<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
<tr><td>D1 <tt class="docutils literal"><span class="pre">code \|</span></tt></td><td>D2</td></tr>
<tr><td>E1 | text</td><td></td></tr>
<tr><td></td><td>F2 without pipe</td></tr>
</table><p>not in table</p>
Expand Down Expand Up @@ -549,6 +555,18 @@ let x = 1
let output2 = input2.toHtml
doAssert "<pre" in output2 and "class=\"Keyword\"" in output2

test "interpreted text":
check """`foo.bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo.bar</span></tt>"""
check """`foo\`\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo``bar</span></tt>"""
check """`foo\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo`bar</span></tt>"""
check """`\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">`bar</span></tt>"""
check """`a\b\x\\ar`""".toHtml == """<tt class="docutils literal"><span class="pre">a\b\x\\ar</span></tt>"""

test "inline literal":
check """``foo.bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo.bar</span></tt>"""
check """``foo\bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo\bar</span></tt>"""
check """``f\`o\\o\b`ar``""".toHtml == """<tt class="docutils literal"><span class="pre">f\`o\\o\b`ar</span></tt>"""

test "RST comments":
let input1 = """
Check that comment disappears:
Expand Down

0 comments on commit 1590d14

Please sign in to comment.