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

  1. import glob, collections
  2. import os
  3. from monsters import Monster
  4. HEADER_CHARACTERS = set('-=')
  5. def parse_headers(spell_text):
  6. """Takes in the 'tail' part of a spell file"""
  7. lines = spell_text.split("\n")
  8. while len(lines[0].strip()) == 0:
  9. lines = lines[1:]
  10. heads = []
  11. while lines:
  12. hd, col, val = lines[0].partition(':')
  13. if not col:
  14. return heads,'\n'.join(lines)
  15. heads.append((hd.strip(), val.strip()))
  16. lines = lines[1:]
  17. return heads, '\n'.join(lines) # Should be empty, but :shrug:
  18. def parse_sections(body):
  19. lines = body.split('\n')
  20. cur_sec = None
  21. cur_lines = []
  22. secs = collections.OrderedDict()
  23. skipping_leader = False
  24. for lidx in range(len(lines) - 1):
  25. if skipping_leader:
  26. skipping_leader = False
  27. continue
  28. if lines[lidx + 1].startswith('---') or lines[lidx + 1].startswith('==='):
  29. secs[cur_sec] = '\n'.join(cur_lines)
  30. cur_lines = []
  31. cur_sec = lines[lidx]
  32. skipping_leader = True
  33. else:
  34. cur_lines.append(lines[lidx])
  35. cur_lines.append(lines[-1])
  36. secs[cur_sec] = '\n'.join(cur_lines)
  37. return secs
  38. def test_spell_parse():
  39. f = open("../Spells/deceptive_sending.txt",'r')
  40. text = '\n'.join(f.read().split("\n")[2:])
  41. print(parse_headers(text))
  42. def get_items(items_dir):
  43. items_list = []
  44. for fname in glob.glob(items_dir):
  45. f = open(fname,'r')
  46. lines = f.read().split('\n')
  47. title = lines[0]
  48. lines = lines[1:]
  49. while not (set(lines[0]) - HEADER_CHARACTERS):
  50. lines = lines[1:]
  51. heads, body = parse_headers('\n'.join(lines))
  52. secs = parse_sections(body)
  53. items_list.append((title,heads,secs))
  54. items_list.sort(key = lambda data: data[0])
  55. return items_list
  56. def get_worlds():
  57. world_list = []
  58. for fname in glob.glob("../Worlds/*"):
  59. world_list.append(fname.split("/")[-1])
  60. return world_list
  61. def get_monsters():
  62. monster_list = []
  63. for fname in glob.glob("../Monsters/*"):
  64. m = Monster(fname)
  65. monster_list.append(m)
  66. return monster_list
  67. def get_new_items():
  68. os.system("./get_last_n_items.sh")
  69. items_list = []
  70. with open("new_content.txt") as nf:
  71. for fname in nf.read().split("\n")[:-1]:
  72. print("Next item: ../"+fname)
  73. f = open("../"+fname,'r')
  74. lines = f.read().split('\n')
  75. title = lines[0]
  76. lines = lines[1:]
  77. while not (set(lines[0]) - HEADER_CHARACTERS):
  78. lines = lines[1:]
  79. heads, body = parse_headers('\n'.join(lines))
  80. secs = parse_sections(body)
  81. items_list.append((title,heads,secs))
  82. return items_list