diff options
author | Mischa Untaga <99098079+MischaU8@users.noreply.github.com> | 2023-11-02 15:08:24 +0100 |
---|---|---|
committer | Mischa Untaga <99098079+MischaU8@users.noreply.github.com> | 2023-11-02 15:08:34 +0100 |
commit | 8dac1a5f79a8e319bc01ca57c866032fa0b15935 (patch) | |
tree | 253da2e4b8930e7aa63c12a6274d969017a7a3cf /tools | |
parent | 664ea4d4e9f85c13389fb79ed1e4b406abffad2a (diff) | |
download | votes-for-women-8dac1a5f79a8e319bc01ca57c866032fa0b15935.tar.gz |
show cards
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gencards.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tools/gencards.py b/tools/gencards.py new file mode 100644 index 0000000..1d22e87 --- /dev/null +++ b/tools/gencards.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import json +import re + +cards = [None] + +# global card object +card = { + 'id': 1, + 'type': "", + 'title': "", + 'era': "", + 'text': [], + 'attrs': {} +} + +kv_pattern = r'<!--\s*(?P<key>[^:]+):\s*(?P<value>[^->]+)\s*-->' +file_format = "tools/{}_cards.md" + +def flush(): + if card['title']: + 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) + + # Reset card attributes for the next entry + card['title'] = "" + card['text'] = [] + card['attrs'] = {} + card['id'] += 1 + +def read_cards(_card_type): + card['type'] = _card_type + card['era'] = "" + filename = file_format.format(_card_type) + + 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['title'] = 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)) |