Skip to content

Commit 1590d14

Browse files
authored
fix #17260 render \ properly in nim doc, rst2html (#17315)
1 parent e94aec2 commit 1590d14

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

lib/packages/docutils/rst.nim

+16-5
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ proc isInlineMarkupEnd(p: RstParser, markup: string): bool =
852852
if not result: return
853853
# Rule 4:
854854
if p.idx > 0:
855+
# see bug #17260; for now `\` must be written ``\``, likewise with sequences
856+
# ending in an un-escaped `\`; `\\` is legal but not `\\\` for example;
857+
# for this reason we can't use `["``", "`"]` here.
855858
if markup != "``" and prevTok(p).symbol == "\\":
856859
result = false
857860

@@ -1089,11 +1092,19 @@ proc parseUntil(p: var RstParser, father: PRstNode, postfix: string,
10891092
if isInlineMarkupEnd(p, postfix):
10901093
inc p.idx
10911094
break
1092-
elif interpretBackslash:
1093-
parseBackslash(p, father)
10941095
else:
1095-
father.add(newLeaf(p))
1096-
inc p.idx
1096+
if postfix == "`":
1097+
if prevTok(p).symbol == "\\" and currentTok(p).symbol == "`":
1098+
father.sons[^1] = newLeaf(p) # instead, we should use lookahead
1099+
else:
1100+
father.add(newLeaf(p))
1101+
inc p.idx
1102+
else:
1103+
if interpretBackslash:
1104+
parseBackslash(p, father)
1105+
else:
1106+
father.add(newLeaf(p))
1107+
inc p.idx
10971108
of tkAdornment, tkWord, tkOther:
10981109
father.add(newLeaf(p))
10991110
inc p.idx
@@ -1243,7 +1254,7 @@ proc parseInline(p: var RstParser, father: PRstNode) =
12431254
father.add(n)
12441255
elif isInlineMarkupStart(p, "`"):
12451256
var n = newRstNode(rnInterpretedText)
1246-
parseUntil(p, n, "`", true)
1257+
parseUntil(p, n, "`", false) # bug #17260
12471258
n = parsePostfix(p, n)
12481259
father.add(n)
12491260
elif isInlineMarkupStart(p, "|"):

lib/system.nim

+2-2
Original file line numberDiff line numberDiff line change
@@ -2827,7 +2827,7 @@ when declared(initDebugger):
28272827
proc addEscapedChar*(s: var string, c: char) {.noSideEffect, inline.} =
28282828
## Adds a char to string `s` and applies the following escaping:
28292829
##
2830-
## * replaces any `\` by `\\`
2830+
## * replaces any ``\`` by `\\`
28312831
## * replaces any `'` by `\'`
28322832
## * replaces any `"` by `\"`
28332833
## * replaces any `\a` by `\\a`
@@ -2838,7 +2838,7 @@ proc addEscapedChar*(s: var string, c: char) {.noSideEffect, inline.} =
28382838
## * replaces any `\f` by `\\f`
28392839
## * replaces any `\r` by `\\r`
28402840
## * replaces any `\e` by `\\e`
2841-
## * replaces any other character not in the set `{'\21..'\126'}
2841+
## * replaces any other character not in the set `{\21..\126}`
28422842
## by `\xHH` where `HH` is its hexadecimal value.
28432843
##
28442844
## The procedure has been designed so that its output is usable for many

nimdoc/rst2html/expected/rst_examples.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ <h1><a class="toc-backref" id="introduction" href="#introduction">Introduction</
300300
<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>
301301
<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
302302
<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
303-
<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
303+
<tr><td>D1 <tt class="docutils literal"><span class="pre">code \|</span></tt></td><td>D2</td></tr>
304304
<tr><td>E1 | text</td><td></td></tr>
305305
<tr><td></td><td>F2 without pipe</td></tr>
306306
</table><p>not in table </p>

tests/stdlib/trstgen.nim

+20-2
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,15 @@ suite "RST/Markdown general":
196196
| | F2 without pipe
197197
not in table"""
198198
let output1 = input1.toHtml
199-
doAssert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
199+
#[
200+
TODO: `\|` inside a table cell should render as `|`
201+
`|` outside a table cell should render as `\|`
202+
consistently with markdown, see https://stackoverflow.com/a/66557930/1426932
203+
]#
204+
doAssert output1 == """
205+
<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
200206
<tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
201-
<tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
207+
<tr><td>D1 <tt class="docutils literal"><span class="pre">code \|</span></tt></td><td>D2</td></tr>
202208
<tr><td>E1 | text</td><td></td></tr>
203209
<tr><td></td><td>F2 without pipe</td></tr>
204210
</table><p>not in table</p>
@@ -549,6 +555,18 @@ let x = 1
549555
let output2 = input2.toHtml
550556
doAssert "<pre" in output2 and "class=\"Keyword\"" in output2
551557

558+
test "interpreted text":
559+
check """`foo.bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo.bar</span></tt>"""
560+
check """`foo\`\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo``bar</span></tt>"""
561+
check """`foo\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">foo`bar</span></tt>"""
562+
check """`\`bar`""".toHtml == """<tt class="docutils literal"><span class="pre">`bar</span></tt>"""
563+
check """`a\b\x\\ar`""".toHtml == """<tt class="docutils literal"><span class="pre">a\b\x\\ar</span></tt>"""
564+
565+
test "inline literal":
566+
check """``foo.bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo.bar</span></tt>"""
567+
check """``foo\bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo\bar</span></tt>"""
568+
check """``f\`o\\o\b`ar``""".toHtml == """<tt class="docutils literal"><span class="pre">f\`o\\o\b`ar</span></tt>"""
569+
552570
test "RST comments":
553571
let input1 = """
554572
Check that comment disappears:

0 commit comments

Comments
 (0)