trstgen.nim 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. outputsub: ""
  4. """
  5. # tests for rstgen module.
  6. import ../../lib/packages/docutils/rstgen
  7. import ../../lib/packages/docutils/rst
  8. import unittest, strutils, strtabs
  9. import std/private/miscdollars
  10. import std/assertions
  11. const
  12. NoSandboxOpts = {roPreferMarkdown, roSupportMarkdown, roNimFile, roSandboxDisabled}
  13. preferMarkdown = {roPreferMarkdown, roSupportMarkdown, roNimFile}
  14. preferRst = {roSupportMarkdown, roNimFile}
  15. proc toHtml(input: string,
  16. rstOptions: RstParseOptions = preferMarkdown,
  17. error: ref string = nil,
  18. warnings: ref seq[string] = nil): string =
  19. ## If `error` is nil then no errors should be generated.
  20. ## The same goes for `warnings`.
  21. result = ""
  22. proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind,
  23. arg: string) =
  24. let mc = msgkind.whichMsgClass
  25. let a = $msgkind % arg
  26. var message: string = ""
  27. toLocation(message, filename, line, col + ColRstOffset)
  28. message.add " $1: $2" % [$mc, a]
  29. if mc == mcError:
  30. if error == nil:
  31. raise newException(EParseError, "[unexpected error] " & message)
  32. error[] = message
  33. # we check only first error because subsequent ones may be meaningless
  34. raise newException(EParseError, "")
  35. else:
  36. doAssert warnings != nil, "unexpected RST warning '" & message & "'"
  37. warnings[].add message
  38. try:
  39. result = rstToHtml(input, rstOptions, defaultConfig(),
  40. msgHandler=testMsgHandler)
  41. except EParseError as e:
  42. if e.msg != "":
  43. result = e.msg
  44. # inline code tags (for parsing originated from highlite.nim)
  45. proc id(str: string): string = """<span class="Identifier">""" & str & "</span>"
  46. proc op(str: string): string = """<span class="Operator">""" & str & "</span>"
  47. proc pu(str: string): string = """<span class="Punctuation">""" & str & "</span>"
  48. proc optionListLabel(opt: string): string =
  49. """<div class="option-list-label"><tt><span class="option">""" &
  50. opt &
  51. "</span></tt></div>"
  52. suite "YAML syntax highlighting":
  53. test "Basics":
  54. let input = """.. code-block:: yaml
  55. %YAML 1.2
  56. ---
  57. a string: string
  58. a list:
  59. - item 1
  60. - item 2
  61. a map:
  62. ? key
  63. : value
  64. ..."""
  65. let output = input.toHtml({})
  66. doAssert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
  67. <span class="Keyword">---</span>
  68. <span class="StringLit">a string</span><span class="Punctuation">:</span> <span class="StringLit">string</span>
  69. <span class="StringLit">a list</span><span class="Punctuation">:</span>
  70. <span class="Punctuation">-</span> <span class="StringLit">item 1</span>
  71. <span class="Punctuation">-</span> <span class="StringLit">item 2</span>
  72. <span class="StringLit">a map</span><span class="Punctuation">:</span>
  73. <span class="Punctuation">?</span> <span class="StringLit">key</span>
  74. <span class="Punctuation">:</span> <span class="StringLit">value</span>
  75. <span class="Keyword">...</span></pre>"""
  76. test "Block scalars":
  77. let input = """.. code-block:: yaml
  78. a literal block scalar: |
  79. some text
  80. # not a comment
  81. # a comment, since less indented
  82. # another comment
  83. a folded block scalar: >2
  84. some text
  85. # not a comment since indented as specified
  86. # a comment
  87. another literal block scalar:
  88. |+ # comment after header
  89. allowed, since more indented than parent"""
  90. let output = input.toHtml({})
  91. doAssert 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">
  92. some text
  93. # not a comment
  94. </span><span class="Comment"># a comment, since less indented</span>
  95. <span class="Comment"># another comment</span>
  96. <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">
  97. some text
  98. # not a comment since indented as specified
  99. </span><span class="Comment"># a comment</span>
  100. <span class="StringLit">another literal block scalar</span><span class="Punctuation">:</span>
  101. <span class="Command">|+</span> <span class="Comment"># comment after header</span><span class="LongStringLit">
  102. allowed, since more indented than parent</span></pre>"""
  103. test "Directives":
  104. let input = """.. code-block:: yaml
  105. %YAML 1.2
  106. ---
  107. %not a directive
  108. ...
  109. %a directive
  110. ...
  111. a string
  112. % not a directive
  113. ...
  114. %TAG ! !foo:"""
  115. let output = input.toHtml({})
  116. doAssert output == """<pre class = "listing"><span class="Directive">%YAML 1.2</span>
  117. <span class="Keyword">---</span>
  118. <span class="StringLit">%not a directive</span>
  119. <span class="Keyword">...</span>
  120. <span class="Directive">%a directive</span>
  121. <span class="Keyword">...</span>
  122. <span class="StringLit">a string</span>
  123. <span class="StringLit">% not a directive</span>
  124. <span class="Keyword">...</span>
  125. <span class="Directive">%TAG ! !foo:</span></pre>"""
  126. test "Flow Style and Numbers":
  127. let input = """.. code-block:: yaml
  128. {
  129. "quoted string": 42,
  130. 'single quoted string': false,
  131. [ list, "with", 'entries' ]: 73.32e-73,
  132. more numbers: [-783, 11e78],
  133. not numbers: [ 42e, 0023, +32.37, 8 ball]
  134. }"""
  135. let output = input.toHtml({})
  136. doAssert output == """<pre class = "listing"><span class="Punctuation">{</span>
  137. <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>
  138. <span class="StringLit">'single quoted string'</span><span class="Punctuation">:</span> <span class="StringLit">false</span><span class="Punctuation">,</span>
  139. <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>
  140. <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>
  141. <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>
  142. <span class="Punctuation">}</span></pre>"""
  143. test "Directives: warnings":
  144. let input = dedent"""
  145. .. non-existent-warning: Paragraph.
  146. .. another.wrong:warning::: Paragraph.
  147. """
  148. var warnings = new seq[string]
  149. let output = input.toHtml(warnings=warnings)
  150. check output == ""
  151. doAssert warnings[].len == 2
  152. check "(1, 24) Warning: RST style:" in warnings[0]
  153. check "double colon :: may be missing at end of 'non-existent-warning'" in warnings[0]
  154. check "(3, 25) Warning: RST style:" in warnings[1]
  155. check "RST style: too many colons for a directive (should be ::)" in warnings[1]
  156. test "not a directive":
  157. let input = "..warning:: I am not a warning."
  158. check input.toHtml == input
  159. test "Anchors, Aliases, Tags":
  160. let input = """.. code-block:: yaml
  161. --- !!map
  162. !!str string: !<tag:yaml.org,2002:int> 42
  163. ? &anchor !!seq []:
  164. : !localtag foo
  165. alias: *anchor
  166. """
  167. let output = input.toHtml({})
  168. doAssert output == """<pre class = "listing"><span class="Keyword">---</span> <span class="TagStart">!!map</span>
  169. <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>
  170. <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>
  171. <span class="Punctuation">:</span> <span class="TagStart">!localtag</span> <span class="StringLit">foo</span>
  172. <span class="StringLit">alias</span><span class="Punctuation">:</span> <span class="Reference">*anchor</span></pre>"""
  173. test "Edge cases":
  174. let input = """.. code-block:: yaml
  175. ...
  176. %a string:
  177. a:string:not:a:map
  178. ...
  179. not a list:
  180. -2
  181. -3
  182. -4
  183. example.com/not/a#comment:
  184. ?not a map key
  185. """
  186. let output = input.toHtml({})
  187. doAssert output == """<pre class = "listing"><span class="Keyword">...</span>
  188. <span class="StringLit">%a string</span><span class="Punctuation">:</span>
  189. <span class="StringLit">a:string:not:a:map</span>
  190. <span class="Keyword">...</span>
  191. <span class="StringLit">not a list</span><span class="Punctuation">:</span>
  192. <span class="DecNumber">-2</span>
  193. <span class="DecNumber">-3</span>
  194. <span class="DecNumber">-4</span>
  195. <span class="StringLit">example.com/not/a#comment</span><span class="Punctuation">:</span>
  196. <span class="StringLit">?not a map key</span></pre>"""
  197. suite "RST/Markdown general":
  198. test "RST emphasis":
  199. doAssert rstToHtml("*Hello* **world**!", {},
  200. newStringTable(modeStyleInsensitive)) ==
  201. "<em>Hello</em> <strong>world</strong>!"
  202. test "Markdown links":
  203. check("(( [Nim](https://nim-lang.org/) ))".toHtml ==
  204. """(( <a class="reference external" href="https://nim-lang.org/">Nim</a> ))""")
  205. check("(([Nim](https://nim-lang.org/)))".toHtml ==
  206. """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))""")
  207. check("[[Nim](https://nim-lang.org/)]".toHtml ==
  208. """[<a class="reference external" href="https://nim-lang.org/">Nim</a>]""")
  209. test "Markdown tables":
  210. let input1 = """
  211. | A1 header | A2 \| not fooled
  212. | :--- | ----: |
  213. | C1 | C2 **bold** | ignored |
  214. | D1 `code \|` | D2 | also ignored
  215. | E1 \| text |
  216. | | F2 without pipe
  217. not in table"""
  218. let output1 = input1.toHtml
  219. #[
  220. TODO: `\|` inside a table cell should render as `|`
  221. `|` outside a table cell should render as `\|`
  222. consistently with markdown, see https://stackoverflow.com/a/66557930/1426932
  223. ]#
  224. check(output1 == """
  225. <table border="1" class="docutils"><tr><th>A1 header</th><th>A2 | not fooled</th></tr>
  226. <tr><td>C1</td><td>C2 <strong>bold</strong></td></tr>
  227. <tr><td>D1 <tt class="docutils literal"><span class="pre">""" & id"code" & " " & op"\|" & """</span></tt></td><td>D2</td></tr>
  228. <tr><td>E1 | text</td><td></td></tr>
  229. <tr><td></td><td>F2 without pipe</td></tr>
  230. </table><p>not in table</p>""")
  231. let input2 = """
  232. | A1 header | A2 |
  233. | --- | --- |"""
  234. let output2 = input2.toHtml
  235. doAssert output2 == """<table border="1" class="docutils"><tr><th>A1 header</th><th>A2</th></tr>
  236. </table>"""
  237. test "RST tables":
  238. let input1 = """
  239. Test 2 column/4 rows table:
  240. ==== ===
  241. H0 H1
  242. ==== ===
  243. A0 A1
  244. ==== ===
  245. A2 A3
  246. ==== ===
  247. A4 A5
  248. ==== === """
  249. let output1 = rstToLatex(input1, {})
  250. doAssert "{LL}" in output1 # 2 columns
  251. doAssert count(output1, "\\\\") == 4 # 4 rows
  252. for cell in ["H0", "H1", "A0", "A1", "A2", "A3", "A4", "A5"]:
  253. doAssert cell in output1
  254. let input2 = """
  255. Now test 3 columns / 2 rows, and also borders containing 4 =, 3 =, 1 = signs:
  256. ==== === =
  257. H0 H1 H
  258. ==== === =
  259. A0 A1 X
  260. Ax Y
  261. ==== === = """
  262. let output2 = rstToLatex(input2, {})
  263. doAssert "{LLL}" in output2 # 3 columns
  264. doAssert count(output2, "\\\\") == 2 # 2 rows
  265. for cell in ["H0", "H1", "H", "A0", "A1", "X", "Ax", "Y"]:
  266. doAssert cell in output2
  267. test "RST adornments":
  268. let input1 = """
  269. Check that a few punctuation symbols are not parsed as adornments:
  270. :word1: word2 .... word3 """
  271. let output1 = input1.toHtml
  272. discard output1
  273. test "RST sections":
  274. let input1 = """
  275. Long chapter name
  276. '''''''''''''''''''
  277. """
  278. let output1 = input1.toHtml
  279. doAssert "Long chapter name" in output1 and "<h1" in output1
  280. let input2 = """
  281. Short chapter name:
  282. ChA
  283. ===
  284. """
  285. let output2 = input2.toHtml
  286. doAssert "ChA" in output2 and "<h1" in output2
  287. let input3 = """
  288. Very short chapter name:
  289. X
  290. ~
  291. """
  292. let output3 = input3.toHtml
  293. doAssert "X" in output3 and "<h1" in output3
  294. let input4 = """
  295. Check that short underline is not enough to make section:
  296. Wrong chapter
  297. ------------
  298. """
  299. var error4 = new string
  300. let output4 = input4.toHtml(error = error4)
  301. check(error4[] == "input(3, 1) Error: new section expected (underline " &
  302. "\'------------\' is too short)")
  303. let input5 = """
  304. Check that punctuation after adornment and indent are not detected as adornment.
  305. Some chapter
  306. --------------
  307. "punctuation symbols" """
  308. let output5 = input5.toHtml
  309. doAssert "&quot;punctuation symbols&quot;" in output5 and "<h1" in output5
  310. # check that EOF after adornment does not prevent it parsing as heading
  311. let input6 = dedent """
  312. Some chapter
  313. ------------"""
  314. let output6 = input6.toHtml
  315. doAssert "<h1 id=\"some-chapter\">Some chapter</h1>" in output6
  316. # check that overline and underline match
  317. let input7 = dedent """
  318. ------------
  319. Some chapter
  320. -----------
  321. """
  322. var error7 = new string
  323. let output7 = input7.toHtml(error=error7)
  324. check(error7[] == "input(1, 1) Error: new section expected (underline " &
  325. "\'-----------\' does not match overline \'------------\')")
  326. let input8 = dedent """
  327. -----------
  328. Overflow
  329. -----------
  330. """
  331. var error8 = new string
  332. let output8 = input8.toHtml(error=error8)
  333. check(error8[] == "input(1, 1) Error: new section expected (overline " &
  334. "\'-----------\' is too short)")
  335. # check that hierarchy of title styles works
  336. let input9good = dedent """
  337. Level1
  338. ======
  339. Level2
  340. ------
  341. Level3
  342. ~~~~~~
  343. L1
  344. ==
  345. Another2
  346. --------
  347. More3
  348. ~~~~~
  349. """
  350. let output9good = input9good.toHtml(preferRst)
  351. doAssert "<h1 id=\"level1\">Level1</h1>" in output9good
  352. doAssert "<h2 id=\"level2\">Level2</h2>" in output9good
  353. doAssert "<h3 id=\"level3\">Level3</h3>" in output9good
  354. doAssert "<h1 id=\"l1\">L1</h1>" in output9good
  355. doAssert "<h2 id=\"another2\">Another2</h2>" in output9good
  356. doAssert "<h3 id=\"more3\">More3</h3>" in output9good
  357. # check that swap causes an exception
  358. let input9Bad = dedent """
  359. Level1
  360. ======
  361. Level2
  362. ------
  363. Level3
  364. ~~~~~~
  365. L1
  366. ==
  367. More
  368. ~~~~
  369. Another
  370. -------
  371. """
  372. var error9Bad = new string
  373. let output9Bad = input9Bad.toHtml(preferRst, error=error9Bad)
  374. check(error9Bad[] == "input(15, 1) Error: new section expected (section " &
  375. "level inconsistent: underline ~~~~~ unexpectedly found, while " &
  376. "the following intermediate section level(s) are missing on " &
  377. "lines 12..15: underline -----)")
  378. test "RST sections overline":
  379. # the same as input9good but with overline headings
  380. # first overline heading has a special meaning: document title
  381. let input = dedent """
  382. ======
  383. Title0
  384. ======
  385. +++++++++
  386. SubTitle0
  387. +++++++++
  388. ------
  389. Level1
  390. ------
  391. Level2
  392. ------
  393. ~~~~~~
  394. Level3
  395. ~~~~~~
  396. --
  397. L1
  398. --
  399. Another2
  400. --------
  401. ~~~~~
  402. More3
  403. ~~~~~
  404. """
  405. var rstGenera: RstGenerator
  406. var output: string
  407. let (rst, files, _) = rstParse(input, "", 1, 1, {})
  408. rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", filenames = files)
  409. rstGenera.renderRstToOut(rst, output)
  410. doAssert rstGenera.meta[metaTitle] == "Title0"
  411. doAssert rstGenera.meta[metaSubtitle] == "SubTitle0"
  412. doAssert "<h1 id=\"level1\"><center>Level1</center></h1>" in output
  413. doAssert "<h2 id=\"level2\">Level2</h2>" in output
  414. doAssert "<h3 id=\"level3\"><center>Level3</center></h3>" in output
  415. doAssert "<h1 id=\"l1\"><center>L1</center></h1>" in output
  416. doAssert "<h2 id=\"another2\">Another2</h2>" in output
  417. doAssert "<h3 id=\"more3\"><center>More3</center></h3>" in output
  418. test "RST sections overline 2":
  419. # check that a paragraph prevents interpreting overlines as document titles
  420. let input = dedent """
  421. Paragraph
  422. ======
  423. Title0
  424. ======
  425. +++++++++
  426. SubTitle0
  427. +++++++++
  428. """
  429. var rstGenera: RstGenerator
  430. var output: string
  431. let (rst, files, _) = rstParse(input, "", 1, 1, {})
  432. rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", filenames=files)
  433. rstGenera.renderRstToOut(rst, output)
  434. doAssert rstGenera.meta[metaTitle] == ""
  435. doAssert rstGenera.meta[metaSubtitle] == ""
  436. doAssert "<h1 id=\"title0\"><center>Title0</center></h1>" in output
  437. doAssert "<h2 id=\"subtitle0\"><center>SubTitle0</center></h2>" in output
  438. test "RST+Markdown sections":
  439. # check that RST and Markdown headings don't interfere
  440. let input = dedent """
  441. ======
  442. Title0
  443. ======
  444. MySection1a
  445. +++++++++++
  446. # MySection1b
  447. MySection1c
  448. +++++++++++
  449. ##### MySection5a
  450. MySection2a
  451. -----------
  452. """
  453. var rstGenera: RstGenerator
  454. var output: string
  455. let (rst, files, _) = rstParse(input, "", 1, 1, {roSupportMarkdown})
  456. rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", filenames=files)
  457. rstGenera.renderRstToOut(rst, output)
  458. doAssert rstGenera.meta[metaTitle] == "Title0"
  459. doAssert rstGenera.meta[metaSubtitle] == ""
  460. doAssert output ==
  461. "\n<h1 id=\"mysection1a\">MySection1a</h1>" & # RST
  462. "\n<h1 id=\"mysection1b\">MySection1b</h1>" & # Markdown
  463. "\n<h1 id=\"mysection1c\">MySection1c</h1>" & # RST
  464. "\n<h5 id=\"mysection5a\">MySection5a</h5>" & # Markdown
  465. "\n<h2 id=\"mysection2a\">MySection2a</h2>" # RST
  466. test "RST inline text":
  467. let input1 = "GC_step"
  468. let output1 = input1.toHtml
  469. doAssert output1 == "GC_step"
  470. test "RST anchors/links to headings":
  471. # Currently in TOC mode anchors are modified (for making links from
  472. # the TOC unique)
  473. let inputNoToc = dedent"""
  474. Type relations
  475. ==============
  476. Convertible relation
  477. --------------------
  478. Ref. `Convertible relation`_
  479. """
  480. let outputNoToc = inputNoToc.toHtml
  481. check outputNoToc.count("id=\"type-relations\"") == 1
  482. check outputNoToc.count("id=\"convertible-relation\"") == 1
  483. check outputNoToc.count("href=\"#convertible-relation\"") == 1
  484. let inputTocCases = @[
  485. dedent"""
  486. .. contents::
  487. Type relations
  488. ==============
  489. Convertible relation
  490. --------------------
  491. Ref. `Convertible relation`_
  492. Guards and locks
  493. ================
  494. """,
  495. dedent"""
  496. Ref. `Convertible relation`_
  497. .. contents::
  498. Type relations
  499. ==============
  500. Convertible relation
  501. --------------------
  502. Guards and locks
  503. ================
  504. """
  505. ]
  506. for inputToc in inputTocCases:
  507. let outputToc = inputToc.toHtml
  508. check outputToc.count("id=\"type-relations\"") == 1
  509. check outputToc.count("id=\"type-relations-convertible-relation\"") == 1
  510. check outputToc.count("id=\"convertible-relation\">") == 0
  511. # Besides "Ref.", heading also contains link to itself:
  512. check outputToc.count(
  513. "href=\"#type-relations-convertible-relation\">") == 2
  514. check outputToc.count("href=\"#convertible-relation\"") == 0
  515. test "RST links":
  516. let input1 = """
  517. Want to learn about `my favorite programming language`_?
  518. .. _my favorite programming language: https://nim-lang.org"""
  519. let output1 = input1.toHtml
  520. doAssert "<a" in output1 and "href=\"https://nim-lang.org\"" in output1
  521. test "RST transitions":
  522. let input1 = """
  523. context1
  524. ~~~~
  525. context2
  526. """
  527. let output1 = input1.toHtml(preferRst)
  528. doAssert "<hr" in output1
  529. let input2 = """
  530. This is too short to be a transition:
  531. ---
  532. context2
  533. ---
  534. """
  535. var error2 = new string
  536. let output2 = input2.toHtml(error=error2)
  537. check(error2[] == "input(3, 1) Error: new section expected (overline " &
  538. "\'---\' is too short)")
  539. test "RST literal block":
  540. let input1 = """
  541. Test literal block
  542. ::
  543. check """
  544. let output1 = input1.toHtml(preferRst)
  545. doAssert "<pre>" in output1
  546. test "Markdown code block":
  547. let input1 = """
  548. ```
  549. let x = 1
  550. ``` """
  551. let output1 = input1.toHtml({roSupportMarkdown, roPreferMarkdown})
  552. doAssert "<pre" in output1 and "class=\"Keyword\"" notin output1
  553. # Check Nim highlighting by default in .nim files:
  554. let output1nim = input1.toHtml({roSupportMarkdown, roPreferMarkdown,
  555. roNimFile})
  556. doAssert "<pre" in output1nim and "class=\"Keyword\"" in output1nim
  557. let input2 = """
  558. Parse the block with language specifier:
  559. ```Nim
  560. let x = 1
  561. ``` """
  562. let output2 = input2.toHtml
  563. doAssert "<pre" in output2 and "class=\"Keyword\"" in output2
  564. test "interpreted text":
  565. check("""`foo.bar`""".toHtml ==
  566. """<tt class="docutils literal"><span class="pre">""" &
  567. id"foo" & op"." & id"bar" & "</span></tt>")
  568. check("""`foo\`\`bar`""".toHtml ==
  569. """<tt class="docutils literal"><span class="pre">""" &
  570. id"foo" & pu"`" & pu"`" & id"bar" & "</span></tt>")
  571. check("""`foo\`bar`""".toHtml ==
  572. """<tt class="docutils literal"><span class="pre">""" &
  573. id"foo" & pu"`" & id"bar" & "</span></tt>")
  574. check("""`\`bar`""".toHtml ==
  575. """<tt class="docutils literal"><span class="pre">""" &
  576. pu"`" & id"bar" & "</span></tt>")
  577. check("""`a\b\x\\ar`""".toHtml ==
  578. """<tt class="docutils literal"><span class="pre">""" &
  579. id"a" & op"""\""" & id"b" & op"""\""" & id"x" & op"""\\""" & id"ar" &
  580. "</span></tt>")
  581. test "inline literal":
  582. check """``foo.bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo.bar</span></tt>"""
  583. check """``foo\bar``""".toHtml == """<tt class="docutils literal"><span class="pre">foo\bar</span></tt>"""
  584. check """``f\`o\\o\b`ar``""".toHtml == """<tt class="docutils literal"><span class="pre">f\`o\\o\b`ar</span></tt>"""
  585. test "default-role":
  586. # nim(default) -> literal -> nim -> code(=literal)
  587. let input = dedent"""
  588. Par1 `value1`.
  589. .. default-role:: literal
  590. Par2 `value2`.
  591. .. default-role:: nim
  592. Par3 `value3`.
  593. .. default-role:: code
  594. Par4 `value4`."""
  595. let p1 = """Par1 <tt class="docutils literal"><span class="pre">""" & id"value1" & "</span></tt>."
  596. let p2 = """<p>Par2 <tt class="docutils literal"><span class="pre">value2</span></tt>.</p>"""
  597. let p3 = """<p>Par3 <tt class="docutils literal"><span class="pre">""" & id"value3" & "</span></tt>.</p>"
  598. let p4 = """<p>Par4 <tt class="docutils literal"><span class="pre">value4</span></tt>.</p>"""
  599. let expected = p1 & p2 & "\n" & p3 & "\n" & p4
  600. check(
  601. input.toHtml(NoSandboxOpts) == expected
  602. )
  603. test "role directive":
  604. let input = dedent"""
  605. .. role:: y(code)
  606. :language: yaml
  607. .. role:: brainhelp(code)
  608. :language: brainhelp
  609. """
  610. var warnings = new seq[string]
  611. let output = input.toHtml(
  612. NoSandboxOpts,
  613. warnings=warnings
  614. )
  615. check(warnings[].len == 1 and "language 'brainhelp' not supported" in warnings[0])
  616. test "RST comments":
  617. let input1 = """
  618. Check that comment disappears:
  619. ..
  620. some comment """
  621. let output1 = input1.toHtml
  622. doAssert output1 == "Check that comment disappears:"
  623. test "RST line blocks + headings":
  624. let input = """
  625. =====
  626. Test1
  627. =====
  628. |
  629. |
  630. | line block
  631. | other line
  632. """
  633. var rstGenera: RstGenerator
  634. var output: string
  635. let (rst, files, _) = rstParse(input, "", 1, 1, {})
  636. rstGenera.initRstGenerator(outHtml, defaultConfig(), "input", filenames=files)
  637. rstGenera.renderRstToOut(rst, output)
  638. doAssert rstGenera.meta[metaTitle] == "Test1"
  639. # check that title was not overwritten to '|'
  640. doAssert output == "<p><br/><br/>line block<br/>other line<br/></p>"
  641. let output1l = rstToLatex(input, {})
  642. doAssert "line block\n\n" in output1l
  643. doAssert "other line\n\n" in output1l
  644. doAssert output1l.count("\\vspace") == 2 + 2 # +2 surrounding paddings
  645. test "RST line blocks":
  646. let input2 = dedent"""
  647. Paragraph1
  648. |
  649. Paragraph2"""
  650. let output2 = input2.toHtml
  651. doAssert "Paragraph1<p><br/></p> <p>Paragraph2</p>" == output2
  652. let input3 = dedent"""
  653. | xxx
  654. | yyy
  655. | zzz"""
  656. let output3 = input3.toHtml
  657. doAssert "xxx<br/>" in output3
  658. doAssert "<span style=\"margin-left: 1.0em\">yyy</span><br/>" in output3
  659. doAssert "<span style=\"margin-left: 2.0em\">zzz</span><br/>" in output3
  660. # check that '| ' with a few spaces is still parsed as new line
  661. let input4 = dedent"""
  662. | xxx
  663. |
  664. | zzz"""
  665. let output4 = input4.toHtml
  666. doAssert "xxx<br/><br/>" in output4
  667. doAssert "<span style=\"margin-left: 2.0em\">zzz</span><br/>" in output4
  668. test "RST enumerated lists":
  669. let input1 = dedent """
  670. 1. line1
  671. 1
  672. 2. line2
  673. 2
  674. 3. line3
  675. 3
  676. 4. line4
  677. 4
  678. 5. line5
  679. 5
  680. """
  681. let output1 = input1.toHtml
  682. for i in 1..5:
  683. doAssert ($i & ". line" & $i) notin output1
  684. doAssert ("<li>line" & $i & " " & $i & "</li>") in output1
  685. let input2 = dedent """
  686. 3. line3
  687. 4. line4
  688. 5. line5
  689. 7. line7
  690. 8. line8
  691. """
  692. let output2 = input2.toHtml
  693. for i in [3, 4, 5, 7, 8]:
  694. doAssert ($i & ". line" & $i) notin output2
  695. doAssert ("<li>line" & $i & "</li>") in output2
  696. # check that nested enumerated lists work
  697. let input3 = dedent """
  698. 1. a) string1
  699. 2. string2
  700. """
  701. let output3 = input3.toHtml
  702. doAssert count(output3, "<ol ") == 2
  703. doAssert count(output3, "</ol>") == 2
  704. doAssert "<li>string1</li>" in output3 and "<li>string2</li>" in output3
  705. let input4 = dedent """
  706. Check that enumeration specifiers are respected
  707. 9. string1
  708. 10. string2
  709. 12. string3
  710. b) string4
  711. c) string5
  712. e) string6
  713. """
  714. let output4 = input4.toHtml
  715. doAssert count(output4, "<ol ") == 4
  716. doAssert count(output4, "</ol>") == 4
  717. for enumerator in [9, 12]:
  718. doAssert "start=\"$1\"" % [$enumerator] in output4
  719. for enumerator in [2, 5]: # 2=b, 5=e
  720. doAssert "start=\"$1\"" % [$enumerator] in output4
  721. let input5 = dedent """
  722. Check that auto-numbered enumeration lists work.
  723. #. string1
  724. #. string2
  725. #. string3
  726. #) string5
  727. #) string6
  728. """
  729. let output5 = input5.toHtml
  730. doAssert count(output5, "<ol ") == 2
  731. doAssert count(output5, "</ol>") == 2
  732. doAssert count(output5, "<li>") == 5
  733. let input5a = dedent """
  734. Auto-numbered RST list can start with 1 even when Markdown support is on.
  735. 1. string1
  736. #. string2
  737. #. string3
  738. """
  739. let output5a = input5a.toHtml
  740. doAssert count(output5a, "<ol ") == 1
  741. doAssert count(output5a, "</ol>") == 1
  742. doAssert count(output5a, "<li>") == 3
  743. let input6 = dedent """
  744. ... And for alphabetic enumerators too!
  745. b. string1
  746. #. string2
  747. #. string3
  748. """
  749. let output6 = input6.toHtml
  750. doAssert count(output6, "<ol ") == 1
  751. doAssert count(output6, "</ol>") == 1
  752. doAssert count(output6, "<li>") == 3
  753. doAssert "start=\"2\"" in output6 and "class=\"loweralpha simple\"" in output6
  754. let input7 = dedent """
  755. ... And for uppercase alphabetic enumerators.
  756. C. string1
  757. #. string2
  758. #. string3
  759. """
  760. let output7 = input7.toHtml
  761. doAssert count(output7, "<ol ") == 1
  762. doAssert count(output7, "</ol>") == 1
  763. doAssert count(output7, "<li>") == 3
  764. doAssert "start=\"3\"" in output7 and "class=\"upperalpha simple\"" in output7
  765. # check that it's not recognized as enum.list without indentation on 2nd line
  766. let input8 = dedent """
  767. Paragraph.
  768. A. stringA
  769. B. stringB
  770. C. string1
  771. string2
  772. """
  773. var warnings8 = new seq[string]
  774. let output8 = input8.toHtml(warnings = warnings8)
  775. check(warnings8[].len == 1)
  776. check("input(6, 1) Warning: RST style: \n" &
  777. "not enough indentation on line 6" in warnings8[0])
  778. doAssert output8 == "Paragraph.<ol class=\"upperalpha simple\">" &
  779. "<li>stringA</li>\n<li>stringB</li>\n</ol>\n<p>C. string1 string2 </p>"
  780. test "Markdown enumerated lists":
  781. let input1 = dedent """
  782. Below are 2 enumerated lists: Markdown-style (5 items) and RST (1 item)
  783. 1. line1
  784. 1. line2
  785. 1. line3
  786. 1. line4
  787. 1. line5
  788. #. lineA
  789. """
  790. let output1 = input1.toHtml
  791. for i in 1..5:
  792. doAssert ($i & ". line" & $i) notin output1
  793. doAssert ("<li>line" & $i & "</li>") in output1
  794. doAssert count(output1, "<ol ") == 2
  795. doAssert count(output1, "</ol>") == 2
  796. test "RST bullet lists":
  797. let input1 = dedent """
  798. * line1
  799. 1
  800. * line2
  801. 2
  802. * line3
  803. 3
  804. * line4
  805. 4
  806. * line5
  807. 5
  808. """
  809. let output1 = input1.toHtml
  810. for i in 1..5:
  811. doAssert ("<li>line" & $i & " " & $i & "</li>") in output1
  812. doAssert count(output1, "<ul ") == 1
  813. doAssert count(output1, "</ul>") == 1
  814. test "Nim RST footnotes and citations":
  815. # check that auto-label footnote enumerated properly after a manual one
  816. let input1 = dedent """
  817. .. [1] Body1.
  818. .. [#note] Body2
  819. Ref. [#note]_
  820. """
  821. let output1 = input1.toHtml(preferRst)
  822. doAssert output1.count(">[1]</a>") == 1
  823. doAssert output1.count(">[2]</a>") == 2
  824. doAssert "href=\"#footnote-note\"" in output1
  825. doAssert ">[-1]" notin output1
  826. doAssert "Body1." in output1
  827. doAssert "Body2" in output1
  828. # check that there are NO footnotes/citations, only comments:
  829. let input2 = dedent """
  830. .. [1 #] Body1.
  831. .. [# note] Body2.
  832. .. [wrong citation] That gives you a comment.
  833. .. [not&allowed] That gives you a comment.
  834. Not references[#note]_[1 #]_ [wrong citation]_ and [not&allowed]_.
  835. """
  836. let output2 = input2.toHtml(preferRst)
  837. doAssert output2 == "Not references[#note]_[1 #]_ [wrong citation]_ and [not&amp;allowed]_."
  838. # check that auto-symbol footnotes work:
  839. let input3 = dedent """
  840. Ref. [*]_ and [*]_ and [*]_.
  841. .. [*] Body1
  842. .. [*] Body2.
  843. .. [*] Body3.
  844. .. [*] Body4
  845. And [*]_.
  846. """
  847. let output3 = input3.toHtml(preferRst)
  848. # both references and footnotes. Footnotes have link to themselves.
  849. doAssert output3.count("href=\"#footnotesym-1\">[*]</a>") == 2
  850. doAssert output3.count("href=\"#footnotesym-2\">[**]</a>") == 2
  851. doAssert output3.count("href=\"#footnotesym-3\">[***]</a>") == 2
  852. doAssert output3.count("href=\"#footnotesym-4\">[^]</a>") == 2
  853. # footnote group
  854. doAssert output3.count("<hr class=\"footnote\">" &
  855. "<div class=\"footnote-group\">") == 1
  856. # footnotes
  857. doAssert output3.count("<div class=\"footnote-label\"><sup><strong>" &
  858. "<a href=\"#footnotesym-1\">[*]</a></strong></sup></div>") == 1
  859. doAssert output3.count("<div class=\"footnote-label\"><sup><strong>" &
  860. "<a href=\"#footnotesym-2\">[**]</a></strong></sup></div>") == 1
  861. doAssert output3.count("<div class=\"footnote-label\"><sup><strong>" &
  862. "<a href=\"#footnotesym-3\">[***]</a></strong></sup></div>") == 1
  863. doAssert output3.count("<div class=\"footnote-label\"><sup><strong>" &
  864. "<a href=\"#footnotesym-4\">[^]</a></strong></sup></div>") == 1
  865. for i in 1 .. 4: doAssert ("Body" & $i) in output3
  866. # check manual, auto-number and auto-label footnote enumeration
  867. let input4 = dedent """
  868. .. [3] Manual1.
  869. .. [#] Auto-number1.
  870. .. [#mylabel] Auto-label1.
  871. .. [#note] Auto-label2.
  872. .. [#] Auto-number2.
  873. Ref. [#note]_ and [#]_ and [#]_.
  874. """
  875. let output4 = input4.toHtml(preferRst)
  876. doAssert ">[-1]" notin output1
  877. let order = @[
  878. "footnote-3", "[3]", "Manual1.",
  879. "footnoteauto-1", "[1]", "Auto-number1",
  880. "footnote-mylabel", "[2]", "Auto-label1",
  881. "footnote-note", "[4]", "Auto-label2",
  882. "footnoteauto-2", "[5]", "Auto-number2",
  883. ]
  884. for i in 0 .. order.len-2:
  885. let pos1 = output4.find(order[i])
  886. let pos2 = output4.find(order[i+1])
  887. doAssert pos1 >= 0
  888. doAssert pos2 >= 0
  889. doAssert pos1 < pos2
  890. # forgot [#]_
  891. let input5 = dedent """
  892. .. [3] Manual1.
  893. .. [#] Auto-number1.
  894. .. [#note] Auto-label2.
  895. Ref. [#note]_
  896. """
  897. var error5 = new string
  898. let output5 = input5.toHtml(preferRst, error=error5)
  899. check(error5[] == "input(1, 1) Error: mismatch in number of footnotes " &
  900. "and their refs: 1 (lines 2) != 0 (lines ) for auto-numbered " &
  901. "footnotes")
  902. # extra [*]_
  903. let input6 = dedent """
  904. Ref. [*]_
  905. .. [*] Auto-Symbol.
  906. Ref. [*]_
  907. """
  908. var error6 = new string
  909. let output6 = input6.toHtml(preferRst, error=error6)
  910. check(error6[] == "input(1, 1) Error: mismatch in number of footnotes " &
  911. "and their refs: 1 (lines 3) != 2 (lines 2, 6) for auto-symbol " &
  912. "footnotes")
  913. let input7 = dedent """
  914. .. [Some:CITATION-2020] Citation.
  915. Ref. [some:citation-2020]_.
  916. """
  917. let output7 = input7.toHtml(preferRst)
  918. doAssert output7.count("href=\"#citation-somecoloncitationminus2020\"") == 2
  919. doAssert output7.count("[Some:CITATION-2020]") == 1
  920. doAssert output7.count("[some:citation-2020]") == 1
  921. doAssert output3.count("<hr class=\"footnote\">" &
  922. "<div class=\"footnote-group\">") == 1
  923. let input8 = dedent """
  924. .. [Some] Citation.
  925. Ref. [som]_.
  926. """
  927. var warnings8 = new seq[string]
  928. let output8 = input8.toHtml(preferRst, warnings=warnings8)
  929. check(warnings8[] == @["input(3, 7) Warning: broken link 'citation-som'"])
  930. # check that footnote group does not break parsing of other directives:
  931. let input9 = dedent """
  932. .. [Some] Citation.
  933. .. _`internal anchor`:
  934. .. [Another] Citation.
  935. .. just comment.
  936. .. [Third] Citation.
  937. Paragraph1.
  938. Paragraph2 ref `internal anchor`_.
  939. """
  940. let output9 = input9.toHtml(preferRst)
  941. # _`internal anchor` got erased:
  942. check "href=\"#internal-anchor\"" notin output9
  943. check "href=\"#citation-another\"" in output9
  944. doAssert output9.count("<hr class=\"footnote\">" &
  945. "<div class=\"footnote-group\">") == 1
  946. doAssert output9.count("<div class=\"footnote-label\">") == 3
  947. doAssert "just comment" notin output9
  948. # check that nested citations/footnotes work
  949. let input10 = dedent """
  950. Paragraph1 [#]_.
  951. .. [First] Citation.
  952. .. [#] Footnote.
  953. .. [Third] Citation.
  954. """
  955. let output10 = input10.toHtml(preferRst)
  956. doAssert output10.count("<hr class=\"footnote\">" &
  957. "<div class=\"footnote-group\">") == 3
  958. doAssert output10.count("<div class=\"footnote-label\">") == 3
  959. doAssert "<a href=\"#citation-first\">[First]</a>" in output10
  960. doAssert "<a href=\"#footnoteauto-1\">[1]</a>" in output10
  961. doAssert "<a href=\"#citation-third\">[Third]</a>" in output10
  962. let input11 = ".. [note]\n" # should not crash
  963. let output11 = input11.toHtml(preferRst)
  964. doAssert "<a href=\"#citation-note\">[note]</a>" in output11
  965. # check that references to auto-numbered footnotes work
  966. let input12 = dedent """
  967. Ref. [#]_ and [#]_ STOP.
  968. .. [#] Body1.
  969. .. [#] Body3
  970. .. [2] Body2.
  971. """
  972. let output12 = input12.toHtml(preferRst)
  973. let orderAuto = @[
  974. "#footnoteauto-1", "[1]",
  975. "#footnoteauto-2", "[3]",
  976. "STOP.",
  977. "Body1.", "Body3", "Body2."
  978. ]
  979. for i in 0 .. orderAuto.len-2:
  980. let pos1 = output12.find(orderAuto[i])
  981. let pos2 = output12.find(orderAuto[i+1])
  982. doAssert pos1 >= 0
  983. doAssert pos2 >= 0
  984. doAssert pos1 < pos2
  985. test "Nim (RST extension) code-block":
  986. # check that presence of fields doesn't consume the following text as
  987. # its code (which is a literal block)
  988. let input0 = dedent """
  989. .. code-block:: nim
  990. :number-lines: 0
  991. Paragraph1"""
  992. let output0 = input0.toHtml
  993. doAssert "<p>Paragraph1</p>" in output0
  994. test "Nim code-block :number-lines:":
  995. let input = dedent """
  996. .. code-block:: nim
  997. :number-lines: 55
  998. x
  999. y
  1000. """
  1001. check "<pre class=\"line-nums\">55\n56\n</pre>" in input.toHtml
  1002. test "Nim code-block indentation":
  1003. let input = dedent """
  1004. .. code-block:: nim
  1005. :number-lines: 55
  1006. x
  1007. """
  1008. let output = input.toHtml
  1009. check "<pre class=\"line-nums\">55\n</pre>" in output
  1010. check "<span class=\"Identifier\">x</span>" in output
  1011. test "Nim code-block indentation":
  1012. let input = dedent """
  1013. .. code-block:: nim
  1014. :number-lines: 55
  1015. let a = 1
  1016. """
  1017. var error = new string
  1018. let output = input.toHtml(error=error)
  1019. check(error[] == "input(2, 3) Error: invalid field: " &
  1020. "extra arguments were given to number-lines: ' let a = 1'")
  1021. check "" == output
  1022. test "code-block warning":
  1023. let input = dedent """
  1024. .. code:: Nim
  1025. :unsupportedField: anything
  1026. .. code:: unsupportedLang
  1027. anything
  1028. ```anotherLang
  1029. someCode
  1030. ```
  1031. """
  1032. let warnings = new seq[string]
  1033. let output = input.toHtml(warnings=warnings)
  1034. check(warnings[] == @[
  1035. "input(2, 4) Warning: field 'unsupportedField' not supported",
  1036. "input(4, 11) Warning: language 'unsupportedLang' not supported",
  1037. "input(8, 4) Warning: language 'anotherLang' not supported"
  1038. ])
  1039. check(output == "<pre class = \"listing\">anything</pre>" &
  1040. "<p><pre class = \"listing\">someCode</pre> </p>")
  1041. test "RST admonitions":
  1042. # check that all admonitions are implemented
  1043. let input0 = dedent """
  1044. .. admonition:: endOf admonition
  1045. .. attention:: endOf attention
  1046. .. caution:: endOf caution
  1047. .. danger:: endOf danger
  1048. .. error:: endOf error
  1049. .. hint:: endOf hint
  1050. .. important:: endOf important
  1051. .. note:: endOf note
  1052. .. tip:: endOf tip
  1053. .. warning:: endOf warning
  1054. """
  1055. let output0 = input0.toHtml(
  1056. NoSandboxOpts
  1057. )
  1058. for a in ["admonition", "attention", "caution", "danger", "error", "hint",
  1059. "important", "note", "tip", "warning" ]:
  1060. doAssert "endOf " & a & "</div>" in output0
  1061. # Test that admonition does not swallow up the next paragraph.
  1062. let input1 = dedent """
  1063. .. error:: endOfError
  1064. Test paragraph.
  1065. """
  1066. let output1 = input1.toHtml(
  1067. NoSandboxOpts
  1068. )
  1069. doAssert "endOfError</div>" in output1
  1070. doAssert "<p>Test paragraph. </p>" in output1
  1071. doAssert "class=\"admonition admonition-error\"" in output1
  1072. # Test that second line is parsed as continuation of the first line.
  1073. let input2 = dedent """
  1074. .. error:: endOfError
  1075. Test2p.
  1076. Test paragraph.
  1077. """
  1078. let output2 = input2.toHtml(
  1079. NoSandboxOpts
  1080. )
  1081. doAssert "endOfError Test2p.</div>" in output2
  1082. doAssert "<p>Test paragraph. </p>" in output2
  1083. doAssert "class=\"admonition admonition-error\"" in output2
  1084. let input3 = dedent """
  1085. .. note:: endOfNote
  1086. """
  1087. let output3 = input3.toHtml(
  1088. NoSandboxOpts
  1089. )
  1090. doAssert "endOfNote</div>" in output3
  1091. doAssert "class=\"admonition admonition-info\"" in output3
  1092. test "RST internal links":
  1093. let input1 = dedent """
  1094. Start.
  1095. .. _target000:
  1096. Paragraph.
  1097. .. _target001:
  1098. * bullet list
  1099. * Y
  1100. .. _target002:
  1101. 1. enumeration list
  1102. 2. Y
  1103. .. _target003:
  1104. term 1
  1105. Definition list 1.
  1106. .. _target004:
  1107. | line block
  1108. .. _target005:
  1109. :a: field list value
  1110. .. _target006:
  1111. -a option description
  1112. .. _target007:
  1113. ::
  1114. Literal block
  1115. .. _target008:
  1116. Doctest blocks are not implemented.
  1117. .. _target009:
  1118. block quote
  1119. .. _target010:
  1120. ===== ===== =======
  1121. A B A and B
  1122. ===== ===== =======
  1123. False False False
  1124. ===== ===== =======
  1125. .. _target100:
  1126. .. CAUTION:: admonition
  1127. .. _target101:
  1128. .. code:: nim
  1129. const pi = 3.14
  1130. .. _target102:
  1131. .. code-block::
  1132. const pi = 3.14
  1133. Paragraph2.
  1134. .. _target202:
  1135. ----
  1136. That was a transition.
  1137. """
  1138. let output1 = input1.toHtml(
  1139. preferRst
  1140. )
  1141. doAssert "<p id=\"target000\"" in output1
  1142. doAssert "<ul id=\"target001\"" in output1
  1143. doAssert "<ol id=\"target002\"" in output1
  1144. doAssert "<dl id=\"target003\"" in output1
  1145. doAssert "<p id=\"target004\"" in output1
  1146. doAssert "<table id=\"target005\"" in output1 # field list
  1147. doAssert "<div id=\"target006\"" in output1 # option list
  1148. doAssert "<pre id=\"target007\"" in output1
  1149. doAssert "<blockquote id=\"target009\"" in output1
  1150. doAssert "<table id=\"target010\"" in output1 # just table
  1151. doAssert "<span id=\"target100\"" in output1
  1152. doAssert "<pre id=\"target101\"" in output1 # code
  1153. doAssert "<pre id=\"target102\"" in output1 # code-block
  1154. doAssert "<hr id=\"target202\"" in output1
  1155. test "RST internal links for sections":
  1156. let input1 = dedent """
  1157. .. _target101:
  1158. .. _target102:
  1159. Section xyz
  1160. -----------
  1161. Ref. target101_
  1162. """
  1163. let output1 = input1.toHtml
  1164. # "target101" should be erased and changed to "section-xyz":
  1165. check "href=\"#target101\"" notin output1
  1166. check "id=\"target101\"" notin output1
  1167. check "href=\"#target102\"" notin output1
  1168. check "id=\"target102\"" notin output1
  1169. check "id=\"section-xyz\"" in output1
  1170. check "href=\"#section-xyz\"" in output1
  1171. let input2 = dedent """
  1172. .. _target300:
  1173. Section xyz
  1174. ===========
  1175. .. _target301:
  1176. SubsectionA
  1177. -----------
  1178. Ref. target300_ and target301_.
  1179. .. _target103:
  1180. .. [cit2020] note.
  1181. Ref. target103_.
  1182. """
  1183. let output2 = input2.toHtml(preferRst)
  1184. # "target101" should be erased and changed to "section-xyz":
  1185. doAssert "href=\"#target300\"" notin output2
  1186. doAssert "id=\"target300\"" notin output2
  1187. doAssert "href=\"#target301\"" notin output2
  1188. doAssert "id=\"target301\"" notin output2
  1189. doAssert "<h1 id=\"section-xyz\"" in output2
  1190. doAssert "<h2 id=\"subsectiona\"" in output2
  1191. # links should preserve their original names but point to section labels:
  1192. doAssert "href=\"#section-xyz\">target300" in output2
  1193. doAssert "href=\"#subsectiona\">target301" in output2
  1194. doAssert "href=\"#citation-cit2020\">target103" in output2
  1195. let output2l = rstToLatex(input2, {})
  1196. doAssert "\\label{section-xyz}\\hypertarget{section-xyz}{}" in output2l
  1197. doAssert "\\hyperlink{section-xyz}{target300}" in output2l
  1198. doAssert "\\hyperlink{subsectiona}{target301}" in output2l
  1199. test "RST internal links (inline)":
  1200. let input1 = dedent """
  1201. Paragraph with _`some definition`.
  1202. Ref. `some definition`_.
  1203. """
  1204. let output1 = input1.toHtml
  1205. doAssert "<span class=\"target\" " &
  1206. "id=\"some-definition\">some definition</span>" in output1
  1207. doAssert "Ref. <a class=\"reference internal\" " &
  1208. "href=\"#some-definition\">some definition</a>" in output1
  1209. test "RST references (additional symbols)":
  1210. # check that ., _, -, +, : are allowed symbols in references without ` `
  1211. let input1 = dedent """
  1212. sec.1
  1213. -----
  1214. 2-other:sec+c_2
  1215. ^^^^^^^^^^^^^^^
  1216. .. _link.1_2021:
  1217. Paragraph
  1218. Ref. sec.1_! and 2-other:sec+c_2_;and link.1_2021_.
  1219. """
  1220. let output1 = input1.toHtml
  1221. doAssert "id=\"secdot1\"" in output1
  1222. doAssert "id=\"Z2minusothercolonsecplusc-2\"" in output1
  1223. check "id=\"linkdot1-2021\"" in output1
  1224. let ref1 = "<a class=\"reference internal\" href=\"#secdot1\">sec.1</a>"
  1225. let ref2 = "<a class=\"reference internal\" href=\"#Z2minusothercolonsecplusc-2\">2-other:sec+c_2</a>"
  1226. let ref3 = "<a class=\"reference internal\" href=\"#linkdot1-2021\">link.1_2021</a>"
  1227. let refline = "Ref. " & ref1 & "! and " & ref2 & ";and " & ref3 & "."
  1228. doAssert refline in output1
  1229. test "Option lists 1":
  1230. # check that "* b" is not consumed by previous bullet item because of
  1231. # incorrect indentation handling in option lists
  1232. let input = dedent """
  1233. * a
  1234. -m desc
  1235. -n very long
  1236. desc
  1237. * b"""
  1238. let output = input.toHtml
  1239. check(output.count("<ul") == 1)
  1240. check(output.count("<li>") == 2)
  1241. check(output.count("<div class=\"option-list\"") == 1)
  1242. check(optionListLabel("-m") &
  1243. """<div class="option-list-description">desc</div></div>""" in
  1244. output)
  1245. check(optionListLabel("-n") &
  1246. """<div class="option-list-description">very long desc</div></div>""" in
  1247. output)
  1248. test "Option lists 2":
  1249. # check that 2nd option list is not united with the 1st
  1250. let input = dedent """
  1251. * a
  1252. -m desc
  1253. -n very long
  1254. desc
  1255. -d option"""
  1256. let output = input.toHtml
  1257. check(output.count("<ul") == 1)
  1258. check output.count("<div class=\"option-list\"") == 2
  1259. check(optionListLabel("-m") &
  1260. """<div class="option-list-description">desc</div></div>""" in
  1261. output)
  1262. check(optionListLabel("-n") &
  1263. """<div class="option-list-description">very long desc</div></div>""" in
  1264. output)
  1265. check(optionListLabel("-d") &
  1266. """<div class="option-list-description">option</div></div>""" in
  1267. output)
  1268. check "<p>option</p>" notin output
  1269. test "Option list 3 (double /)":
  1270. let input = dedent """
  1271. * a
  1272. //compile compile1
  1273. //doc doc1
  1274. cont
  1275. -d option"""
  1276. let output = input.toHtml
  1277. check(output.count("<ul") == 1)
  1278. check output.count("<div class=\"option-list\"") == 2
  1279. check(optionListLabel("compile") &
  1280. """<div class="option-list-description">compile1</div></div>""" in
  1281. output)
  1282. check(optionListLabel("doc") &
  1283. """<div class="option-list-description">doc1 cont</div></div>""" in
  1284. output)
  1285. check(optionListLabel("-d") &
  1286. """<div class="option-list-description">option</div></div>""" in
  1287. output)
  1288. check "<p>option</p>" notin output
  1289. test "Roles: subscript prefix/postfix":
  1290. let expected = "See <sub>some text</sub>."
  1291. check "See :subscript:`some text`.".toHtml == expected
  1292. check "See `some text`:subscript:.".toHtml == expected
  1293. test "Roles: correct parsing from beginning of line":
  1294. let expected = "<sup>3</sup>He is an isotope of helium."
  1295. check """:superscript:`3`\ He is an isotope of helium.""".toHtml == expected
  1296. check """:sup:`3`\ He is an isotope of helium.""".toHtml == expected
  1297. check """`3`:sup:\ He is an isotope of helium.""".toHtml == expected
  1298. check """`3`:superscript:\ He is an isotope of helium.""".toHtml == expected
  1299. test "Roles: warnings":
  1300. let input = dedent"""
  1301. See function :py:func:`spam`.
  1302. See also `egg`:py:class:.
  1303. """
  1304. var warnings = new seq[string]
  1305. let output = input.toHtml(warnings=warnings)
  1306. doAssert warnings[].len == 2
  1307. check "(1, 14) Warning: " in warnings[0]
  1308. check "language 'py:func' not supported" in warnings[0]
  1309. check "(3, 15) Warning: " in warnings[1]
  1310. check "language 'py:class' not supported" in warnings[1]
  1311. check("""<p>See function <span class="py:func">spam</span>.</p>""" & "\n" &
  1312. """<p>See also <span class="py:class">egg</span>. </p>""" ==
  1313. output)
  1314. test "(not) Roles: check escaping 1":
  1315. let expected = """See :subscript:<tt class="docutils literal">""" &
  1316. """<span class="pre">""" & id"some" & " " & id"text" &
  1317. "</span></tt>."
  1318. check """See \:subscript:`some text`.""".toHtml == expected
  1319. check """See :subscript\:`some text`.""".toHtml == expected
  1320. test "(not) Roles: check escaping 2":
  1321. check("""See :subscript:\`some text\`.""".toHtml ==
  1322. "See :subscript:`some text`.")
  1323. test "Field list":
  1324. check(":field: text".toHtml ==
  1325. """<table class="docinfo" frame="void" rules="none">""" &
  1326. """<col class="docinfo-name" /><col class="docinfo-content" />""" &
  1327. """<tbody valign="top"><tr><th class="docinfo-name">field:</th>""" &
  1328. """<td>text</td></tr>""" & "\n</tbody></table>")
  1329. test "Field list: body after newline":
  1330. let output = dedent"""
  1331. :field:
  1332. text1""".toHtml
  1333. check "<table class=\"docinfo\"" in output
  1334. check ">field:</th>" in output
  1335. check "<td>text1</td>" in output
  1336. test "Field list (incorrect)":
  1337. check ":field:text".toHtml == ":field:text"
  1338. suite "RST/Code highlight":
  1339. test "Basic Python code highlight":
  1340. let pythonCode = """
  1341. .. code-block:: python
  1342. def f_name(arg=42):
  1343. print(f"{arg}")
  1344. """
  1345. let expected = """<blockquote><p><span class="Keyword">def</span> <span class="Identifier">f_name</span><span class="Punctuation">(</span><span class="Identifier">arg</span><span class="Operator">=</span><span class="DecNumber">42</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  1346. <span class="Identifier">print</span><span class="Punctuation">(</span><span class="RawData">f&quot;{arg}&quot;</span><span class="Punctuation">)</span></p></blockquote>"""
  1347. check strip(rstToHtml(pythonCode, {}, newStringTable(modeCaseSensitive))) ==
  1348. strip(expected)
  1349. suite "invalid targets":
  1350. test "invalid image target":
  1351. let input1 = dedent """.. image:: /images/myimage.jpg
  1352. :target: https://bar.com
  1353. :alt: Alt text for the image"""
  1354. let output1 = input1.toHtml
  1355. check output1 == """<a class="reference external" href="https://bar.com"><img src="/images/myimage.jpg" alt="Alt text for the image"/></a>"""
  1356. let input2 = dedent """.. image:: /images/myimage.jpg
  1357. :target: javascript://bar.com
  1358. :alt: Alt text for the image"""
  1359. let output2 = input2.toHtml
  1360. check output2 == """<img src="/images/myimage.jpg" alt="Alt text for the image"/>"""
  1361. let input3 = dedent """.. image:: /images/myimage.jpg
  1362. :target: bar.com
  1363. :alt: Alt text for the image"""
  1364. let output3 = input3.toHtml
  1365. check output3 == """<a class="reference external" href="bar.com"><img src="/images/myimage.jpg" alt="Alt text for the image"/></a>"""
  1366. test "invalid links":
  1367. check("(([Nim](https://nim-lang.org/)))".toHtml ==
  1368. """((<a class="reference external" href="https://nim-lang.org/">Nim</a>))""")
  1369. # unknown protocol is treated just like plain text, not a link
  1370. var warnings = new seq[string]
  1371. check("(([Nim](javascript://nim-lang.org/)))".toHtml(warnings=warnings) ==
  1372. """(([Nim](javascript://nim-lang.org/)))""")
  1373. check(warnings[] == @["input(1, 9) Warning: broken link 'javascript'"])
  1374. warnings[].setLen 0
  1375. check("`Nim <javascript://nim-lang.org/>`_".toHtml(warnings=warnings) ==
  1376. """Nim &lt;javascript://nim-lang.org/&gt;""")
  1377. check(warnings[] == @["input(1, 33) Warning: broken link 'javascript'"])
  1378. suite "local file inclusion":
  1379. test "cannot include files in sandboxed mode":
  1380. var error = new string
  1381. discard ".. include:: ./readme.md".toHtml(error=error)
  1382. check(error[] == "input(1, 11) Error: disabled directive: 'include'")
  1383. test "code-block file directive is disabled":
  1384. var error = new string
  1385. discard ".. code-block:: nim\n :file: ./readme.md".toHtml(error=error)
  1386. check(error[] == "input(2, 20) Error: disabled directive: 'file'")
  1387. test "code-block file directive is disabled - Markdown":
  1388. var error = new string
  1389. discard "```nim file = ./readme.md\n```".toHtml(error=error)
  1390. check(error[] == "input(1, 23) Error: disabled directive: 'file'")
  1391. proc documentToHtml*(doc: string, isMarkdown: bool = false): string {.gcsafe.} =
  1392. var options = {roSupportMarkdown}
  1393. if isMarkdown:
  1394. options.incl roPreferMarkdown
  1395. result = rstToHtml(doc, options, defaultConfig())