| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from .omml import OMML_NS, oMath2Latex |
|
|
| |
| _M = OMML_NS |
| _A14 = "{http://schemas.microsoft.com/office/drawing/2010/main}" |
|
|
|
|
| def convert_omath(omath_element) -> str: |
| """Convert an m:oMath lxml element to LaTeX string. Returns empty string on failure.""" |
| try: |
| return str(oMath2Latex(omath_element)).strip() |
| except Exception: |
| return "" |
|
|
|
|
| def paragraph_has_math(para_element) -> bool: |
| """Check if an XML element contains OMML math (a14:m or m:oMath).""" |
| return ( |
| para_element.find(f".//{_A14}m") is not None |
| or para_element.find(f".//{_M}oMath") is not None |
| ) |
|
|
|
|
| def extract_math_from_paragraph(para_element) -> list: |
| """Extract LaTeX strings from math elements in a DrawingML paragraph XML element. |
| |
| Handles three nesting patterns: |
| 1. a14:m → m:oMath (or m:oMathPara → m:oMath) |
| 2. Direct m:oMathPara → m:oMath (not wrapped in a14:m) |
| 3. Direct m:oMath (not inside a14:m or m:oMathPara) |
| """ |
| results = [] |
| |
| for a14m in para_element.findall(f".//{_A14}m"): |
| found_omath = False |
| for omath in a14m.findall(f".//{_M}oMath"): |
| latex = convert_omath(omath) |
| if latex: |
| results.append(latex) |
| found_omath = True |
| |
| if not found_omath: |
| latex = convert_omath(a14m) |
| if latex: |
| results.append(latex) |
| |
| for omath_para in para_element.findall(f".//{_M}oMathPara"): |
| parent = omath_para.getparent() |
| if parent is not None and parent.tag == f"{_A14}m": |
| continue |
| for omath in omath_para.findall(f"{_M}oMath"): |
| latex = convert_omath(omath) |
| if latex: |
| results.append(latex) |
| for omath in para_element.findall(f".//{_M}oMath"): |
| parent = omath.getparent() |
| if parent is not None and parent.tag in (f"{_A14}m", f"{_M}oMathPara"): |
| continue |
| latex = convert_omath(omath) |
| if latex: |
| results.append(latex) |
| return results |
|
|
|
|
| __all__ = [ |
| "oMath2Latex", |
| "OMML_NS", |
| "convert_omath", |
| "paragraph_has_math", |
| "extract_math_from_paragraph", |
| ] |
|
|