1
|
|
|
# This Python file uses the following encoding: utf-8 |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from pandocfilters import Para, Str, Space, Span, Strong, RawInline, Emph, Header |
5
|
|
|
|
6
|
|
|
import json |
7
|
|
|
|
8
|
|
|
import pandoc_numbering |
9
|
|
|
|
10
|
|
|
from helper import init, createMetaList, createMetaMap, createMetaInlines, createListStr, createMetaString, createMetaBool |
11
|
|
|
|
12
|
|
|
def getMeta1(): |
13
|
|
|
return { |
14
|
|
|
'pandoc-numbering': createMetaList([ |
15
|
|
|
createMetaMap({ |
16
|
|
|
'category': createMetaInlines('exercise'), |
17
|
|
|
'sectioning': createMetaInlines('-.+.') |
18
|
|
|
}) |
19
|
|
|
]) |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
def getMeta2(): |
23
|
|
|
return { |
24
|
|
|
'pandoc-numbering': createMetaList([ |
25
|
|
|
createMetaMap({ |
26
|
|
|
'category': createMetaInlines('exercise'), |
27
|
|
|
'first': createMetaString('2'), |
28
|
|
|
'last': createMetaString('2'), |
29
|
|
|
}) |
30
|
|
|
]) |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
def getMeta3(): |
34
|
|
|
return { |
35
|
|
|
'pandoc-numbering': createMetaList([ |
36
|
|
|
createMetaMap({ |
37
|
|
|
'category': createMetaInlines('exercise'), |
38
|
|
|
'first': createMetaString('a'), |
39
|
|
|
'last': createMetaString('b'), |
40
|
|
|
}) |
41
|
|
|
]) |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
def getMeta4(): |
45
|
|
|
return { |
46
|
|
|
'pandoc-numbering': createMetaList([ |
47
|
|
|
createMetaMap({ |
48
|
|
|
'category': createMetaInlines('exercise'), |
49
|
|
|
'classes': createMetaList([createMetaInlines('my-class')]) |
50
|
|
|
}) |
51
|
|
|
]) |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
def getMeta5(): |
55
|
|
|
return { |
56
|
|
|
'pandoc-numbering': createMetaList([ |
57
|
|
|
createMetaMap({ |
58
|
|
|
'category': createMetaInlines('exercise'), |
59
|
|
|
'format': createMetaBool(False) |
60
|
|
|
}) |
61
|
|
|
]) |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
def test_numbering(): |
65
|
|
|
init() |
66
|
|
|
|
67
|
|
|
src = Para(createListStr('Exercise #')) |
68
|
|
|
dest = Para([ |
69
|
|
|
Span( |
70
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
71
|
|
|
[Strong(createListStr('Exercise 1'))] |
72
|
|
|
) |
73
|
|
|
]) |
74
|
|
|
|
75
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
76
|
|
|
|
77
|
|
|
def test_numbering_prefix_single(): |
78
|
|
|
init() |
79
|
|
|
|
80
|
|
|
src = Para(createListStr('Exercise #ex:')) |
81
|
|
|
dest = Para([ |
82
|
|
|
Span( |
83
|
|
|
[u'ex:1', ['pandoc-numbering-text', 'ex'], []], |
84
|
|
|
[Strong(createListStr('Exercise 1'))] |
85
|
|
|
) |
86
|
|
|
]) |
87
|
|
|
|
88
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
89
|
|
|
|
90
|
|
|
src = Para(createListStr('Exercise #')) |
91
|
|
|
dest = Para([ |
92
|
|
|
Span( |
93
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
94
|
|
|
[Strong(createListStr('Exercise 1'))] |
95
|
|
|
) |
96
|
|
|
]) |
97
|
|
|
|
98
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
99
|
|
|
|
100
|
|
|
def test_numbering_latex(): |
101
|
|
|
init() |
102
|
|
|
|
103
|
|
|
src = Para(createListStr('Exercise #')) |
104
|
|
|
dest = Para([ |
105
|
|
|
RawInline(u'tex', u'\\phantomsection\\addcontentsline{exercise}{exercise}{\\protect\\numberline {1}{\\ignorespaces Exercise}}'), |
106
|
|
|
Span( |
107
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
108
|
|
|
[Strong(createListStr('Exercise 1'))] |
109
|
|
|
) |
110
|
|
|
]) |
111
|
|
|
|
112
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], 'latex', {}) == dest |
113
|
|
|
|
114
|
|
|
init() |
115
|
|
|
|
116
|
|
|
src = Para(createListStr('Exercise (The title) #')) |
117
|
|
|
dest = Para([ |
118
|
|
|
RawInline(u'tex', u'\\phantomsection\\addcontentsline{exercise}{exercise}{\\protect\\numberline {1}{\\ignorespaces The title}}'), |
119
|
|
|
Span( |
120
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
121
|
|
View Code Duplication |
[ |
|
|
|
|
122
|
|
|
Strong(createListStr('Exercise 1')), |
123
|
|
|
Space(), |
124
|
|
|
Emph(createListStr('(') + createListStr('The title') + createListStr(')')) |
125
|
|
|
] |
126
|
|
|
) |
127
|
|
|
]) |
128
|
|
|
|
129
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], 'latex', {}) == dest |
130
|
|
|
|
131
|
|
|
def test_numbering_double(): |
132
|
|
|
init() |
133
|
|
|
|
134
|
|
|
src = Para(createListStr('Exercise #')) |
135
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
136
|
|
|
|
137
|
|
|
src = Para(createListStr('Exercise #')) |
138
|
|
|
dest = Para([ |
139
|
|
|
Span( |
140
|
|
|
[u'exercise:2', ['pandoc-numbering-text', 'exercise'], []], |
141
|
|
|
[Strong(createListStr('Exercise 2'))] |
142
|
|
|
) |
143
|
|
|
]) |
144
|
|
|
|
145
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
146
|
|
|
|
147
|
|
View Code Duplication |
def test_numbering_title(): |
|
|
|
|
148
|
|
|
init() |
149
|
|
|
|
150
|
|
|
src = Para(createListStr('Exercise (The title) #')) |
151
|
|
|
dest = Para([ |
152
|
|
|
Span( |
153
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
154
|
|
|
[ |
155
|
|
|
Strong(createListStr('Exercise 1')), |
156
|
|
|
Space(), |
157
|
|
|
Emph(createListStr('(') + createListStr('The title') + createListStr(')')) |
158
|
|
|
] |
159
|
|
|
) |
160
|
|
|
]) |
161
|
|
|
|
162
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
163
|
|
|
|
164
|
|
|
def test_numbering_level(): |
165
|
|
|
init() |
166
|
|
|
|
167
|
|
|
src = Para(createListStr('Exercise +.+.#')) |
168
|
|
|
dest = Para([ |
169
|
|
|
Span( |
170
|
|
|
[u'exercise:0.0.1', ['pandoc-numbering-text', 'exercise'], []], |
171
|
|
|
[Strong(createListStr('Exercise 0.0.1'))] |
172
|
|
|
) |
173
|
|
|
]) |
174
|
|
|
|
175
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
176
|
|
|
|
177
|
|
|
src = Header(1, [u'first-chapter', [], []], createListStr('First chapter')) |
178
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
179
|
|
|
|
180
|
|
|
src = Header(2, [u'first-section', [], []], createListStr('First section')) |
181
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
182
|
|
|
|
183
|
|
|
src = Para(createListStr('Exercise +.+.#')) |
184
|
|
|
dest = Para([ |
185
|
|
|
Span( |
186
|
|
|
[u'exercise:1.1.1', ['pandoc-numbering-text', 'exercise'], []], |
187
|
|
|
[Strong(createListStr('Exercise 1.1.1'))] |
188
|
|
|
) |
189
|
|
|
]) |
190
|
|
|
|
191
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
192
|
|
|
|
193
|
|
|
src = Para(createListStr('Exercise +.+.#')) |
194
|
|
|
dest = Para([ |
195
|
|
|
Span( |
196
|
|
|
[u'exercise:1.1.2', ['pandoc-numbering-text', 'exercise'], []], |
197
|
|
|
[Strong(createListStr('Exercise 1.1.2'))] |
198
|
|
|
) |
199
|
|
|
]) |
200
|
|
|
|
201
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
202
|
|
|
|
203
|
|
|
src = Header(2, [u'second-section', [], []], createListStr('Second section')) |
204
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
205
|
|
|
|
206
|
|
View Code Duplication |
src = Para(createListStr('Exercise +.+.#')) |
|
|
|
|
207
|
|
|
dest = Para([ |
208
|
|
|
Span( |
209
|
|
|
[u'exercise:1.2.1', ['pandoc-numbering-text', 'exercise'], []], |
210
|
|
|
[Strong(createListStr('Exercise 1.2.1'))] |
211
|
|
|
) |
212
|
|
|
]) |
213
|
|
|
|
214
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
215
|
|
|
|
216
|
|
|
def test_numbering_unnumbered(): |
217
|
|
|
init() |
218
|
|
|
|
219
|
|
|
src = Header(1, [u'unnumbered-chapter', [u'unnumbered'], []], createListStr('Unnumbered chapter')) |
220
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
221
|
|
|
|
222
|
|
|
src = Para(createListStr('Exercise +.#')) |
223
|
|
|
dest = Para([ |
224
|
|
|
Span( |
225
|
|
|
[u'exercise:0.1', ['pandoc-numbering-text', 'exercise'], []], |
226
|
|
|
[Strong(createListStr('Exercise 0.1'))] |
227
|
|
|
) |
228
|
|
|
]) |
229
|
|
|
|
230
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
231
|
|
|
|
232
|
|
|
def test_numbering_hidden(): |
233
|
|
|
init() |
234
|
|
|
|
235
|
|
|
src = Header(1, [u'first-chapter', [], []], createListStr('First chapter')) |
236
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
237
|
|
|
|
238
|
|
|
src = Para(createListStr('Exercise -.#exercise:one')) |
239
|
|
|
dest = Para([ |
240
|
|
|
Span( |
241
|
|
|
[u'exercise:one', ['pandoc-numbering-text', 'exercise'], []], |
242
|
|
|
[ |
243
|
|
|
Strong(createListStr('Exercise 1')) |
244
|
|
|
] |
245
|
|
|
) |
246
|
|
|
]) |
247
|
|
|
|
248
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
249
|
|
|
|
250
|
|
|
src = Para(createListStr('Exercise -.#')) |
251
|
|
|
dest = Para([ |
252
|
|
|
Span( |
253
|
|
|
[u'exercise:1.2', ['pandoc-numbering-text', 'exercise'], []], |
254
|
|
|
[Strong(createListStr('Exercise 2'))] |
255
|
|
|
) |
256
|
|
|
]) |
257
|
|
|
|
258
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
259
|
|
|
|
260
|
|
|
src = Header(1, [u'second-chapter', [], []], createListStr('Second chapter')) |
261
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
262
|
|
|
|
263
|
|
|
src = Para(createListStr('Exercise -.#')) |
264
|
|
|
dest = Para([ |
265
|
|
|
Span( |
266
|
|
|
[u'exercise:2.1', ['pandoc-numbering-text', 'exercise'], []], |
267
|
|
|
[Strong(createListStr('Exercise 1'))] |
268
|
|
|
) |
269
|
|
|
]) |
270
|
|
|
|
271
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
272
|
|
|
|
273
|
|
|
src = Para(createListStr('Exercise +.#')) |
274
|
|
|
dest = Para([ |
275
|
|
|
Span( |
276
|
|
|
[u'exercise:2.2', ['pandoc-numbering-text', 'exercise'], []], |
277
|
|
|
[Strong(createListStr('Exercise 2.2'))] |
278
|
|
|
) |
279
|
|
|
]) |
280
|
|
|
|
281
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
282
|
|
|
|
283
|
|
|
src = Para([Str(u'Exercise'), Space(), Str(u'#')]) |
284
|
|
|
dest = Para([ |
285
|
|
|
Span( |
286
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
287
|
|
|
[Strong(createListStr('Exercise 1'))] |
288
|
|
|
) |
289
|
|
|
]) |
290
|
|
|
|
291
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', {}) == dest |
292
|
|
|
|
293
|
|
|
def test_numbering_sharp_sharp(): |
294
|
|
|
init() |
295
|
|
|
|
296
|
|
|
src = Para(createListStr('Exercise ##')) |
297
|
|
|
dest = Para(createListStr('Exercise #')) |
298
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', {}) |
299
|
|
|
|
300
|
|
|
assert src == dest |
301
|
|
|
|
302
|
|
|
def sectioning(meta): |
303
|
|
|
src = Header(1, [u'first-chapter', [], []], createListStr('First chapter')) |
304
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', meta) |
305
|
|
|
|
306
|
|
|
src = Header(1, [u'second-chapter', [], []], createListStr('Second chapter')) |
307
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', meta) |
308
|
|
|
|
309
|
|
|
src = Header(2, [u'first-section', [], []], createListStr('First section')) |
310
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', meta) |
311
|
|
|
|
312
|
|
|
src = Header(2, [u'second-section', [], []], createListStr('Second section')) |
313
|
|
|
pandoc_numbering.numbering(src['t'], src['c'], '', meta) |
314
|
|
|
|
315
|
|
View Code Duplication |
def test_numbering_sectioning_string(): |
|
|
|
|
316
|
|
|
init() |
317
|
|
|
|
318
|
|
|
meta = getMeta1() |
319
|
|
|
|
320
|
|
|
sectioning(meta) |
321
|
|
|
|
322
|
|
|
src = Para(createListStr('Exercise #')) |
323
|
|
|
dest = Para([ |
324
|
|
|
Span( |
325
|
|
|
[u'exercise:2.2.1', ['pandoc-numbering-text', 'exercise'], []], |
326
|
|
|
[Strong(createListStr('Exercise 2.1'))] |
327
|
|
|
) |
328
|
|
|
]) |
329
|
|
|
|
330
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', meta) == dest |
331
|
|
|
|
332
|
|
View Code Duplication |
def test_numbering_sectioning_map(): |
|
|
|
|
333
|
|
|
init() |
334
|
|
|
|
335
|
|
|
meta = getMeta2() |
336
|
|
|
|
337
|
|
|
sectioning(meta) |
338
|
|
|
|
339
|
|
|
src = Para([Str(u'Exercise'), Space(), Str(u'#')]) |
340
|
|
|
dest = Para([ |
341
|
|
|
Span( |
342
|
|
|
[u'exercise:2.2.1', ['pandoc-numbering-text', 'exercise'], []], |
343
|
|
|
[Strong(createListStr('Exercise 2.1'))] |
344
|
|
|
) |
345
|
|
|
]) |
346
|
|
|
|
347
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', meta) == dest |
348
|
|
|
|
349
|
|
View Code Duplication |
def test_numbering_sectioning_map_error(): |
|
|
|
|
350
|
|
|
init() |
351
|
|
|
|
352
|
|
|
meta = getMeta3() |
353
|
|
|
|
354
|
|
|
sectioning(meta) |
355
|
|
|
|
356
|
|
|
src = Para(createListStr('Exercise #')) |
357
|
|
|
dest = Para([ |
358
|
|
|
Span( |
359
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercise'], []], |
360
|
|
|
[Strong(createListStr('Exercise 1'))] |
361
|
|
|
) |
362
|
|
|
]) |
363
|
|
|
|
364
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', meta) == dest |
365
|
|
|
|
366
|
|
View Code Duplication |
def test_classes(): |
|
|
|
|
367
|
|
|
init() |
368
|
|
|
|
369
|
|
|
meta = getMeta4() |
370
|
|
|
|
371
|
|
|
src = Para(createListStr('Exercise #')) |
372
|
|
|
dest = Para([ |
373
|
|
|
Span( |
374
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'my-class'], []], |
375
|
|
|
[Strong(createListStr('Exercise 1'))] |
376
|
|
|
) |
377
|
|
|
]) |
378
|
|
|
|
379
|
|
|
assert pandoc_numbering.numbering(src['t'], src['c'], '', meta) == dest |
380
|
|
|
|
381
|
|
|
def test_format(): |
382
|
|
|
init() |
383
|
|
|
|
384
|
|
|
meta = getMeta5() |
385
|
|
|
|
386
|
|
|
src = Para(createListStr('Exercise #')) |
387
|
|
|
dest = json.loads(json.dumps(Para([ |
388
|
|
|
Span( |
389
|
|
|
[u'exercise:1', ['pandoc-numbering-text', 'exercice'], []], |
390
|
|
|
[ |
391
|
|
|
Span(['', ['description'], []], createListStr('Exercise')), |
392
|
|
|
Span(['', ['number'], []], createListStr('1')), |
393
|
|
|
Span(['', ['title'], []], []) |
394
|
|
|
] |
395
|
|
|
) |
396
|
|
|
]))) |
397
|
|
|
|
398
|
|
|
json.loads(json.dumps(pandoc_numbering.numbering(src['t'], src['c'], '', meta))) == dest |
399
|
|
|
|
400
|
|
|
|