summaryrefslogtreecommitdiff
path: root/tools/gencards.py
blob: 2cb04d02e0bf92959129a28dcc3901d6db5f519f (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
#!/usr/bin/env python3

import json
import re

cards = [None]

# global card object
card = {
    'id': 1,
    'type': "",
    'name': "",
    'era': "",
    'text': [],
    'attrs': {}
}

kv_pattern = r'<!--\s*(?P<key>[^:]+):\s*(?P<value>[^->]+)\s*-->'
file_format = "tools/{}_cards.md"

def flush():
    if card['name']:
        output = card.copy()
        # Combine text into a single string
        output['text'] = " ".join(output['text'])

        # Integrate attrs into the card object and then clear attrs
        for key, value in output['attrs'].items():
            output[key] = value
        output.pop('attrs')

        # Add era only if it's a non-empty string
        if not output['era']:
            output.pop('era')

        cards.append(output)

        # print("CARD {} - {}\n# {}\n".format(output['id'], output['name'], output['text']))

        # Reset card attributes for the next entry
        card['name'] = ""
        card['text'] = []
        card['attrs'] = {}
        card['id'] += 1

def read_cards(_card_type):
    card['type'] = _card_type
    card['era'] = ""
    filename = file_format.format(_card_type)
    # print("# {} Cards\n".format(_card_type.title()))

    with open(filename) as fp:
        for line in fp:
            line = line.rstrip()
            if line.startswith("# "):
                flush()
                card['era'] = line[2:].strip()
            elif line.startswith("## "):
                flush()
                card['name'] = line[3:].strip()
            elif match := re.match(kv_pattern, line):
                key = match.group('key').strip()
                value = match.group('value').strip()
                card['attrs'][key] = value
            elif line:
                card['text'].append(line)
    flush()

if __name__ == "__main__":
    read_cards("support")
    read_cards("opposition")
    read_cards("strategy")
    read_cards("states")

    print("const CARDS = " + json.dumps(cards))
    print("if (typeof module !== 'undefined') module.exports = {CARDS}")