blob: 4d533bec3e1e9b35e0b1abec006e104d18e086b1 (
plain)
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
|
#!/usr/bin/env python3
# TODO: support nested lists
import sys
import re
def add_ids(ol):
"""add ids to all li"""
id = ""
for s in ol.previous_siblings:
if not s.name:
continue
if s.name.startswith("h"):
id = s.attrs["data-number"]
break
for i, li in enumerate(ol.find_all("li")):
li["id"] = f"{id}-{i+1}"
li["class"] = "rule"
header_re = re.compile(r'<h\d')
data_number_re = re.compile(r'data\-number="([^"]*)"')
def extract_headline_nr(line: str) -> str:
m = data_number_re.search(line)
if m:
return m.group(1)
else:
return ""
headline = ""
i = 0
for line in sys.stdin.readlines():
if header_re.match(line):
headline = extract_headline_nr(line)
h_idx = line.find('<h') + 3
line = line[:h_idx] + ' class="rule"' + line[h_idx:]
if '<ol' in line and headline:
i = 1
if '<li' in line and i:
line = line.replace('<li', f'<li id="{headline}-{i}" class="rule"')
i = i + 1
if '</ol' in line:
i = 0
print(line, end="")
|