shorthand_filter.py 640 B

123456789101112131415161718192021222324252627282930
  1. #! /usr/bin/env python3
  2. import panflute as pf
  3. shorthands = {
  4. r'\um': r'$\mu \mathrm{m}$',
  5. }
  6. def handle_shorthand(elem, doc):
  7. if isinstance(elem, pf.Str):
  8. for shorthand, replacement in shorthands.items():
  9. parts = elem.text.split(shorthand)
  10. if len(parts) > 1:
  11. # build a new thinggy
  12. pass
  13. else:
  14. new = elem.text.replace(shorthand, replacement)
  15. if new != elem.text:
  16. print(new, elem.text)
  17. return elem
  18. def main(doc=None):
  19. return pf.run_filter(handle_shorthand, doc=doc)
  20. if __name__ == "__main__":
  21. main()