aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-05-19 18:53:39 -0600
committerFlorian Fischer <florian.fischer@muhq.space>2025-05-19 18:53:39 -0600
commit41c8eb87e003f214a0143f27f1b0a10dbabf89d5 (patch)
tree9bb13a368a21defc755cf8e9499619997db43907 /scripts
parentd1d8e75ea8177b742364054218e12ec3bc3aeb0d (diff)
downloadmuhqs-game-41c8eb87e003f214a0143f27f1b0a10dbabf89d5.tar.gz
muhqs-game-41c8eb87e003f214a0143f27f1b0a10dbabf89d5.zip
improve hover link creation
* read the complete file from stdin * support links from outside the html root by specifying a path prefix to the card listings * small code cleanups
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/generate_card_hover_links.py41
1 files changed, 30 insertions, 11 deletions
diff --git a/scripts/generate_card_hover_links.py b/scripts/generate_card_hover_links.py
index 51d18fbd..8f945c91 100755
--- a/scripts/generate_card_hover_links.py
+++ b/scripts/generate_card_hover_links.py
@@ -5,9 +5,12 @@ import argparse
from pathlib import Path
import re
from typing import Callable
+import sys
from data import name2set, name2file, name2cardlisting
+ROOT_URL = '/muhqs-game/html/build'
+
PATTERN = re.compile(r'\{\{(?P<card>.*?)\}\}')
CSS_STYLE = 'zindex: 100; position: absolute;'
@@ -22,10 +25,10 @@ var w = window;
var up = b.bottom - w.innerHeight;
var left = b.right - w.innerWidth;
if (up > 0) {{
- o.style.top = (b.top - up) + w.pageYOffset + 'px';
+ o.style.top = (b.top - up) + w.pageYOffset + 'px';
}}
if (left > 0) {{
- o.style.left = (b.left - left) + + w.pageXOffset + 'px';
+ o.style.left = (b.left - left) + w.pageXOffset + 'px';
}}
"""
@@ -77,19 +80,27 @@ def gen_hoverable_link(card: str, gen_link_text: GenLinkTextCallback,
link_text=link_text)
-def gen_link_to_cardlisting(card: str, language: str) -> str:
+def gen_link_to_cardlisting(card: str, language: str, path_prefix='') -> str:
"""Return the link to a card in the card listing"""
card_listing_target = name2cardlisting(card)
- return f'{language}/cards_listing.html#{card_listing_target}'
+ return f'{path_prefix}{language}/cards_listing.html#{card_listing_target}'
-def replace_all_links(string: str, images_path: str, language='en') -> str:
+def replace_all_links(string: str,
+ images_path: str,
+ language='en',
+ link_path_prefix='') -> str:
"""Replace a document containing all included cards"""
- gen_link_text = lambda card: f'<em>{card}</em>'
- sub_func = lambda match: gen_hoverable_link(match.group(
- 'card'), gen_link_text, gen_link_to_cardlisting, images_path, language)
+ def gen_link_text(card):
+ return f'<em>{card}</em>'
+
+ def sub_func(match):
+ return gen_hoverable_link(
+ match.group('card'),
+ gen_link_text, lambda card, lang: gen_link_to_cardlisting(
+ card, lang, link_path_prefix), images_path, language)
return re.sub(PATTERN, sub_func, string)
@@ -107,9 +118,13 @@ def main():
default='en',
help='the language of the cards to output')
parser.add_argument('--images',
- default='/muhqs-game/html/build/latex-build',
+ default=f'{ROOT_URL}/latex-build',
type=str,
help='the path to the rendered cards')
+ parser.add_argument('--cardlisting-path-prefix',
+ default='',
+ type=str,
+ help='a prefix to the path to the cardlisting')
args = parser.parse_args()
@@ -119,9 +134,13 @@ def main():
with open(markdown_path, 'r', encoding="utf8") as markdown_file:
content = markdown_file.read()
else:
- content = input()
+ content = sys.stdin.read()
- print(replace_all_links(content, args.images, language=args.language))
+ print(
+ replace_all_links(content,
+ args.images,
+ language=args.language,
+ link_path_prefix=args.cardlisting_path_prefix))
if __name__ == '__main__':