aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2022-01-04 14:39:57 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2022-01-04 14:39:57 +0100
commit0c797df47f0e65f6aeadd917e21ca580866723de (patch)
tree9d4666d7f64b44f21e3f94467e0ff6cad1f7a5bc
parent791a83da34ff35f7dc159e7f8d5f060f02c6bc5a (diff)
downloadmuhqs-game-0c797df47f0e65f6aeadd917e21ca580866723de.tar.gz
muhqs-game-0c797df47f0e65f6aeadd917e21ca580866723de.zip
use python to generate card listings
The previous make/shell based approach results in only a single placeholder id 'placeholder-0' because we invoke generate_card.py for each card which will initialize the placeholder id count each time with 0. Now we generate the whole car listing in python which greatly reduces the number of program invocations and allows the placeholder id to be incremented for each listed card.
-rw-r--r--html/Makefile12
-rwxr-xr-xscripts/generate_card_listing.py51
2 files changed, 55 insertions, 8 deletions
diff --git a/html/Makefile b/html/Makefile
index e4c09aab..83740d14 100644
--- a/html/Makefile
+++ b/html/Makefile
@@ -20,6 +20,8 @@ LATEX_BUILDDIR := $(LATEX_ROOT)/build
GENERATE_CARD := $(GAME_ROOT)/scripts/generate_card.py
GENERATE_CARD_ARGS := --format markdown
+GENERATE_CARD_LISTING := $(GAME_ROOT)/scripts/generate_card_listing.py
+
GENERATE_HOVER_LINKS := $(GAME_ROOT)/scripts/generate_card_hover_links.py
SETS := $(shell find $(CARDS_YML_DIR) -mindepth 1 -type d -printf "%f\n")
@@ -88,17 +90,11 @@ $(BUILDDIR)/cards-data: $(LANG_CARDS_LISTINGS) $(BUILDDIR)/latex-build
$(VERBOSE) ln -sfT $(DATA_ROOT)/cards $@
define generateCardsListing
-$(1)/cards_listing.md: $(CARDS_YAML) $(GENERATE_CARD) $(MAKEFILE_LIST)
+$(1)/cards_listing.md: $(CARDS_YAML) $(GENERATE_CARD) $(GENERATE_CARD_LISTING) $(MAKEFILE_LIST)
@echo "building $$@"
@if test \( ! \( -d $$(@D) \) \) ;then mkdir -p $$(@D);fi
$(VERBOSE) echo -e "% Cards" > $$@
- $(VERBOSE) for set in $(SETS); \
- do echo -e "\n## [$$$$set](../cards/$(2)/$$$$set.pdf)" >> $$@; \
- for card in $(CARDS_YML_DIR)/$$$$set/*.yml; \
- do echo "" >> $$@; \
- $(GENERATE_CARD) $(GENERATE_CARD_ARGS) --language=$(2) $$$$card >> $$@; \
- done; \
- done
+ $(VERBOSE) $(GENERATE_CARD_LISTING) --language=$(2) $(CARDS_YML_DIR) >> $$@;
$(1)/cards_listing.html: $(1)/cards_listing.md $(HTML_TEMPLATE)
@echo "building $$@"
diff --git a/scripts/generate_card_listing.py b/scripts/generate_card_listing.py
new file mode 100755
index 00000000..ee6c9e68
--- /dev/null
+++ b/scripts/generate_card_listing.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+"""Generate markdown card listing from yaml card definitions"""
+
+import argparse
+from pathlib import Path
+import sys
+import yaml
+
+import generate_card
+
+
+def main():
+ """Generate a markdown card listing"""
+
+ parser = argparse.ArgumentParser(
+ description='generate makrdown card listing')
+ parser.add_argument('data', help='directory containing the card data')
+ parser.add_argument('--language',
+ choices=['en', 'de'],
+ default='en',
+ help='the language of the card text')
+
+ args = parser.parse_args()
+
+ data_path = Path(args.data)
+ if not data_path.is_dir():
+ print('data must be a directory', file=sys.stderr)
+ sys.exit(1)
+
+ set_dirs = [d for d in data_path.glob('*') if d.is_dir()]
+ if not set_dirs:
+ print('data must contain at least one set subdirectory',
+ file=sys.stderr)
+ sys.exit(1)
+
+ for set_dir in set_dirs:
+ set_name = set_dir.name
+ print(
+ f'## [{set_name}](../latex-build/{args.language}/{set_name}.pdf)')
+
+ for card_path in set_dir.glob('*.yml'):
+ with open(card_path, 'r', encoding="utf8") as card_file:
+ card = yaml.full_load(card_file)
+
+ card['set'] = set_name
+ generate_card.generate_markdown(card, language=args.language)
+ print()
+
+
+if __name__ == '__main__':
+ main()