discard """ outputsub: "" """ # tests for rstgen module. import ../../lib/packages/docutils/rstgen import ../../lib/packages/docutils/rst import unittest, strutils, strtabs proc toHtml(input: string): string = rstToHtml(input, {roSupportMarkdown}, defaultConfig()) 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 = rstTohtml(input, {}, defaultConfig()) 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 = rstToHtml(input, {}, defaultConfig()) 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 = rstToHtml(input, {}, defaultConfig()) 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 = rstToHtml(input, {}, defaultConfig()) 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 "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 = rstToHtml(input, {}, defaultConfig()) 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": let a = rstToHtml("(( [Nim](https://nim-lang.org/) ))", {roSupportMarkdown}, defaultConfig()) b = rstToHtml("(([Nim](https://nim-lang.org/)))", {roSupportMarkdown}, defaultConfig()) c = rstToHtml("[[Nim](https://nim-lang.org/)]", {roSupportMarkdown}, defaultConfig()) doAssert a == """(( Nim ))""" doAssert b == """((Nim))""" doAssert c == """[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 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig()) doAssert output1 == """
A1 header | A2 | not fooled |
---|---|
C1 | C2 bold |
D1 code | | D2 |
E1 | text | |
F2 without pipe |
not in table
""" let input2 = """ | A1 header | A2 | | --- | --- |""" let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig()) doAssert output2 == """A1 header | A2 |
---|
line block
other line
" 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 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig()) doAssert "Paragraph1
Paragraph2
\n" == output2 let input3 = dedent""" | xxx | yyy | zzz""" let output3 = rstToHtml(input3, {roSupportMarkdown}, defaultConfig()) 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 = rstToHtml(input4, {roSupportMarkdown}, defaultConfig()) 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 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig()) for i in 1..5: doAssert ($i & ". line" & $i) notin output1 doAssert ("
Paragraph1
" in output0 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 = rstToHtml(input0, {roSupportMarkdown}, defaultConfig()) 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 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig()) doAssert "endOfError Test2p.Test paragraph.
" in output2 doAssert "class=\"admonition admonition-error\"" in output2 let input3 = dedent """ .. note:: endOfNote """ let output3 = rstToHtml(input3, {roSupportMarkdown}, defaultConfig()) 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 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig()) doAssert "some definition" in output1 doAssert "Ref. some definition" in output1 suite "RST/Code highlight": test "Basic Python code highlight": let pythonCode = """ .. code-block:: python def f_name(arg=42): print(f"{arg}") """ let expected = """
""" check strip(rstToHtml(pythonCode, {}, newStringTable(modeCaseSensitive))) == strip(expected)def f_name(arg=42): print(f"{arg}")