Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, json
- '''Turns spell blocks into json for use with the 5e shaped spell importer on roll20'''
- def parse_spell_plain(spell_file):
- '''for spells in plain text, can have markdown formating in description'''
- f = open(spell_file)
- raw = f.read()
- f.close()
- # print raw
- spell = {}
- # get title
- name = re.search(r'(.*)', raw).groups()
- print name
- spell['name'] = name[0].strip()
- # get spell level
- spell_level = re.search(r'(?=(?:(.*?)-[Ll]evel (\w*)\s*(\(.*\))?|(.*?)\s(?:[Cc]antrip)))',raw).groups()
- print spell_level
- spell['level'] = int(spell_level[0][0]) if spell_level[0] else 0
- spell['school'] = (spell_level[1] or spell_level[3]).capitalize().strip()
- if spell_level[2]: spell['ritual'] = True
- # get casting time
- time = re.search(r'[Cc]asting [Tt]ime:\s*(.*)',raw).groups()[0].strip()
- if '1 action' in time:
- spell['castingTime'] = 'action'
- elif '1 bonus action' in time:
- spell['castingTime'] = 'bonus'
- elif '1 minute' in time:
- spell['castingTime'] = 'minute'
- elif '1 reaction' in time:
- spell['castingTime'] = 'reaction'
- else:
- spell['castingTime'] = time
- # get range
- spell_range = re.search(r'[Rr]ange:\s*(.*)', raw).groups()
- if spell_range[0]: spell['range'] = spell_range[0].strip()
- # get components
- m = re.search(r'Components:\s*([Vv])?,?\s*([Ss])?,?\s*([Mm])?(?:\s*\((.*)\))?', raw)
- if m:
- components = m.groups()
- c = {}
- if components[0]: c['verbal'] = True
- if components[1]: c['somatic'] = True
- if components[2]: c['material'] = True
- if components[3]: c['materialMaterial'] = components[3]
- spell['components'] = c
- # get duration
- duration = re.search(r'[Dd]uration:\s*([Cc]oncentration)?,?\s*(.*)',raw).groups()
- # print duration
- spell['duration'] = duration[1].strip()
- if duration[0]: spell['concentration'] = True
- # get description and higher levels
- desc_all = re.search(r'[Dd]uration.*?$(.*)',raw,re.DOTALL | re.MULTILINE).groups()
- desc = re.split(r'[Aa]t [Hh]igher [Ll]evels\.?', desc_all[0])
- if spell['castingTime'] is 'reaction':
- spell['description'] = time +'\n' + desc[0].strip()
- else:
- spell['description'] = desc[0].strip()
- if len(desc)>1: spell['higherLevel'] = desc[1].strip()
- # get save
- m = re.search(r'\s(\w*)\s*[Ss]aving [Tt]hrow', spell['description'])
- save = m.groups() if m else []
- # get attack
- attack = re.search(r'spell attack',spell['description'])
- # get heal
- heal = re.search(r'regain.*hit\s*points\s*equal\s*to\s*(\d+d\d+)',spell['description'])
- # get damage based on two common damage formats
- damage = re.findall(r'(\d+d\d+)\s*([\w ]*?)\s*[Dd]amage', spell['description'])
- damage_abil = re.search(r'\s(\w*)\s*[Dd]amage equal to (\d+d\d+)', spell['description'])
- # higher level
- hd = None
- if 'higherLevel' in spell:
- hd = re.search(r'(\d+)(d\d+)', spell['higherLevel'])
- hl = re.search(r'\s(\w*)\s*slot level',spell['higherLevel'])
- if hd: higher_damage = hd.groups()
- if hl: higher_levels = hl.groups()
- # print damage
- spell_stats = {}
- if save and save[0]:
- spell_stats['ability'] = save[0]
- suc = re.search(r'successful save.*(half as much damage)',spell['description'])
- if suc: spell_stats['saveSuccess'] = 'half damage'
- if save or attack or heal:
- if damage:
- spell_stats['damage'] = damage[0][0]
- spell_stats['damageType'] = damage[0][1]
- if len(damage) > 1:
- spell_stats['secondaryDamage'] = damage[1][0]
- spell_stats['secondaryDamageType'] = damage[1][1]
- elif damage_abil:
- d = damage_abil.groups()
- spell_stats['damage'] = d[1]
- spell_stats['damageType'] = d[0]
- spell_stats['castingStat'] = True
- elif heal:
- h = heal.groups()
- spell_stats['amount'] = h[0]
- spell_stats['castingStat'] = True
- if hd:
- spell_stats['higherLevelDice'] = .49 if higher_levels and 'two' in higher_levels else higher_damage[0]
- spell_stats['higherLevelDie'] = higher_damage[1]
- if 'secondaryDamage' in spell_stats:
- spell_stats['higherLevelSecondaryDice'] = spell_stats['higherLevelDice']
- spell_stats['higherLevelSecondaryDie'] = spell_stats['higherLevelDie']
- # set save attack or heal
- if save:
- spell['save'] = spell_stats
- elif attack:
- spell['attack'] = spell_stats
- elif heal:
- spell['heal'] = spell_stats
- # print spell_stats
- print spell
- filename = 'spells/%s.json' % (spell['name'].lower().replace(' ','-').replace('/','-'))
- print filename
- f = open(filename,'w')
- json.dump(spell, f, separators=(',', ':'), indent=4)
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement