aboutsummaryrefslogtreecommitdiff
path: root/scripts/prepare_html_rules.py
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-05-11 21:38:22 -0600
committerFlorian Fischer <florian.fischer@muhq.space>2025-05-18 20:35:04 -0600
commit4d2093c8f449d85fed4df9761ce808e030534a07 (patch)
tree50f6e09d690f8bc352b5a09b3ad7f7db58ef903a /scripts/prepare_html_rules.py
parent37acd982268df214ae13646d9174ef2490ac16f4 (diff)
downloadmuhqs-game-4d2093c8f449d85fed4df9761ce808e030534a07.tar.gz
muhqs-game-4d2093c8f449d85fed4df9761ce808e030534a07.zip
make rules in html linkable
Diffstat (limited to 'scripts/prepare_html_rules.py')
-rwxr-xr-xscripts/prepare_html_rules.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/scripts/prepare_html_rules.py b/scripts/prepare_html_rules.py
new file mode 100755
index 00000000..4d533bec
--- /dev/null
+++ b/scripts/prepare_html_rules.py
@@ -0,0 +1,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="")