From 791a83da34ff35f7dc159e7f8d5f060f02c6bc5a Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 4 Jan 2022 14:37:27 +0100 Subject: improve scripts * move more generic code handling data to data.py * fix markdown generation in generate_deck.py * make gen_hoverable_link more flexible by introducing callbacks to generate the relevant link components. * fix the link targets for hoverable links in the generated card markdown --- scripts/data.py | 71 ++++++++++++++++++++++++++++++++---- scripts/generate_card.py | 11 ++++-- scripts/generate_card_hover_links.py | 25 ++++++++----- scripts/generate_deck.py | 8 ++-- 4 files changed, 90 insertions(+), 25 deletions(-) (limited to 'scripts') diff --git a/scripts/data.py b/scripts/data.py index b72fc094..fadf9613 100755 --- a/scripts/data.py +++ b/scripts/data.py @@ -7,10 +7,67 @@ 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 + +def populate_lookup_dicts(): + """Populate the global 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 + + +def name2set(card: str) -> str: + """Return the set containing the card + + :param card: the english card name + :returns: the set containing the card + """ + + if not CARDS_TO_SETS: + populate_lookup_dicts() + + return CARDS_TO_SETS[card] + + +def set2names(set_name: str) -> list[str]: + """Return a list of all card in a set + + :param set_name: the set name + :returns: a list of all cards in the set + """ + + if not SETS_TO_CARDS: + populate_lookup_dicts() + + return SETS_TO_CARDS[set_name] + + +def name2file(card: str) -> str: + """Return the file name of a card + + File names are lower case card names with '_' instead of whitespace. + + :param card: the english name of a card + :returns: the file name of the card + """ + + return card.lower().replace(' ', '_') + + +def name2cardlisting(card: str) -> str: + """Return the card listing target + + The returned target can be used to directly navigate the + card listing to the specific card. + + Card listing targets are all lower case and use '-' instead of + whitespace. + Ending '!' are stripped from the card names. + + :param card: the english name of a card + :returns: the card listing target + """ + + return card.lower().replace(' ', '-').replace('!', '') diff --git a/scripts/generate_card.py b/scripts/generate_card.py index 973377a1..6500a480 100755 --- a/scripts/generate_card.py +++ b/scripts/generate_card.py @@ -1,4 +1,6 @@ #!/usr/bin/env python3 +"""Generate latex or markdown from yaml card definitions""" + import argparse import pathlib from pathlib import Path @@ -124,11 +126,11 @@ def get_latex_field(card: MutableMapping, # latex_* values are just concatenated. # NOTE: it is allowed for a latex_ field to be sparse. - # Missing entries in a latex_ field list are populated from the corrsponding + # Missing entries in a latex_ field list are populated from the corresponding # entry of the normal field assert len(latex_value) > 1 - for i in range(0, len(latex_value)): - if latex_value[i] is None: + for i, l_val in enumerate(latex_value): + if l_val is None: latex_value[i] = f'{fmt_prefix}{normal_value[i]}// ' return ''.join(latex_value), True @@ -149,7 +151,8 @@ def generate_markdown(card: MutableMapping, language='en', indentation=3): gen_png_link_text = lambda _: 'png' gen_png_link_target = lambda c, l: f'{built_card_path}.png' png_hover_link = generate_card_hover_links.gen_hoverable_link( - str(file_name), gen_png_link_text, gen_png_link_target, language) + str(file_name), gen_png_link_text, gen_png_link_target, + '../latex-build', language) print( f'\n[pdf]({built_card_path}.pdf) {png_hover_link} [yml]({yml_card_path})\n' diff --git a/scripts/generate_card_hover_links.py b/scripts/generate_card_hover_links.py index a835e172..9623037b 100755 --- a/scripts/generate_card_hover_links.py +++ b/scripts/generate_card_hover_links.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 +"""Generate html/js code for hoverable links showing a rendered image""" + import argparse from pathlib import Path import re from typing import Callable -from data import CARDS_TO_SETS +from data import name2set, name2file, name2cardlisting PATTERN = re.compile(r'\{\{(?P.*?)\}\}') @@ -42,16 +44,19 @@ f''' str: +def gen_hoverable_link(card: str, gen_link_text: GenLinkTextCallback, + gen_link_target: GenLinkTargetCallback, + images_path: str, language: str) -> str: """Return the hoverable link generated for the match :param card: the card name :param gen_link_text: a Callable returning the link text as string :param gen_link_target: a Callable returning the link target as string + :param images_path: path to where the rendered images are stored :param language: the language :returns: the code of the hoverable link """ @@ -59,10 +64,10 @@ def gen_hoverable_link(card: str, gen_link_text: Callable[[str], str], link_target = gen_link_target(card, language) - cannonical_name = card.lower().replace(' ', '_') - card_img = f'latex-build/{language}/{CARDS_TO_SETS[cannonical_name]}/{cannonical_name}.png' + cannonical_name = name2file(card) + card_img = f'{images_path}/{language}/{name2set(cannonical_name)}/{cannonical_name}.png' - global PLACEHOLDER_ID + global PLACEHOLDER_ID # pylint: disable=W0603 placeholder_id = PLACEHOLDER_ID PLACEHOLDER_ID += 1 @@ -75,8 +80,7 @@ def gen_hoverable_link(card: str, gen_link_text: Callable[[str], str], def gen_link_to_cardlisting(card: str, language: str) -> str: """Return the link to a card in the card listing""" - cannonical_name = card.lower().replace(' ', '_') - card_listing_target = cannonical_name.replace('_', '-').replace('!', '') + card_listing_target = name2cardlisting(card) return f'{language}/cards_listing.html#{card_listing_target}' @@ -84,7 +88,8 @@ def replace_all_links(string: str, language='en') -> str: """Replace a document containing all included cards""" gen_link_text = lambda card: f'{card}' sub_func = lambda match: gen_hoverable_link(match.group( - 'card'), gen_link_text, gen_link_to_cardlisting, language) + 'card'), gen_link_text, gen_link_to_cardlisting, 'latex-build', + language) return re.sub(PATTERN, sub_func, string) diff --git a/scripts/generate_deck.py b/scripts/generate_deck.py index 7003e750..8ccd12d0 100755 --- a/scripts/generate_deck.py +++ b/scripts/generate_deck.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import argparse from pathlib import Path -from data import CARDS_TO_SETS +from data import name2set DECK_TEMPLATE = \ """\\documentclass[a4paper,landscape]{{article}} @@ -18,13 +18,13 @@ DECK_TEMPLATE = \ \\end{{document}}""" -def generate_markdown(deck: list[str], language='en', indentation=0): +def generate_markdown(deck: list[str], language='en', indentation=0): # pylint: disable=W0613 """Output a markdown enumeration""" m = [] for line in deck: line = line.strip() amount, card = line.split(' ', maxsplit=1) - m += [f'{indentation}* {card}'] * amount + m += [f'{indentation}* {card}'] * int(amount) return ''.join(m) @@ -36,7 +36,7 @@ def generate_latex(deck: list[str], language='en'): line = line.strip() amount, card = line.split(' ', maxsplit=1) card = card.replace(' ', '_').lower() - containing_set = CARDS_TO_SETS[card] + containing_set = name2set(card) for _ in range(int(amount)): cards.append( f'\\includestandalone{{cards/{language}/{containing_set}/{card}}}' -- cgit v1.2.3