trstgen.nim 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. discard """
  2. outputsub: ""
  3. """
  4. # tests for rstgen module.
  5. import ../../lib/packages/docutils/rstgen
  6. import ../../lib/packages/docutils/rst
  7. import unittest
  8. suite "YAML syntax highlighting":
  9. test "Basics":
  10. let input = """.. code-block:: yaml
  11. %YAML 1.2
  12. ---
  13. a string: string
  14. a list:
  15. - item 1
  16. - item 2
  17. a map:
  18. ? key
  19. : value
  20. ..."""
  21. let output = rstTohtml(input, {}, defaultConfig())
  22. assert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
  23. <span class="Keyword">---</span>
  24. <span class="StringLit">a string</span><span class="Punctuation">:</span> <span class="StringLit">string</span>
  25. <span class="StringLit">a list</span><span class="Punctuation">:</span>
  26. <span class="Punctuation">-</span> <span class="StringLit">item 1</span>
  27. <span class="Punctuation">-</span> <span class="StringLit">item 2</span>
  28. <span class="StringLit">a map</span><span class="Punctuation">:</span>
  29. <span class="Punctuation">?</span> <span class="StringLit">key</span>
  30. <span class="Punctuation">:</span> <span class="StringLit">value</span>
  31. <span class="Keyword">...</span></pre>"""
  32. test "Block scalars":
  33. let input = """.. code-block:: yaml
  34. a literal block scalar: |
  35. some text
  36. # not a comment
  37. # a comment, since less indented
  38. # another comment
  39. a folded block scalar: >2
  40. some text
  41. # not a comment since indented as specified
  42. # a comment
  43. another literal block scalar:
  44. |+ # comment after header
  45. allowed, since more indented than parent"""
  46. let output = rstToHtml(input, {}, defaultConfig())
  47. assert output == """<pre class = "listing"><span class="StringLit">a literal block scalar</span><span class="Punctuation">:</span> <span class="Command">|</span><span class="Command"></span><span class="LongStringLit">
  48. some text
  49. # not a comment
  50. </span><span class="Comment"># a comment, since less indented</span>
  51. <span class="Comment"># another comment</span>
  52. <span class="StringLit">a folded block scalar</span><span class="Punctuation">:</span> <span class="Command">&gt;2</span><span class="Command"></span><span class="LongStringLit">
  53. some text
  54. # not a comment since indented as specified
  55. </span><span class="Comment"># a comment</span>
  56. <span class="StringLit">another literal block scalar</span><span class="Punctuation">:</span>
  57. <span class="Command">|+</span> <span class="Comment"># comment after header</span><span class="LongStringLit">
  58. allowed, since more indented than parent</span></pre>"""
  59. test "Directives":
  60. let input = """.. code-block:: yaml
  61. %YAML 1.2
  62. ---
  63. %not a directive
  64. ...
  65. %a directive
  66. ...
  67. a string
  68. % not a directive
  69. ...
  70. %TAG ! !foo:"""
  71. let output = rstToHtml(input, {}, defaultConfig())
  72. assert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
  73. <span class="Keyword">---</span>
  74. <span class="StringLit">%not a directive</span>
  75. <span class="Keyword">...</span>
  76. <span class="Directive">%a directive</span>
  77. <span class="Keyword">...</span>
  78. <span class="StringLit">a string</span>
  79. <span class="StringLit">% not a directive</span>
  80. <span class="Keyword">...</span>
  81. <span class="Directive">%TAG ! !foo:</span></pre>"""
  82. test "Flow Style and Numbers":
  83. let input = """.. code-block:: yaml
  84. {
  85. "quoted string": 42,
  86. 'single quoted string': false,
  87. [ list, "with", 'entries' ]: 73.32e-73,
  88. more numbers: [-783, 11e78],
  89. not numbers: [ 42e, 0023, +32.37, 8 ball]
  90. }"""
  91. let output = rstToHtml(input, {}, defaultConfig())
  92. assert output == """<pre class = "listing"><span class="Punctuation">{</span>
  93. <span class="StringLit">&quot;</span><span class="StringLit">quoted string&quot;</span><span class="Punctuation">:</span> <span class="DecNumber">42</span><span class="Punctuation">,</span>
  94. <span class="StringLit">'single quoted string'</span><span class="Punctuation">:</span> <span class="StringLit">false</span><span class="Punctuation">,</span>
  95. <span class="Punctuation">[</span> <span class="StringLit">list</span><span class="Punctuation">,</span> <span class="StringLit">&quot;</span><span class="StringLit">with&quot;</span><span class="Punctuation">,</span> <span class="StringLit">'entries'</span> <span class="Punctuation">]</span><span class="Punctuation">:</span> <span class="FloatNumber">73.32e-73</span><span class="Punctuation">,</span>
  96. <span class="StringLit">more numbers</span><span class="Punctuation">:</span> <span class="Punctuation">[</span><span class="DecNumber">-783</span><span class="Punctuation">,</span> <span class="FloatNumber">11e78</span><span class="Punctuation">]</span><span class="Punctuation">,</span>
  97. <span class="StringLit">not numbers</span><span class="Punctuation">:</span> <span class="Punctuation">[</span> <span class="StringLit">42e</span><span class="Punctuation">,</span> <span class="StringLit">0023</span><span class="Punctuation">,</span> <span class="StringLit">+32.37</span><span class="Punctuation">,</span> <span class="StringLit">8 ball</span><span class="Punctuation">]</span>
  98. <span class="Punctuation">}</span></pre>"""
  99. test "Anchors, Aliases, Tags":
  100. let input = """.. code-block:: yaml
  101. --- !!map
  102. !!str string: !<tag:yaml.org,2002:int> 42
  103. ? &anchor !!seq []:
  104. : !localtag foo
  105. alias: *anchor
  106. """
  107. let output = rstToHtml(input, {}, defaultConfig())
  108. assert output == """<pre class = "listing"><span class="Keyword">---</span> <span class="TagStart">!!map</span>
  109. <span class="TagStart">!!str</span> <span class="StringLit">string</span><span class="Punctuation">:</span> <span class="TagStart">!&lt;tag:yaml.org,2002:int&gt;</span> <span class="DecNumber">42</span>
  110. <span class="Punctuation">?</span> <span class="Label">&amp;anchor</span> <span class="TagStart">!!seq</span> <span class="Punctuation">[</span><span class="Punctuation">]</span><span class="Punctuation">:</span>
  111. <span class="Punctuation">:</span> <span class="TagStart">!localtag</span> <span class="StringLit">foo</span>
  112. <span class="StringLit">alias</span><span class="Punctuation">:</span> <span class="Reference">*anchor</span></pre>"""
  113. test "Edge cases":
  114. let input = """.. code-block:: yaml
  115. ...
  116. %a string:
  117. a:string:not:a:map
  118. ...
  119. not a list:
  120. -2
  121. -3
  122. -4
  123. example.com/not/a#comment:
  124. ?not a map key
  125. """
  126. let output = rstToHtml(input, {}, defaultConfig())
  127. assert output == """<pre class = "listing"><span class="Keyword">...</span>
  128. <span class="StringLit">%a string</span><span class="Punctuation">:</span>
  129. <span class="StringLit">a:string:not:a:map</span>
  130. <span class="Keyword">...</span>
  131. <span class="StringLit">not a list</span><span class="Punctuation">:</span>
  132. <span class="DecNumber">-2</span>
  133. <span class="DecNumber">-3</span>
  134. <span class="DecNumber">-4</span>
  135. <span class="StringLit">example.com/not/a#comment</span><span class="Punctuation">:</span>
  136. <span class="StringLit">?not a map key</span></pre>"""
  137. test "Markdown links":
  138. let
  139. a = rstToHtml("(( [Nim](https://nim-lang.org/) ))", {roSupportMarkdown}, defaultConfig())
  140. b = rstToHtml("(([Nim](https://nim-lang.org/)))", {roSupportMarkdown}, defaultConfig())
  141. c = rstToHtml("[[Nim](https://nim-lang.org/)]", {roSupportMarkdown}, defaultConfig())
  142. assert a == """(( <a class="reference external" href="https://nim-lang.org/">Nim</a> ))"""
  143. assert b == """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))"""
  144. assert c == """[<a class="reference external" href="https://nim-lang.org/">Nim</a>]"""
  145. test "Markdown tables":
  146. let input1 = """
  147. | A1 header | A2 \| not fooled
  148. | :--- | ----: |
  149. | C1 | C2 **bold** | ignored |
  150. | D1 `code \|` | D2 | also ignored
  151. | E1 \| text |
  152. | | F2 without pipe
  153. not in table"""
  154. let output1 = rstToHtml(input1, {roSupportMarkdown}, defaultConfig())
  155. assert output1 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
  156. <tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
  157. <tr><td>D1 <tt class="docutils literal"><span class="pre">code |</span></tt></td><td>D2</td></tr>
  158. <tr><td>E1 | text</td><td></td></tr>
  159. <tr><td></td><td>F2 without pipe</td></tr>
  160. </table><p>not in table</p>
  161. """
  162. let input2 = """
  163. | A1 header | A2 |
  164. | --- | --- |"""
  165. let output2 = rstToHtml(input2, {roSupportMarkdown}, defaultConfig())
  166. assert output2 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2</th></tr>
  167. </table>"""