#!/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
import sys
from data import name2set, name2file, name2cardlisting
ROOT_URL = '/muhqs-game/html/build'
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='{placeholder_png_prefix}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,
placeholder_png_prefix="../") -> 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
:param placeholder_png_prefix: prefix of the path to the placeholder png
:returns: the code of the hoverable link
"""
link_text = gen_link_text(card)
link_target = gen_link_target(card, language)
cannonical_name = name2file(card)
if '/' not in cannonical_name:
cannonical_name = f'{name2set(cannonical_name)}/{cannonical_name}'
card_img = f'{images_path}/{language}/{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,
placeholder_png_prefix=placeholder_png_prefix)
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'{path_prefix}{language}/cards_listing.html#{card_listing_target}'
def replace_all_links(string: str,
images_path: str,
language='en',
link_path_prefix='',
placeholder_png_prefix='../') -> str:
"""Replace a document containing all included cards"""
def gen_link_text(card):
return f'{card}'
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,
placeholder_png_prefix=placeholder_png_prefix)
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=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')
parser.add_argument('--placeholder-path-prefix',
default='../',
type=str,
help='a prefix to the path to the cardlisting')
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 = sys.stdin.read()
print(
replace_all_links(content,
args.images,
language=args.language,
link_path_prefix=args.cardlisting_path_prefix,
placeholder_png_prefix=args.placeholder_path_prefix))
if __name__ == '__main__':
main()