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.
 
 
 
 
 
 

75 lines
2.2 KiB

TEMPLATE_NAME = "template.tex"
def read_sexpr_file(fname):
with open(fname,'r') as f:
text = f.read()[1:].replace("\n","").replace("("," ( ").replace(")"," ) ").split(" ")
text = list(filter(bool,text))
print(text)
t = read_sexpr(text)
print(t)
return t
#takes a list of words (text split on spaces), returns a list
#runs until paren count is negative, requires first paren cut off
def read_sexpr(text):
s = []
while(True):
data = text.pop(0)
if(data == "("):
l = read_sexpr(text)
s.append(l)
elif(data == ")"):
return s
else:
s.append(data)
def output_latex(tree,tablevel=0):
#name is either first elem or first and second
s = ""
name = tree.pop(0)
s += " "*tablevel + "g[id={}]".format(name)
if(isinstance(tree[0],str)):
name = name + " " + tree.pop(0)
name += ",\n"
print()
print(name)
print(tree)
print()
s += "{\n"
tablevel += 1
s += " "*tablevel + "name=" + name
while(len(tree) > 0):
t = " "*tablevel
s += t
data = tree.pop(0) #this is a list
#switch on data[0]
if(data[0] == "sex"):
s += data[1] + ",\n"
elif(data[0] == "born"):
s += "birth={" + data[1] + "}{" + ' '.join(data[2:]) +"},\n"
elif(data[0] == "comment"):
s += "comment={" + ' '.join(data[1:]) + "},\n"
elif(data[0] == "parent"):
s += "\n" + " "*(tablevel-1) + "}\n" + " "*(tablevel-1) + "parent{\n"
s += output_latex(data[1],tablevel)
s += "\n" + t + "}"
return s
def insert_into_template(text,fname):
lines = []
with open(TEMPLATE_NAME) as template:
lines = template.readlines()
for (i,l) in enumerate(text.split("\n")):
lines.insert(15+i,l+"\n")
lines.insert(15+i+1," "*12 + "}\n")
with open(fname,'w') as out:
for l in lines:
if len(l.strip()) > 0:
out.write(l)
if __name__ == "__main__":
tree = read_sexpr_file("montaigne_family.fam")
print()
l = output_latex(tree,3)
print(l)
insert_into_template(l,"testout.tex")