a
    ¡˜ëjU  ã                
   @  sÐ   U d dl mZ d dlZd dlmZmZmZ d dlZd dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d
ddœdd„ZdZded< dddœdd„Zddddddddddœdd„ZdS )é    )ÚannotationsN)ÚIOÚAnyÚMatch)ÚElement)Úread_txt_file)ÚFileType)Úexactly_one)Úget_last_modified_date)Úpartition_htmlzstr | bytesÚstr)ÚcontentsÚreturnc                 C  s   t | tƒr|  d¡S | S )Nzutf-8)Ú
isinstanceÚbytesÚdecode)r   © r   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/unstructured/partition/md.pyÚoptional_decode   s    

r   ZmdÚDETECTION_ORIGIN)Útextr   c                 C  s(   d}dddœdd„}t j||| t jd�S )a^  Pre-process code blocks so that processing instructions can be properly escaped.

    The markdown library can fail to properly escape processing instructions like <?xml>, <?php>,
    etc. in code blocks. This function adds minimal indentation to the processing instruction line
    to force markdown to treat it as text content rather than XML.
    z3```\s*\n([ \t]{0,3})?(<\?[a-zA-Z][^>]*\?>.*?)\n?```z
Match[str]r   )Úmatchr   c                 S  s0   |   d¡}| ¡  d¡r$d| ¡  }d|› d�S )Né   z<?z    z```
z
```)ÚgroupÚlstripÚ
startswith)r   Úcontentr   r   r   Úindent_processing_instruction(   s    
zG_preprocess_markdown_code_blocks.<locals>.indent_processing_instruction)Úflags)ÚreÚsubÚDOTALL)r   Zcode_block_patternr   r   r   r   Ú _preprocess_markdown_code_blocks   s    r"   z
str | NonezIO[bytes] | Noner   zlist[Element])ÚfilenameÚfiler   ÚurlÚmetadata_filenameÚmetadata_last_modifiedÚkwargsr   c                 K  sî   |du rd}t | |||d� | r(t| ƒnd}| durDt| d�\}}nn|dur\t|d�\}}nV|dur²t |¡}	|	js„td|	j› �ƒ‚|	j dd¡}
|
 	d¡s¬td	|
› d
�ƒ‚|	j
}t|ƒ}tj|dgd�}tf ||pÖ| tj|pà|tdœ|¤ŽS )aÚ  Partitions a markdown file into its constituent elements

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object using "rb" mode --> open(filename, "rb").
    text
        The string representation of the markdown document.
    url
        The URL of a webpage to parse. Only for URLs that return a markdown document.
    metadata_last_modified
        The last modified date for the document.
    NÚ )r#   r$   r   r%   )r#   )r$   zURL return an error: zContent-Typeztext/markdownz)Expected content type text/markdown. Got Ú.Ztables)Ú
extensions)r   r&   Zmetadata_file_typer'   Zdetection_origin)r	   r
   r   ÚrequestsÚgetÚokÚ
ValueErrorÚstatus_codeÚheadersr   r   r"   Úmarkdownr   r   ZMDr   )r#   r$   r   r%   r&   r'   r(   Zlast_modifiedÚ_ÚresponseÚcontent_typeZprocessed_textÚhtmlr   r   r   Úpartition_md2   s:    


ÿûúr7   )NNNNNN)Ú
__future__r   r   Útypingr   r   r   r2   r,   Zunstructured.documents.elementsr   Z unstructured.file_utils.encodingr   Zunstructured.file_utils.modelr   Z$unstructured.partition.common.commonr	   Z&unstructured.partition.common.metadatar
   Zunstructured.partition.htmlr   r   r   Ú__annotations__r"   r7   r   r   r   r   Ú<module>   s(         ú