#!/usr/bin/env python3 import argparse from pathlib import Path import re from typing import Callable from data import CARDS_TO_SETS PATTERN = re.compile(r'\{\{(?P.*?)\}\}') CSS_STYLE = 'zindex: 100; position: absolute;' HOVER_JS = \ """var d = document; var o=d.getElementById('placeholder-{pid}'); o.src='{img_target}'; o.width=355; o.height=532; var b = o.getBoundingClientRect(); // make sure the popup image is in the viewport 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'; }} if (left > 0) {{ o.style.left = (b.left - left) + + w.pageXOffset + 'px'; }} """ RESET_JS = \ """var o=document.getElementById('placeholder-{pid}'); o.src='hover-placeholder.png'; o.width=1; o.height=1;""" HOVERABLE_LINK = \ f''' {{link_text}} ''' PLACEHOLDER_ID = 0 GenLinkCallback = Callable[[str, str], str] def gen_hoverable_link(card: str, gen_link_text: Callable[[str], str], gen_link_target: GenLinkCallback, 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 language: the language :returns: the code of the hoverable link """ link_text = gen_link_text(card) 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' 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 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('!', '') return f'{language}/cards_listing.html#{card_listing_target}' 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) 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()