
    j                         d dl Z d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 ddlmZmZ dj                  d	      Zd
e
defdZdd
e
dede
fdZd
e
fdZdedefdZdedefdZy)    N)BytesIO)BinaryIO)ElementTree)BeautifulSoupTag   )OMML_NSoMath2Latex )z<w:document zOxmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" zGxmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" z2xmlns:o="urn:schemas-microsoft-com:office:office" zNxmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" zExmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" z(xmlns:v="urn:schemas-microsoft-com:vml" zQxmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" zRxmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" z2xmlns:w10="urn:schemas-microsoft-com:office:word" zGxmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" zAxmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" zNxmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" zLxmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" zAxmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" zfxmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">z{0}</w:document>tagreturnc                     t        j                  t        j                  t	        |                   }|j                  t        dz         }t        |      j                  }|S )z
    Converts an OMML (Office Math Markup Language) tag to LaTeX format.

    Args:
        tag (Tag): A BeautifulSoup Tag object representing the OMML element.

    Returns:
        str: The LaTeX representation of the OMML element.
    oMath)	ET
fromstringMATH_ROOT_TEMPLATEformatstrfindr	   r
   latex)r   	math_rootmath_elementr   s       l/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/markitdown/converter_utils/docx/pre_process.py_convert_omath_to_latexr   !   sJ     077CABI>>'G"34L%++EL    blockc                     t        d      }|rdt        |        dndt        |        d|_        t        d      }|j                  |       |S )a  
    Creates a replacement tag for an OMML (Office Math Markup Language) element.

    Args:
        tag (Tag): A BeautifulSoup Tag object representing the "oMath" element.
        block (bool, optional): If True, the LaTeX will be wrapped in double dollar signs for block mode. Defaults to False.

    Returns:
        Tag: A BeautifulSoup Tag object representing the replacement element.
    zw:tnamez$$$zw:r)r   r   stringappend)r   r   t_tagr_tags       r   _get_omath_tag_replacementr%   4   s]     UOE  $S)*"-(-.a0 
L
 UOE	LLLr   c                 H   | j                   dk(  rPt        d      }| j                  d      D ]  }|j                  t	        |d               | j                  |       y	| j                   dk(  r| j                  t	        | d             y	t        d| j                          )
a  
    Replaces OMML (Office Math Markup Language) elements with their LaTeX equivalents.

    Args:
        tag (Tag): A BeautifulSoup Tag object representing the OMML element. Could be either "oMathPara" or "oMath".

    Raises:
        ValueError: If the tag is not supported.
    	oMathParazw:pr   r   T)r   FzNot supported tag: N)r   r   find_allr"   r%   replace_with
ValueError)r   p_tag	child_tags      r   _replace_equationsr-   J   s     xx;g. 	LILL3ITJK	L 		W	3CuEF.sxxj9::r   contentc                     t        | j                         d      }|j                  d      D ]  }t        |        |j                  d      D ]  }t        |        t	        |      j                         S )a  
    Pre-processes the math content in a DOCX -> XML file by converting OMML (Office Math Markup Language) elements to LaTeX.
    This preprocessed content can be directly replaced in the DOCX file -> XMLs.

    Args:
        content (bytes): The XML content of the DOCX file as bytes.

    Returns:
        bytes: The processed content with OMML elements replaced by their LaTeX equivalents, encoded as bytes.
    xml)featuresr'   r   )r   decoder(   r-   r   encode)r.   soupr   s      r   _pre_process_mathr5   c   sj     )E:D}}[)  3 }}W%  3 t9r   
input_docxc                 X   t               }g d}t        j                  | d      5 }|j                         D ci c]  }||j	                  |       }}t        j                  |d      5 }|j
                  |_        |j                         D ]:  \  }}||v r	 t        |      }|j                  ||       )|j                  ||       < 	 ddd       ddd       |j                  d       |S c c}w # t        $ r |j                  ||       Y w xY w# 1 sw Y   JxY w# 1 sw Y   NxY w)a  
    Pre-processes a DOCX file with provided steps.

    The process works by unzipping the DOCX file in memory, transforming specific XML files
    (such as converting OMML elements to LaTeX), and then zipping everything back into a
    DOCX file without writing to disk.

    Args:
        input_docx (BinaryIO): A binary input stream representing the DOCX file.

    Returns:
        BinaryIO: A binary output stream representing the processed DOCX file.
    )zword/document.xmlzword/footnotes.xmlzword/endnotes.xmlr)modewNr   )r   zipfileZipFilenamelistreadcommentitemsr5   writestr	Exceptionseek)	r6   output_docxpre_process_enable_files	zip_inputr   files
zip_outputr.   updated_contents	            r   pre_process_docxrJ   v   s'    )K 
 
#	. 7)8A8J8J8LMy~~d++MM__[s3 	7z!*!2!2J!& 7g33;*;G*D"++D/B
 ''g67	77  Q! N % ;"++D':;	7 	77 7sX   D C.D +,DC35DD .D 3D	DD	DD	D  D))F)r;   ior   typingr   	xml.etreer   r   bs4r   r   	math.ommlr	   r
   joinr   r   r   boolr%   r-   bytesr5   rJ    r   r   <module>rT      s       ' " +WW .  &C   ,;C ;2u  && &h &r   