generateDefinitions.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import re, glob
  4. def get_definitions(filename):
  5. with open(filename) as f:
  6. content = f.read()
  7. pattern = re.compile(r"^\\begin{definition}(.*?)\\end{definition}", re.DOTALL | re.UNICODE | re.MULTILINE)
  8. index_pattern = re.compile(r"\\xindex{(?:.*?@)?(.*?)(?:\|.*?)?}", re.UNICODE)
  9. todo_pattern = re.compile(r"\\todo{.*?}", re.UNICODE)
  10. definitions = re.findall(pattern, content)
  11. def_dict_list = []
  12. for definition in definitions:
  13. names = re.findall(index_pattern, definition)
  14. names = map(lambda s: s.replace("!", ", "), names)
  15. name = "\\\\".join(names)
  16. definition = re.sub(todo_pattern, "", definition)
  17. def_dict_list.append({"name":name, "definition":definition})
  18. #return "\n\n".join('\\vspace*{{\\fill}}\n{0}\n\\vspace*{{\\fill}}\\clearpage'.format(definition["definition"]) for definition in def_dict_list)
  19. return "\n\n".join('\\begin{{flashcard}}{{ {1} }}\n{{ {0} }}\n\\end{{flashcard}}'.format(definition["definition"], definition["name"]) for definition in def_dict_list)
  20. def write_definitions_to_template(definitions, template="mathe-vorlage.tex", target="definitionen.tex"):
  21. with open(template) as f:
  22. content = f.read()
  23. content = content.replace('%CONTENT%', definitions)
  24. with open(target, 'w') as f:
  25. f.write(content)
  26. if __name__ == "__main__":
  27. definitions = []
  28. for texsource in sorted(glob.glob("../Kapitel*.tex")):
  29. definitions.append(get_definitions(texsource))
  30. write_definitions_to_template("\n\n\n".join(definitions), "flashcards-try.tex")