generateDefinitions.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  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)
  8. index_pattern = re.compile(r"\\xindex{(?:.*?@)?(.*?)(?:\|.*?)?}", re.UNICODE)
  9. definitions = re.findall(pattern, content)
  10. def_dict_list = []
  11. for definition in definitions:
  12. names = re.findall(index_pattern, definition)
  13. names = map(lambda s: s.replace("!", ", "), names)
  14. name = "\\\\".join(names)
  15. def_dict_list.append({"name":name, "definition":definition})
  16. #return "\n\n".join('\\vspace*{{\\fill}}\n{0}\n\\vspace*{{\\fill}}\\clearpage'.format(definition["definition"]) for definition in def_dict_list)
  17. return "\n\n".join('\\begin{{flashcard}}{{ {1} }}\n{{ {0} }}\n\\end{{flashcard}}'.format(definition["definition"], definition["name"]) for definition in def_dict_list)
  18. def write_definitions_to_template(definitions, template="mathe-vorlage.tex", target="definitionen.tex"):
  19. with open(template) as f:
  20. content = f.read()
  21. content = content.replace('%CONTENT%', definitions)
  22. with open(target, 'w') as f:
  23. f.write(content)
  24. if __name__ == "__main__":
  25. definitions = []
  26. for texsource in sorted(glob.glob("../Kapitel*.tex")):
  27. definitions.append(get_definitions(texsource))
  28. write_definitions_to_template("\n\n\n".join(definitions), "flashcards-try.tex")