aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-06-16 21:09:31 -0500
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:57:20 +0200
commit96a4492e6706b9802aec9744db247395f1bf0cbd (patch)
tree12a656e600651f1d9ca55fa8d74b43244ca49fc8 /scripts
parent06f7814237308d177eae1b3faba294197af9ae30 (diff)
downloadmuhqs-game-96a4492e6706b9802aec9744db247395f1bf0cbd.tar.gz
muhqs-game-96a4492e6706b9802aec9744db247395f1bf0cbd.zip
fix linkable rules and support nesting
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/prepare_html_rules.py29
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="")