utils.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """
  2. Utility functions to extract text from the supported mathematical equations from xml tags and
  3. convert them into LaTeX
  4. """
  5. from .cleaners import clean_exp
  6. ns_map = {
  7. "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
  8. "m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
  9. }
  10. def linear_expression(tag):
  11. """
  12. Just returns the text contained in the given tag while setting docxlatex_skip_iteration flags
  13. for all its children.
  14. :param tag:defusedxml.Element - An xml element which contains a math equation in linear form
  15. :return text:str - The equation in valid LaTeX syntax
  16. """
  17. text = ""
  18. for child in tag.iter():
  19. child.set("docxlatex_skip_iteration", True)
  20. text += child.text if child.text is not None else ""
  21. text = clean_exp(text)
  22. return text
  23. def qn(tag):
  24. """
  25. A utility function to turn a namespace
  26. prefixed tag name into a Clark-notation qualified tag name for lxml. For
  27. example, qn('m:oMath') returns '{http://schemas.openxmlformats.org/officeDocument/2006/math}oMath'
  28. :param tag:str - A namespace-prefixed tag name
  29. :return qn:str - A Clark-notation qualified name tag for lxml.
  30. """
  31. prefix, tag_root = tag.split(":")
  32. uri = ns_map[prefix]
  33. return "{{{}}}{}".format(uri, tag_root)