-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_list.py
53 lines (41 loc) · 1.01 KB
/
parse_list.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
#!/usr/bin/env python3
import re
import sys
from pathlib import Path
import doctree
txt = [i.rstrip() for i in Path(sys.argv[1]).read_text().splitlines()]
rec = re.compile(r'^(?P<tab>\t*)(?P<marker>[\*#]) (?P<line>.*)$')
prev_indent = -1
doc = doctree.Leaf('root')
leaf = doc
for n, line in enumerate(txt) :
res = rec.search(line)
if res is None :
leaf.sub[-1].add_alinea(line)
continue
indent = len(res.group('tab'))
if indent == prev_indent :
print(n, "1", res.groupdict())
leaf.grow('li').add_alinea(res.group('line'))
elif indent == prev_indent + 1 :
print(n, "2", res.groupdict())
if res.group('marker') == '*' :
leaf = leaf.grow('ul')
elif res.group('marker') == '#' :
leaf = leaf.grow('ol')
leaf.grow('li').add_alinea(res.group('line'))
elif indent == prev_indent - 1 :
print(n, "3", res.groupdict())
leaf = leaf.parent
leaf.grow('li').add_alinea(res.group('line'))
prev_indent = indent
print(doc)
reference_lst = [
'alpha',
'beta',
[
'charlie',
'delta'
],
'epsilon'
]