#!/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 name2set, name2file, name2cardlisting 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 GenLinkTextCallback = Callable[[str], str] GenLinkTargetCallback = Callable[[str, str], 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 """ link_text = gen_link_text(card) link_target = gen_link_target(card, language) cannonical_name = name2file(card) card_img = f'{images_path}/{language}/{name2set(cannonical_name)}/{cannonical_name}.png' global PLACEHOLDER_ID # pylint: disable=W0603 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""" card_listing_target = name2cardlisting(card) return f'{language}/cards_listing.html#{card_listing_target}' def replace_all_links(string: str, images_path: 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, images_path, 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') parser.add_argument('--images', default='/muhqs-game/html/build/latex-build', type=str, help='the path to the rendered cards') 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, args.images, language=args.language)) if __name__ == '__main__': main()