-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_toc.py
76 lines (51 loc) · 1.21 KB
/
test_toc.py
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
import mistune
from mistune_contrib.toc import TocMixin
class TocRenderer(TocMixin, mistune.Renderer):
pass
text = '''# mistune
mistune is a markdown parser in pure python.
## renderer
renderer can change the result html.
## grammar
mistune is parsed by grammar
### inline grammar
inline things
### block grammar
block things
#### level 4
this level will not be parsed
# contrib
here is the contribution
### invalid
this would not show in toc.
## valid
this would be in toc.
'''
expected = '''
<ul id="table-of-content">
<li><a href="#toc-0">mistune</a>
<ul>
<li><a href="#toc-1">renderer</a></li>
<li><a href="#toc-2">grammar</a>
<ul>
<li><a href="#toc-3">inline grammar</a></li>
<li><a href="#toc-4">block grammar</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#toc-6">contrib</a>
<ul>
<li><a href="#toc-8">valid</a></li>
</ul>
</li>
</ul>
'''
def test_toc():
toc = TocRenderer()
md = mistune.Markdown(renderer=toc)
toc.reset_toc()
assert 'toc-0' in md.parse(text)
rv = toc.render_toc(level=3)
rv = rv.replace('\n', '').replace(' ', '')
assert rv == expected.replace('\n', '').replace(' ', '')