diff options
Diffstat (limited to 'scripts/prepare_html_rules.py')
| -rwxr-xr-x | scripts/prepare_html_rules.py | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/scripts/prepare_html_rules.py b/scripts/prepare_html_rules.py index aca29932..d2131b2a 100755 --- a/scripts/prepare_html_rules.py +++ b/scripts/prepare_html_rules.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 """Add ids to all rules based on their headline""" -# TODO: support nested lists import sys import re @@ -9,21 +8,6 @@ HEADER_RE = re.compile(r'<h\d') DATA_NUMBER_RE = re.compile(r'data\-number="([^"]*)"') -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" - - def extract_headline_nr(line: str) -> str: """Extract the headline's number from it's data-number attribute""" m = DATA_NUMBER_RE.search(line) @@ -36,19 +20,20 @@ def extract_headline_nr(line: str) -> str: def main(): """Add ids to all ordered lists in the input""" headline = "" - i = 0 + items = [] 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 + items.append(1) + if '<li' in line and items: + p = ".".join([str(i) for i in items]) + line = line.replace('<li', f'<li id="{headline}.{p}" class="rule"') + items[-1] += 1 if '</ol' in line: - i = 0 + items.pop(-1) print(line, end="") |
