1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#!/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<card>.*?)\}\}')
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'''<a href="{{link_target}}"
onmouseover="{HOVER_JS}"
onmouseout="{RESET_JS}"
>
{{link_text}}
<img src="hover-placeholder.png" id="placeholder-{{pid}}" width=1 height=1 style="{CSS_STYLE}" />
</a>'''
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'<em>{card}</em>'
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()
|