1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
discard """
output: '''
[Suite] RST indentation
[Suite] RST include directive
'''
"""
# tests for rst module
import ../../lib/packages/docutils/rstgen
import ../../lib/packages/docutils/rst
import ../../lib/packages/docutils/rstast
import unittest, strutils
import std/private/miscdollars
import os
proc toAst(input: string,
rstOptions: RstParseOptions = {roSupportMarkdown, roNimFile},
error: ref string = nil,
warnings: ref seq[string] = nil): string =
## If `error` is nil then no errors should be generated.
## The same goes for `warnings`.
proc testMsgHandler(filename: string, line, col: int, msgkind: MsgKind,
arg: string) =
let mc = msgkind.whichMsgClass
let a = $msgkind % arg
var message: string
toLocation(message, filename, line, col + ColRstOffset)
message.add " $1: $2" % [$mc, a]
if mc == mcError:
doAssert error != nil, "unexpected RST error '" & message & "'"
error[] = message
# we check only first error because subsequent ones may be meaningless
raise newException(EParseError, message)
else:
doAssert warnings != nil, "unexpected RST warning '" & message & "'"
warnings[].add message
try:
const filen = "input"
proc myFindFile(filename: string): string =
# we don't find any files in online mode:
result = ""
var dummyHasToc = false
var rst = rstParse(input, filen, line=LineRstInit, column=ColRstInit,
dummyHasToc, rstOptions, myFindFile, testMsgHandler)
result = renderRstToStr(rst)
except EParseError:
discard
suite "RST indentation":
test "nested bullet lists":
let input = dedent """
* - bullet1
- bullet2
* - bullet3
- bullet4
"""
let output = input.toAst
check(output == dedent"""
rnBulletList
rnBulletItem
rnBulletList
rnBulletItem
rnInner
rnLeaf 'bullet1'
rnBulletItem
rnInner
rnLeaf 'bullet2'
rnBulletItem
rnBulletList
rnBulletItem
rnInner
rnLeaf 'bullet3'
rnBulletItem
rnInner
rnLeaf 'bullet4'
""")
test "nested markup blocks":
let input = dedent"""
#) .. Hint:: .. Error:: none
#) .. Warning:: term0
Definition0
#) some
paragraph1
#) term1
Definition1
term2
Definition2
"""
check(input.toAst == dedent"""
rnEnumList labelFmt=1)
rnEnumItem
rnAdmonition adType=hint
[nil]
[nil]
rnAdmonition adType=error
[nil]
[nil]
rnLeaf 'none'
rnEnumItem
rnAdmonition adType=warning
[nil]
[nil]
rnDefList
rnDefItem
rnDefName
rnLeaf 'term0'
rnDefBody
rnInner
rnLeaf 'Definition0'
rnEnumItem
rnInner
rnLeaf 'some'
rnLeaf ' '
rnLeaf 'paragraph1'
rnEnumItem
rnDefList
rnDefItem
rnDefName
rnLeaf 'term1'
rnDefBody
rnInner
rnLeaf 'Definition1'
rnDefItem
rnDefName
rnLeaf 'term2'
rnDefBody
rnInner
rnLeaf 'Definition2'
""")
test "code-block parsing":
let input1 = dedent"""
.. code-block:: nim
:test: "nim c $1"
template additive(typ: typedesc) =
discard
"""
let input2 = dedent"""
.. code-block:: nim
:test: "nim c $1"
template additive(typ: typedesc) =
discard
"""
let input3 = dedent"""
.. code-block:: nim
:test: "nim c $1"
template additive(typ: typedesc) =
discard
"""
let inputWrong = dedent"""
.. code-block:: nim
:test: "nim c $1"
template additive(typ: typedesc) =
discard
"""
let ast = dedent"""
rnCodeBlock
rnDirArg
rnLeaf 'nim'
rnFieldList
rnField
rnFieldName
rnLeaf 'test'
rnFieldBody
rnInner
rnLeaf '"'
rnLeaf 'nim'
rnLeaf ' '
rnLeaf 'c'
rnLeaf ' '
rnLeaf '$'
rnLeaf '1'
rnLeaf '"'
rnField
rnFieldName
rnLeaf 'default-language'
rnFieldBody
rnLeaf 'Nim'
rnLiteralBlock
rnLeaf 'template additive(typ: typedesc) =
discard'
"""
check input1.toAst == ast
check input2.toAst == ast
check input3.toAst == ast
# "template..." should be parsed as a definition list attached to ":test:":
check inputWrong.toAst != ast
suite "RST include directive":
test "Include whole":
"other.rst".writeFile("**test1**")
let input = ".. include:: other.rst"
doAssert "<strong>test1</strong>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Include starting from":
"other.rst".writeFile("""
And this should **NOT** be visible in `docs.html`
OtherStart
*Visible*
""")
let input = """
.. include:: other.rst
:start-after: OtherStart
"""
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Include everything before":
"other.rst".writeFile("""
*Visible*
OtherEnd
And this should **NOT** be visible in `docs.html`
""")
let input = """
.. include:: other.rst
:end-before: OtherEnd
"""
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Include everything between":
"other.rst".writeFile("""
And this should **NOT** be visible in `docs.html`
OtherStart
*Visible*
OtherEnd
And this should **NOT** be visible in `docs.html`
""")
let input = """
.. include:: other.rst
:start-after: OtherStart
:end-before: OtherEnd
"""
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
test "Ignore premature ending string":
"other.rst".writeFile("""
OtherEnd
And this should **NOT** be visible in `docs.html`
OtherStart
*Visible*
OtherEnd
And this should **NOT** be visible in `docs.html`
""")
let input = """
.. include:: other.rst
:start-after: OtherStart
:end-before: OtherEnd
"""
doAssert "<em>Visible</em>" == rstTohtml(input, {}, defaultConfig())
removeFile("other.rst")
|