From 7c699dacb5268965806ede4ac297cf27546f240e Mon Sep 17 00:00:00 2001 From: Andrey Makarov Date: Thu, 17 Dec 2020 18:24:28 +0300 Subject: [PATCH 1/2] fix matching of enumerator #) --- lib/packages/docutils/rst.nim | 3 ++- tests/stdlib/trstgen.nim | 11 ++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/packages/docutils/rst.nim b/lib/packages/docutils/rst.nim index 7091735263208..d30e1e81e462d 100644 --- a/lib/packages/docutils/rst.nim +++ b/lib/packages/docutils/rst.nim @@ -1195,7 +1195,8 @@ proc whichSection(p: RstParser): RstNodeKind = elif match(p, p.idx, ":w:") and predNL(p): # (currentTok(p).symbol == ":") result = rnFieldList - elif match(p, p.idx, "(e) ") or match(p, p.idx, "e. "): + elif match(p, p.idx, "(e) ") or match(p, p.idx, "e) ") or + match(p, p.idx, "e. "): result = rnEnumList elif isDefList(p): result = rnDefList diff --git a/tests/stdlib/trstgen.nim b/tests/stdlib/trstgen.nim index f3a8aa59ab4b7..6a47b6722e880 100644 --- a/tests/stdlib/trstgen.nim +++ b/tests/stdlib/trstgen.nim @@ -440,13 +440,18 @@ Test1 Check that auto-numbered enumeration lists work. #. string1 + #. string2 + #. string3 + + #) string5 + #) string6 """ let output5 = rstToHtml(input5, {roSupportMarkdown}, defaultConfig()) - assert count(output5, "
    ") == 1 - assert count(output5, "
  1. ") == 3 + assert count(output5, "
      ") == 2 + assert count(output5, "
    1. ") == 5 let input6 = dedent """ ... And for alphabetic enumerators too! From 47b38392b344758415101db10ad9c80b5824d689 Mon Sep 17 00:00:00 2001 From: Andrey Makarov Date: Thu, 17 Dec 2020 23:15:01 +0300 Subject: [PATCH 2/2] RST: markdown list with auto-enumerator `1` --- lib/packages/docutils/rst.nim | 22 ++++++++++++++++------ tests/stdlib/trstgen.nim | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/packages/docutils/rst.nim b/lib/packages/docutils/rst.nim index d30e1e81e462d..0d86edcdb4f87 100644 --- a/lib/packages/docutils/rst.nim +++ b/lib/packages/docutils/rst.nim @@ -48,6 +48,10 @@ ## * markdown code blocks ## * markdown links ## * markdown headlines +## * using ``1`` as auto-enumerator in enumerated lists like RST ``#`` +## (auto-enumerator ``1`` can not be used with ``#`` in the same list) +## +## **Note:** By default nim has ``roSupportMarkdown`` turned **on**. ## ## Limitations: ## @@ -1538,8 +1542,12 @@ proc parseEnumList(p: var RstParser): PRstNode = if match(p, p.idx, wildcards[w]): break inc w assert w < wildcards.len + let autoEnums = if roSupportMarkdown in p.s.options: @["#", "1"] else: @["#"] + var prevAE = "" # so as not allow mixing auto-enumerators `1` and `#` + var curEnum = 1 for i in 0 ..< wildToken[w]-1: # add first enumerator with (, ), and . if p.tok[p.idx + i].symbol == "#": + prevAE = "#" result.text.add "1" else: result.text.add p.tok[p.idx + i].symbol @@ -1557,17 +1565,19 @@ proc parseEnumList(p: var RstParser): PRstNode = # check that it's in sequence: enumerator == next(prevEnum) if "n" in wildcards[w]: # arabic numeral let prevEnumI = try: parseInt(prevEnum) except: 1 - let curEnum = - if enumerator == "#": prevEnumI + 1 - else: (try: parseInt(enumerator) except: 1) + if enumerator in autoEnums: + if prevAE != "" and enumerator != prevAE: + break + prevAE = enumerator + curEnum = prevEnumI + 1 + else: curEnum = (try: parseInt(enumerator) except: 1) if curEnum - prevEnumI != 1: break prevEnum = enumerator else: # a..z let prevEnumI = ord(prevEnum[0]) - let curEnum = - if enumerator == "#": prevEnumI + 1 - else: ord(enumerator[0]) + if enumerator == "#": curEnum = prevEnumI + 1 + else: curEnum = ord(enumerator[0]) if curEnum - prevEnumI != 1: break prevEnum = $chr(curEnum) diff --git a/tests/stdlib/trstgen.nim b/tests/stdlib/trstgen.nim index 6a47b6722e880..85a96056afd29 100644 --- a/tests/stdlib/trstgen.nim +++ b/tests/stdlib/trstgen.nim @@ -453,6 +453,18 @@ Test1 assert count(output5, "
    ") == 2 assert count(output5, "
  2. ") == 5 + let input5a = dedent """ + Auto-numbered RST list can start with 1 even when Markdown support is on. + + 1. string1 + #. string2 + #. string3 + """ + let output5a = rstToHtml(input5a, {roSupportMarkdown}, defaultConfig()) + assert count(output5a, "
      ") == 1 + assert count(output5a, "
    1. ") == 3 + let input6 = dedent """ ... And for alphabetic enumerators too! @@ -479,6 +491,25 @@ Test1 assert count(output7, "
    2. ") == 3 assert "start=\"3\"" in output7 and "class=\"upperalpha simple\"" in output7 + test "Markdown enumerated lists": + let input1 = dedent """ + Below are 2 enumerated lists: Markdown-style (5 items) and RST (1 item) + 1. line1 + 1. line2 + 1. line3 + 1. line4 + + 1. line5 + + #. lineA + """ + let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig()) + for i in 1..5: + assert ($i & ". line" & $i) notin output1 + assert ("
    3. line" & $i & "
    4. ") in output1 + assert count(output1, "
        ") == 2 + test "RST bullet lists": let input1 = dedent """ * line1