Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

95 lines
2.7 KiB

import glob, collections
import os
from monsters import Monster
HEADER_CHARACTERS = set('-=')
def parse_headers(spell_text):
"""Takes in the 'tail' part of a spell file"""
lines = spell_text.split("\n")
while len(lines[0].strip()) == 0:
lines = lines[1:]
heads = []
while lines:
hd, col, val = lines[0].partition(':')
if not col:
return heads,'\n'.join(lines)
heads.append((hd.strip(), val.strip()))
lines = lines[1:]
return heads, '\n'.join(lines) # Should be empty, but :shrug:
def parse_sections(body):
lines = body.split('\n')
cur_sec = None
cur_lines = []
secs = collections.OrderedDict()
skipping_leader = False
for lidx in range(len(lines) - 1):
if skipping_leader:
skipping_leader = False
continue
if lines[lidx + 1].startswith('---') or lines[lidx + 1].startswith('==='):
secs[cur_sec] = '\n'.join(cur_lines)
cur_lines = []
cur_sec = lines[lidx]
skipping_leader = True
else:
cur_lines.append(lines[lidx])
cur_lines.append(lines[-1])
secs[cur_sec] = '\n'.join(cur_lines)
return secs
def test_spell_parse():
f = open("../Spells/deceptive_sending.txt",'r')
text = '\n'.join(f.read().split("\n")[2:])
print(parse_headers(text))
def get_items(items_dir):
items_list = []
for fname in glob.glob(items_dir):
f = open(fname,'r')
lines = f.read().split('\n')
title = lines[0]
lines = lines[1:]
while not (set(lines[0]) - HEADER_CHARACTERS):
lines = lines[1:]
heads, body = parse_headers('\n'.join(lines))
secs = parse_sections(body)
items_list.append((title,heads,secs))
items_list.sort(key = lambda data: data[0])
return items_list
def get_worlds():
world_list = []
for fname in glob.glob("../Worlds/*"):
world_list.append(fname.split("/")[-1])
return world_list
def get_monsters():
monster_list = []
for fname in glob.glob("../Monsters/*"):
m = Monster(fname)
monster_list.append(m)
return monster_list
def get_new_items():
os.system("./get_last_n_items.sh")
items_list = []
with open("new_content.txt") as nf:
for fname in nf.read().split("\n")[:-1]:
print("Next item: ../"+fname)
f = open("../"+fname,'r')
lines = f.read().split('\n')
title = lines[0]
lines = lines[1:]
while not (set(lines[0]) - HEADER_CHARACTERS):
lines = lines[1:]
heads, body = parse_headers('\n'.join(lines))
secs = parse_sections(body)
items_list.append((title,heads,secs))
return items_list