import os, sys, re, unicodedata def slugify(value, allow_unicode=False): """ Taken from https://github.com/django/django/blob/master/django/utils/text.py Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated dashes to single dashes. Remove characters that aren't alphanumerics, whitespace, underscores, or hyphens. Convert to lowercase. Also strip leading and trailing whitespace. """ value = str(value) if allow_unicode: value = unicodedata.normalize('NFKC', value) else: value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') value = re.sub(r'[^\w\s-]', '', value) return value.strip() def snippet(text, seq1, seq2): try: start = text.index(seq1)+len(seq1) except ValueError: start=0 end =text.index(seq2, start+1) return text[start:end] with open('incomplete_tome_of_spells_raw.txt') as filein: raw_txt = filein.read().split('\n\n') if not os.path.exists('./spells/'): os.mkdir('spells') base_dir = './spells/ICTS' if not os.path.exists(base_dir): os.mkdir(base_dir) level = 0 for para in raw_txt: spell = {} if not para: continue try : lines = para.split('\n') while True: if not lines[0]: lines.pop(0) else: break while True: if not lines[-1]: lines.pop() else: break spell['name'] = lines.pop(0) info = lines.pop(0).lower() spell['level'] = 0 if 'cantrip' in info.lower() else int(info[0]) spell['school'] = info.split()[0].title() if 'cantrip' in info.lower() else info.split()[1].title() spell['ritual'] = 'ritual' in info spell['casting'] = lines.pop(0)[14:] spell['range'] = lines.pop(0)[7:] spell['components'] = lines.pop(0)[12:] if '(' in spell['components'] and ')' not in spell['components']: while ')' not in spell['components']: spell['components'] += lines.pop(0) spell['duration'] = lines.pop(0)[10:] spell['classes'] = lines.pop(0)[9:] spell['sources'] = lines.pop(0) spell['upcasttag'] = 'false' breaks = [] for idx, line in enumerate(lines): if line and line[-1] in ['.', ':']: breaks.append(idx) if line.startswith('**At Higher Levels.**'): spell['upcasttag'] = 'true' else: if idx not in breaks: lines[-1] = lines[-1]+'.' breaks.append(idx) if len(breaks) >1: for idx in breaks[:-1]: lines[idx]+='\n' spell['description'] = ' '.join(lines).replace('\n ','\n') if 'reaction' in spell['casting'].lower(): spell['cast_time'] = 'Reaction' elif 'bonus' in spell['casting'].lower(): spell['cast_time'] = 'Bonus acn.' elif 'action' in spell['casting'].lower(): spell['cast_time'] = 'Action' else: spell['cast_time'] = spell['casting'] spell['concentration'] = 'true' if 'concentration' in spell['duration'].lower() else 'false' spell['mats'] = spell['components'].split(' (')[0] spell['priced'] = 'true' if 'worth' in spell['components'].lower() else 'false' spell['consumed'] = 'true' if 'consume' in spell['components'].lower() else 'false' tags = [spell['school']] if spell['concentration']: tags.append('Concentration') if spell['ritual']: tags.append('Ritual') spell['tags'] = ', '.join(tags) spell['book'] = "The (in)complete Tome of Spells" spell['subtitle'] = info cardtext = """--- level: {level} components: [{mats}] material: {priced} consumed: {consumed} cast_time: {cast_time} upcast: {upcasttag} classes: [{classes}] tags: [{tags}] --- #### {name} *{subtitle}* ___ - **Casting Time:** {casting} - **Range:** {range} - **Components:** {components} - **Duration:** {duration} --- {description} *{book}* """.format(**spell) with open(os.path.join(base_dir, slugify(spell['name'])+'.md'),'w') as fileout: fileout.write(cardtext) except Exception as e: print(para) raise e