diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-06-16 21:09:31 -0500 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-07-03 22:01:22 -0400 |
| commit | 56dc7801d6e91e3df57f305a42c3d38ce0ae6e70 (patch) | |
| tree | a28c32a5eb5a85907b8f83f49938cd5dd64af52d /scripts | |
| parent | 2432c7cd45f8955e2dcd46a3c063052d09a085ab (diff) | |
| download | muhqs-game-56dc7801d6e91e3df57f305a42c3d38ce0ae6e70.tar.gz muhqs-game-56dc7801d6e91e3df57f305a42c3d38ce0ae6e70.zip | |
fix linkable rules and support nesting
Diffstat (limited to 'scripts')
| -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="") |
