diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2021-12-08 22:05:23 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2021-12-08 22:05:23 +0100 |
| commit | b119dae1631291b0052f1a7fe50c4c4f5f91be02 (patch) | |
| tree | 930289000b29260d8899bf18c852d1980c2b90ac /scripts | |
| parent | 7cc955249a26f68345d9a6cd355e39bffa365a59 (diff) | |
| download | muhqs-game-b119dae1631291b0052f1a7fe50c4c4f5f91be02.tar.gz muhqs-game-b119dae1631291b0052f1a7fe50c4c4f5f91be02.zip | |
html: add script generating hoverable card links with image previews
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/__init__.py | 0 | ||||
| -rwxr-xr-x | scripts/data.py | 15 | ||||
| -rwxr-xr-x | scripts/generate_card_hover_links.py | 78 |
3 files changed, 93 insertions, 0 deletions
diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/scripts/__init__.py diff --git a/scripts/data.py b/scripts/data.py new file mode 100755 index 00000000..e19a805e --- /dev/null +++ b/scripts/data.py @@ -0,0 +1,15 @@ +from pathlib import Path + +GAME_ROOT = Path(__file__).parent.parent +CARDS_DATA_DIR = GAME_ROOT / 'data' / 'cards' + +CARDS_TO_SETS = {} +SETS_TO_CARDS = {} + +# build up the card lookup dictionaries +for set_dir in CARDS_DATA_DIR.iterdir(): + set_name = set_dir.name + SETS_TO_CARDS[set_name] = [] + for card in set_dir.iterdir(): + SETS_TO_CARDS[set_name].append(card.stem) + CARDS_TO_SETS[card.stem] = set_name diff --git a/scripts/generate_card_hover_links.py b/scripts/generate_card_hover_links.py new file mode 100755 index 00000000..e86177ac --- /dev/null +++ b/scripts/generate_card_hover_links.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +import argparse +import pathlib +from pathlib import Path +import re +from typing import MutableMapping, Tuple +import sys + +from data import CARDS_TO_SETS + +PATTERN = re.compile('\{\{(?P<card>.*?)\}\}') + +CSS_STYLE = 'position: absolute; bottom: 0; left: 0; transform: translate(50%, 50%);' + +HOVERABLE_LINK = \ +f'''<a href="{{link_target}}" + onmouseover="var o=document.getElementById('placeholder-{{pid}}');o.src='{{img_target}}';o.width=355;o.height=532;" + onmouseout="var o=document.getElementById('placeholder-{{pid}}');o.src='hover-placeholder.png';o.width=1;o.height=1;" +> +{{link_text}} +<img src="hover-placeholder.png" id="placeholder-{{pid}}" width=1 height=1 style="{CSS_STYLE}" /> +</a>''' + +PLACEHOLDER_ID = 0 + + +def replace_with_link(match, language) -> str: + card = match.group('card') + link_text = f'<em>{card}</em>' + + cannonical_name = card.lower().replace(' ', '_') + card_listing_target = cannonical_name.replace('_', '-').replace('!', '') + link_target = f'{language}/cards_listing.html#{card_listing_target}' + card_img = f'latex-build/{language}/{CARDS_TO_SETS[cannonical_name]}/{cannonical_name}.png' + global PLACEHOLDER_ID + placeholder_id = PLACEHOLDER_ID + PLACEHOLDER_ID += 1 + return HOVERABLE_LINK.format(link_target=link_target, + pid=placeholder_id, + img_target=card_img, + link_text=link_text) + + +def replace_all_links(string: str, language='en') -> str: + """Replace a document containing all included cards""" + sub_func = lambda match: replace_with_link(match, language=language) + + return re.sub(PATTERN, sub_func, string) + + +def main(): + """Generate one standalone tikz picture for each card in the input data""" + + parser = argparse.ArgumentParser( + description='generate decks from deck descriptions') + parser.add_argument('markdown_file', + help='file in which links should be replaced', + nargs='?') + parser.add_argument('--language', + choices=['en', 'de'], + default='en', + help='the language of the cards to output') + + args = parser.parse_args() + + if args.markdown_file: + markdown_path = Path(args.markdown_file) + + with open(markdown_path, 'r', encoding="utf8") as markdown_file: + content = markdown_file.read() + else: + content = input() + + print(replace_all_links(content, language=args.language)) + + +if __name__ == '__main__': + main() |
