trstgen.nim 52 KB

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