discard """ outputsub: "" """ # tests for rstgen module. import ../../lib/packages/docutils/rstgen import ../../lib/packages/docutils/rst import unittest, strutils, strtabs import std/private/miscdollars proc toHtml(input: string, rstOptions: RstParseOptions = {roSupportMarkdown, roNimFile}, error: ref string = nil, warnings: ref seq[string] = nil): string = ## If `error` is nil then no errors should be generated. ## The same goes for `warnings`. proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind, arg: string) = let mc = msgkind.whichMsgClass let a = $msgkind % arg var message: string toLocation(message, filename, line, col + ColRstOffset) message.add " $1: $2" % [$mc, a] if mc == mcError: doAssert error != nil, "unexpected RST error '" & message & "'" error[] = message # we check only first error because subsequent ones may be meaningless raise newException(EParseError, message) else: doAssert warnings != nil, "unexpected RST warning '" & message & "'" warnings[].add message try: result = rstToHtml(input, rstOptions, defaultConfig(), msgHandler=testMsgHandler) except EParseError: discard # inline code tags (for parsing originated from highlite.nim) proc id(str: string): string = """""" & str & "" proc op(str: string): string = """""" & str & "" proc pu(str: string): string = """""" & str & "" suite "YAML syntax highlighting": test "Basics": let input = """.. code-block:: yaml %YAML 1.2 --- a string: string a list: - item 1 - item 2 a map: ? key : value ...""" let output = input.toHtml({}) doAssert output == """
%YAML 1.2 --- a string: string a list: - item 1 - item 2 a map: ? key : value ...""" test "Block scalars": let input = """.. code-block:: yaml a literal block scalar: | some text # not a comment # a comment, since less indented # another comment a folded block scalar: >2 some text # not a comment since indented as specified # a comment another literal block scalar: |+ # comment after header allowed, since more indented than parent""" let output = input.toHtml({}) doAssert output == """
a literal block scalar: | some text # not a comment # a comment, since less indented # another comment a folded block scalar: >2 some text # not a comment since indented as specified # a comment another literal block scalar: |+ # comment after header allowed, since more indented than parent""" test "Directives": let input = """.. code-block:: yaml %YAML 1.2 --- %not a directive ... %a directive ... a string % not a directive ... %TAG ! !foo:""" let output = input.toHtml({}) doAssert output == """
%YAML 1.2 --- %not a directive ... %a directive ... a string % not a directive ... %TAG ! !foo:""" test "Flow Style and Numbers": let input = """.. code-block:: yaml { "quoted string": 42, 'single quoted string': false, [ list, "with", 'entries' ]: 73.32e-73, more numbers: [-783, 11e78], not numbers: [ 42e, 0023, +32.37, 8 ball] }""" let output = input.toHtml({}) doAssert output == """
{ "quoted string": 42, 'single quoted string': false, [ list, "with", 'entries' ]: 73.32e-73, more numbers: [-783, 11e78], not numbers: [ 42e, 0023, +32.37, 8 ball] }""" test "Directives: warnings": let input = dedent""" .. non-existant-warning: Paragraph. .. another.wrong:warning::: Paragraph. """ var warnings = new seq[string] let output = input.toHtml(warnings=warnings) check output == "" doAssert warnings[].len == 2 check "(1, 24) Warning: RST style:" in warnings[0] check "double colon :: may be missing at end of 'non-existant-warning'" in warnings[0] check "(3, 25) Warning: RST style:" in warnings[1] check "RST style: too many colons for a directive (should be ::)" in warnings[1] test "not a directive": let input = "..warning:: I am not a warning." check input.toHtml == input test "Anchors, Aliases, Tags": let input = """.. code-block:: yaml --- !!map !!str string: !
--- !!map !!str string: !<tag:yaml.org,2002:int> 42 ? &anchor !!seq []: : !localtag foo alias: *anchor""" test "Edge cases": let input = """.. code-block:: yaml ... %a string: a:string:not:a:map ... not a list: -2 -3 -4 example.com/not/a#comment: ?not a map key """ let output = input.toHtml({}) doAssert output == """
... %a string: a:string:not:a:map ... not a list: -2 -3 -4 example.com/not/a#comment: ?not a map key""" suite "RST/Markdown general": test "RST emphasis": doAssert rstToHtml("*Hello* **world**!", {}, newStringTable(modeStyleInsensitive)) == "Hello world!" test "Markdown links": check("(( [Nim](https://nim-lang.org/) ))".toHtml == """(( Nim ))""") check("(([Nim](https://nim-lang.org/)))".toHtml == """((Nim))""") check("[[Nim](https://nim-lang.org/)]".toHtml == """[Nim]""") test "Markdown tables": let input1 = """ | A1 header | A2 \| not fooled | :--- | ----: | | C1 | C2 **bold** | ignored | | D1 `code \|` | D2 | also ignored | E1 \| text | | | F2 without pipe not in table""" let output1 = input1.toHtml #[ 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 ]# check(output1 == """
A1 header | A2 | not fooled |
---|---|
C1 | C2 bold |
D1 """ & id"code" & " " & op"\|" & """ | D2 |
E1 | text | |
F2 without pipe |
not in table
""") let input2 = """ | A1 header | A2 | | --- | --- |""" let output2 = input2.toHtml doAssert output2 == """A1 header | A2 |
---|
""" & id"foo" & op"." & id"bar" & "") check("""`foo\`\`bar`""".toHtml == """""" & id"foo" & pu"`" & pu"`" & id"bar" & "") check("""`foo\`bar`""".toHtml == """""" & id"foo" & pu"`" & id"bar" & "") check("""`\`bar`""".toHtml == """""" & pu"`" & id"bar" & "") check("""`a\b\x\\ar`""".toHtml == """""" & id"a" & op"""\""" & id"b" & op"""\""" & id"x" & op"""\\""" & id"ar" & "") test "inline literal": check """``foo.bar``""".toHtml == """foo.bar""" check """``foo\bar``""".toHtml == """foo\bar""" check """``f\`o\\o\b`ar``""".toHtml == """f\`o\\o\b`ar""" test "default-role": # nim(default) -> literal -> nim -> code(=literal) let input = dedent""" Par1 `value1`. .. default-role:: literal Par2 `value2`. .. default-role:: nim Par3 `value3`. .. default-role:: code Par4 `value4`.""" let p1 = """Par1 """ & id"value1" & "." let p2 = """Par2 value2.
""" let p3 = """Par3 """ & id"value3" & ".
" let p4 = """Par4 value4.
""" let expected = p1 & p2 & "\n" & p3 & "\n" & p4 & "\n" check(input.toHtml == expected) test "role directive": let input = dedent""" .. role:: y(code) :language: yaml .. role:: brainhelp(code) :language: brainhelp """ var warnings = new seq[string] let output = input.toHtml(warnings=warnings) check(warnings[].len == 1 and "language 'brainhelp' not supported" in warnings[0]) test "RST comments": let input1 = """ Check that comment disappears: .. some comment """ let output1 = input1.toHtml doAssert output1 == "Check that comment disappears:" test "RST line blocks": let input1 = """ ===== Test1 ===== | | | line block | other line """ var option: bool var rstGenera: RstGenerator var output1: string rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", {}) rstGenera.renderRstToOut(rstParse(input1, "", 1, 1, option, {}), output1) doAssert rstGenera.meta[metaTitle] == "Test1" # check that title was not overwritten to '|' doAssert output1 == "" let output1l = rstToLatex(input1, {}) doAssert "line block\n\n" in output1l doAssert "other line\n\n" in output1l doAssert output1l.count("\\vspace") == 2 + 2 # +2 surrounding paddings let input2 = dedent""" Paragraph1 | Paragraph2""" let output2 = input2.toHtml doAssert "Paragraph1
line block
other line
Paragraph2
\n" == output2 let input3 = dedent""" | xxx | yyy | zzz""" let output3 = input3.toHtml doAssert "xxx
" in output3 doAssert "yyy
" in output3 doAssert "zzz
" in output3 # check that '| ' with a few spaces is still parsed as new line let input4 = dedent""" | xxx | | zzz""" let output4 = input4.toHtml doAssert "xxx
" in output4 doAssert "zzz
" in output4 test "RST enumerated lists": let input1 = dedent """ 1. line1 1 2. line2 2 3. line3 3 4. line4 4 5. line5 5 """ let output1 = input1.toHtml for i in 1..5: doAssert ($i & ". line" & $i) notin output1 doAssert ("
C. string1 string2
\n" 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 = input1.toHtml for i in 1..5: doAssert ($i & ". line" & $i) notin output1 doAssert ("Paragraph1
" in output0 test "Nim code-block :number-lines:": let input = dedent """ .. code-block:: nim :number-lines: 55 x y """ check "55\n56\n" in input.toHtml test "RST admonitions": # check that all admonitions are implemented let input0 = dedent """ .. admonition:: endOf admonition .. attention:: endOf attention .. caution:: endOf caution .. danger:: endOf danger .. error:: endOf error .. hint:: endOf hint .. important:: endOf important .. note:: endOf note .. tip:: endOf tip .. warning:: endOf warning """ let output0 = input0.toHtml for a in ["admonition", "attention", "caution", "danger", "error", "hint", "important", "note", "tip", "warning" ]: doAssert "endOf " & a & "
Test paragraph.
" in output1 doAssert "class=\"admonition admonition-error\"" in output1 # Test that second line is parsed as continuation of the first line. let input2 = dedent """ .. error:: endOfError Test2p. Test paragraph. """ let output2 = input2.toHtml doAssert "endOfError Test2p.Test paragraph.
" in output2 doAssert "class=\"admonition admonition-error\"" in output2 let input3 = dedent """ .. note:: endOfNote """ let output3 = input3.toHtml doAssert "endOfNotetarget300" in output2 doAssert "href=\"#subsectiona\">target301" in output2 doAssert "href=\"#citation-cit2020\">target103" in output2 let output2l = rstToLatex(input2, {}) doAssert "\\label{section-xyz}\\hypertarget{section-xyz}{}" in output2l doAssert "\\hyperlink{section-xyz}{target300}" in output2l doAssert "\\hyperlink{subsectiona}{target301}" in output2l test "RST internal links (inline)": let input1 = dedent """ Paragraph with _`some definition`. Ref. `some definition`_. """ let output1 = input1.toHtml doAssert "some definition" in output1 doAssert "Ref. some definition" in output1 test "RST references (additional symbols)": # check that ., _, -, +, : are allowed symbols in references without ` ` let input1 = dedent """ sec.1 ----- 2-other:sec+c_2 ^^^^^^^^^^^^^^^ .. _link.1_2021: Paragraph Ref. sec.1_! and 2-other:sec+c_2_;and link.1_2021_. """ let output1 = input1.toHtml doAssert "id=\"secdot1\"" in output1 doAssert "id=\"Z2minusothercolonsecplusc-2\"" in output1 doAssert "id=\"linkdot1-2021\"" in output1 let ref1 = "sec.1" let ref2 = "2-other:sec+c_2" let ref3 = "link.1_2021" let refline = "Ref. " & ref1 & "! and " & ref2 & ";and " & ref3 & "." doAssert refline in output1 test "Option lists 1": # check that "* b" is not consumed by previous bullet item because of # incorrect indentation handling in option lists let input = dedent """ * a -m desc -n very long desc * b""" let output = input.toHtml check(output.count("
option
" notin output test "Option list 3 (double /)": let input = dedent """ * a //compile compile1 //doc doc1 cont -d option""" let output = input.toHtml check(output.count("option
" notin output test "Roles: subscript prefix/postfix": let expected = "See some text." check "See :subscript:`some text`.".toHtml == expected check "See `some text`:subscript:.".toHtml == expected test "Roles: correct parsing from beginning of line": let expected = "3He is an isotope of helium." check """:superscript:`3`\ He is an isotope of helium.""".toHtml == expected check """:sup:`3`\ He is an isotope of helium.""".toHtml == expected check """`3`:sup:\ He is an isotope of helium.""".toHtml == expected check """`3`:superscript:\ He is an isotope of helium.""".toHtml == expected test "Roles: warnings": let input = dedent""" See function :py:func:`spam`. See also `egg`:py:class:. """ var warnings = new seq[string] let output = input.toHtml(warnings=warnings) doAssert warnings[].len == 2 check "(1, 14) Warning: " in warnings[0] check "language 'py:func' not supported" in warnings[0] check "(3, 15) Warning: " in warnings[1] check "language 'py:class' not supported" in warnings[1] check("""See function spam.
""" & "\n" & """See also egg.
""" & "\n" == output) test "(not) Roles: check escaping 1": let expected = """See :subscript:""" & """""" & id"some" & " " & id"text" & "." check """See \:subscript:`some text`.""".toHtml == expected check """See :subscript\:`some text`.""".toHtml == expected test "(not) Roles: check escaping 2": check("""See :subscript:\`some text\`.""".toHtml == "See :subscript:`some text`.") test "Field list": check(":field: text".toHtml == """field: | """ & """text |
---|
text1 | " in output test "Field list (incorrect)": check ":field:text".toHtml == ":field:text" suite "RST/Code highlight": test "Basic Python code highlight": let pythonCode = """ .. code-block:: python def f_name(arg=42): print(f"{arg}") """ let expected = """