forked from kinkofer/5eTtoFC5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureAsFeat.py
240 lines (218 loc) · 10.6 KB
/
featureAsFeat.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# vim: set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab : #
import xml.etree.cElementTree as ET
import re
import utils
import json
import os
from slugify import slugify
from shutil import copyfile
stats = {"str":"Strength","dex":"Dexterity","con":"Constitution","int":"Intelligence","wis":"Wisdom","cha":"Charisma"}
def parseFeature(m, compendium, args):
feat = ET.SubElement(compendium, 'feat')
name = ET.SubElement(feat, 'name')
name.text = m['name']
prereqs = ET.SubElement(feat,'prerequisite')
if 'prerequisite' in m:
prereqs.text = getPrereqs(m)
if 'source' in m and not args.srd:
setSource(feat, m, args)
bodyText = ET.SubElement(feat, 'text')
bodyText.text = ""
featureTypeText = ""
featureType = m['featureType']
if type(m['featureType']) != list and ":" in m['featureType']:
featureType = [m['featureType']]
if type(featureType) == list:
featureTypes = {}
for t in featureType:
typeSubtype = t.split(':')
if len(typeSubtype) == 2:
if typeSubtype[0] not in featureTypes:
featureTypes[typeSubtype[0]] = []
featureTypes[typeSubtype[0]].append(typeSubtype[1])
for k, v in featureTypes.items():
featureTypeText += "{}: ".format(getFeatureType(k))
subs = []
for s in v:
subs.append(getFeatureSubtype(k, s))
featureTypeText += ", ".join(subs) + "\n"
bodyText.text += featureTypeText
if 'entries' in m:
bodyText.text += parseEntries(m, args)
for match in re.finditer(r'You gain proficiency in the ([^ ]*?)( and (.*?))? skill',bodyText.text):
bonusmod = ET.SubElement(feat, 'proficiency')
bonusmod.text = match.group(1)
if match.group(2) and match.group(3):
bonusmod.text = ", " + match.group(3)
def getPrereqs(m):
prereq = []
for pre in m['prerequisite']:
if 'ability' in pre:
if type(pre['ability']) == list:
abilityor = []
if all(next(iter(v.values())) == next(iter(pre['ability'][0].values())) for v in pre['ability']):
for v in pre['ability']:
for s,val in v.items():
abilityor.append(stats[s])
prereq.append("{} {} or higher".format(" or ".join(abilityor),next(iter(pre['ability'][0].values()))))
else:
for v in pre['ability']:
for s,val in v.items():
abilityor.append("{} {} or higher".format(stats[k],val))
prereq.append(" or ".join(abilityor))
else:
for k,v in pre['ability'].items():
prereq.append("{} {} or higher".format(stats[k],v))
if 'spellcasting' in pre and pre['spellcasting']:
prereq.append("The ability to cast at least one spell")
if 'proficiency' in pre:
for prof in pre['proficiency']:
for k,v in prof.items():
prereq.append("Proficiency with {} {}".format(v,k))
if 'race' in pre:
for r in pre['race']:
prereq.append("{}{}".format(r['name']," ({})".format(r['subrace']) if 'subrace' in r else "").title())
if 'spell' in pre:
for s in pre['spell']:
if '#c' in s:
prereq.append("{} cantrip".format(s.replace('#c','').title()))
else:
prereq.append("The ability to cast {}".format(s.title()))
if 'level' in pre:
level = pre['level']['level']
prereq.append("{} level".format(utils.ordinal(level)))
if 'patron' in pre:
prereq.append("{} patron".format(pre['patron']))
if 'pact' in pre:
prereq.append("Pact of the {}".format(pre['pact']))
if 'item' in pre:
for i in pre['item']:
prereq.append(i)
if 'otherSummary' in pre:
prereq.append(pre['otherSummary']['entrySummary'])
return ", ".join(prereq)
def setSource(feat, m, args):
sourcetext = "{} p. {}".format(
utils.getFriendlySource(m['source'],args), m['page']) if 'page' in m and m['page'] != 0 else utils.getFriendlySource(m['source'],args)
if 'otherSources' in m and m["otherSources"] is not None:
for s in m["otherSources"]:
if "source" not in s:
continue
sourcetext += ", "
sourcetext += "{} p. {}".format(
utils.getFriendlySource(s["source"],args), s["page"]) if 'page' in s and s["page"] != 0 else utils.getFriendlySource(s["source"],args)
if 'entries' in m:
if args.nohtml:
m['entries'].append("Source: {}".format(sourcetext))
else:
m['entries'].append("<i>Source: {}</i>".format(sourcetext))
else:
if args.nohtml:
m['entries'] = "Source: {}".format(sourcetext)
else:
m['entries'] = ["<i>Source: {}</i>".format(sourcetext)]
if not args.nohtml:
source = ET.SubElement(feat, 'source')
source.text = sourcetext
def parseEntries(m, args):
bodyText = ""
for e in m['entries']:
if "colLabels" in e:
if 'caption' in e:
bodyText += "{}\n".format(e['caption'])
bodyText += " | ".join([utils.remove5eShit(x)
for x in e['colLabels']])
bodyText += "\n"
for row in e['rows']:
rowthing = []
for r in row:
if isinstance(r, dict) and 'roll' in r:
rowthing.append(
"{}-{}".format(
r['roll']['min'],
r['roll']['max']) if 'min' in r['roll'] else str(
r['roll']['exact']))
else:
rowthing.append(utils.fixTags(str(r),m,args.nohtml))
bodyText += " | ".join(rowthing) + "\n"
elif "entries" in e:
subentries = []
if 'name' in e:
if args.nohtml:
bodyText += "{}: ".format(e['name'])
else:
bodyText += "<b>{}:</b> ".format(e['name'])
for sube in e["entries"]:
if type(sube) == str:
subentries.append(utils.fixTags(sube,m,args.nohtml))
elif type(sube) == dict and "text" in sube:
subentries.append(utils.fixTags(sube["text"],m,args.nohtml))
elif type(sube) == dict and sube["type"] == "list" and "style" in sube and sube["style"] == "list-hang-notitle":
for item in sube["items"]:
if type(item) == dict and 'type' in item and item['type'] == 'item':
if args.nohtml:
subentries.append("• {}: {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• <i>{}:</i> {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• {}".format(utils.fixTags(item,m,args.nohtml)))
elif type(sube) == dict and sube["type"] == "list":
for item in sube["items"]:
if type(item) == dict and "entries" in item:
ssubentries = []
for sse in item["entries"]:
if type(sse) == str:
ssubentries.append(utils.fixTags(sse,m,args.nohtml))
elif type(sse) == dict and "text" in sse:
ssubentries.append(utils.fixTags(sse["text"],m,args.nohtml))
subentries.append("\n".join(ssubentries))
elif type(item) == dict and 'type' in item and item['type'] == 'item':
if args.nohtml:
subentries.append("• {}: {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• <i>{}:</i> {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)))
else:
subentries.append("• {}".format(utils.fixTags(item,m,args.nohtml)))
bodyText += "\n".join(subentries) + "\n"
else:
if type(e) == dict and e["type"] == "list" and "style" in e and e["style"] == "list-hang-notitle":
for item in e["items"]:
if args.nohtml:
bodyText += "• {}: {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)) + "\n"
else:
bodyText += "• <i>{}:</i> {}".format(item["name"],utils.fixTags(item["entry"],m,args.nohtml)) + "\n"
elif type(e) == dict and e["type"] == "list":
for item in e["items"]:
if "entries" in item:
subentries = []
for sube in item["entries"]:
if type(sube) == str:
subentries.append(utils.fixTags(sube,m,args.nohtml))
elif type(sube) == dict and "text" in sube:
subentries.append(utils.fixTags(sube["text"],m,args.nohtml))
bodyText += "\n".join(subentries) + "\n"
else:
bodyText += "• {}".format(utils.fixTags(item,m,args.nohtml)) + "\n"
else:
bodyText += utils.fixTags(e,m,args.nohtml) + "\n"
bodyText = bodyText.rstrip()
return bodyText
# Return the full name of the featureType
def getFeatureType(t):
if t == "AF": return "Alchemical Formula"
elif t == "AI": return "Artificer Infusion"
elif t == "AS": return "Arcane Shot"
elif t == "ED": return "Elemental Discipline"
elif t == "EI": return "Eldritch Infusion"
elif t == "MV": return "Maneuver"
elif t == "OR": return "Onamancy Rune"
elif t == "PB": return "Pact Boon"
elif t == "Rune": return "Rune"
else: return t
# Return the subtype of the featureType, ex. "Battle Master" in MV:B
def getFeatureSubtype(t, s):
if s == "V1-UA": return "V1 (UA)"
elif s == "V2-UA": return "V2 (UA)"
elif s == "B": return "Battle Master"
elif s == "C2-UA": return "Cavalier V2 (UA)"
else: return s