aboutsummaryrefslogtreecommitdiff
path: root/scripts/generate_card_hover_links.py
blob: d3b11f3fde19ded55f4cdd676b85cc874dc2b622 (plain)
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
#!/usr/bin/env python3
import argparse
from pathlib import Path
import re

from data import CARDS_TO_SETS

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


def replace_with_link(match, language) -> str:
    card = match.group('card')
    link_text = f'<em>{card}</em>'

    cannonical_name = card.lower().replace(' ', '_')
    card_listing_target = cannonical_name.replace('_', '-').replace('!', '')
    link_target = f'{language}/cards_listing.html#{card_listing_target}'
    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 replace_all_links(string: str, language='en') -> str:
    """Replace a document containing all included cards"""
    sub_func = lambda match: replace_with_link(match, language=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()