aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-05-19 18:56:29 -0600
committerFlorian Fischer <florian.fischer@muhq.space>2025-05-19 20:35:50 -0600
commit62cb3617c6e7a5a9935a041da2848a7eb2c7acf5 (patch)
treef9088a66a7f79fc7a36719e850a6d63e3674911e /scripts
parent41c8eb87e003f214a0143f27f1b0a10dbabf89d5 (diff)
downloadmuhqs-game-62cb3617c6e7a5a9935a041da2848a7eb2c7acf5.tar.gz
muhqs-game-62cb3617c6e7a5a9935a041da2848a7eb2c7acf5.zip
improve code style
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/prepare_html_rules.py60
1 files changed, 36 insertions, 24 deletions
diff --git a/scripts/prepare_html_rules.py b/scripts/prepare_html_rules.py
index 4d533bec..aca29932 100755
--- a/scripts/prepare_html_rules.py
+++ b/scripts/prepare_html_rules.py
@@ -1,44 +1,56 @@
#!/usr/bin/env python3
+"""Add ids to all rules based on their headline"""
# TODO: support nested lists
import sys
import re
+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 = ""
+ _id = ""
for s in ol.previous_siblings:
if not s.name:
continue
if s.name.startswith("h"):
- id = s.attrs["data-number"]
+ _id = s.attrs["data-number"]
break
for i, li in enumerate(ol.find_all("li")):
- li["id"] = f"{id}-{i+1}"
+ 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)
+ """Extract the headline's number from it's data-number attribute"""
+ 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="")
+
+ return ""
+
+
+def main():
+ """Add ids to all ordered lists in the input"""
+ 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="")
+
+
+if __name__ == "__main__":
+ main()