-
Notifications
You must be signed in to change notification settings - Fork 1
/
genspells.py
119 lines (93 loc) · 3.22 KB
/
genspells.py
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
import sys
import re
import json
import sys
import jinja2
import os
import contextlib
from jinja2 import Template
import argparse
from functools import lru_cache
@lru_cache(maxsize=5)
def get_template(name):
latex_jinja_env = jinja2.Environment(
block_start_string="\\BLOCK{",
block_end_string="}",
variable_start_string="\\VAR{",
variable_end_string="}",
comment_start_string="\\#{",
comment_end_string="}",
line_statement_prefix="%%",
line_comment_prefix="%#",
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(
os.path.abspath(os.path.join(os.path.dirname(__file__), "templates/"))
),
)
template = latex_jinja_env.get_template(f"{name}.tex.jinja")
return template
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--spells", "-f", type=str, nargs="+")
parser.add_argument("characterclass", type=str, default="All")
parser.add_argument("--sort", "-s", type=str, default="level")
parser.add_argument("--twosided", "-2", action="store_true")
parser.add_argument("--output", "-o", nargs="?")
return parser.parse_args()
def spell_sorter(sortby):
def sort_spell(x):
srt = ""
for elem in sortby:
srt += str(x[1][elem])
return srt
return sort_spell
def main():
args = parse_args()
spells = {}
for f in args.spells:
with open(f) as jf:
spells.update(json.load(jf))
if args.characterclass != "All":
spells = dict(
filter(lambda x: args.characterclass in x[1]["classes"], spells.items())
)
spells = dict(sorted(spells.items(), key=spell_sorter(args.sort.split(","))))
pages = generate_pages(spells, args.twosided)
filename = args.output if args.output is not None else args.characterclass
write_doc(filename, pages)
def write_doc(filename, pages):
with open(f"{filename}.tex", "w+") as of:
of.write(get_template("spelldeck").render(content="".join(pages)))
def generate_pages(spells, twosided=False):
spellpages = []
back = get_template("spellcard_back").render()
for i in range(0, len(spells) + 9, 10):
spellpages.append(generate_page(list(spells.items())[i : i + 10]))
if twosided:
spellpages.append(back)
if not twosided:
spellpages.append(back)
return spellpages
def generate_page(spells):
pagetemplate = get_template("spellpage")
data = {}
for i, spell in enumerate(spells):
data[f"spell{i}"] = generate_spell(*spell)
return pagetemplate.render(**data)
def generate_spell(spellname, spell):
template = get_template("spellcard")
spell["text"] = latex_format(spell["text"])
if "text_card" in spell:
spell["text_card"] = latex_format(spell["text_card"])
return template.render(title=spellname, **spell)
def latex_format(text):
text = re.sub(r"\*(?! )(([^*])*?)(?! )\*", r"\\textbf{\1}", text)
text = re.sub(
r"(\s|\()([0-9]*W(?:4|6|8|10|12|20)(?:\s*\+[0-9]+)?)", r"\1\\textbf{\2}", text
)
text = re.sub(r"([0-9]+)\sm", r"\1~m", text)
text = re.sub(r"&", r"\&", text)
return text
if __name__ == "__main__":
main()