U
    \>e                     @  s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
Z
erPddlmZ d	d
ddddZG dd de	jZG dd deZG dd deZe	dG dd deZdS )a5  

Post-processors run on the text of the entire document after is has been serialized into a string.
Postprocessors should be used to work with the text just before output. Usually, they are used add
back sections that were extracted in a preprocessor, fix up outgoing encodings, or wrap the whole
document.

    )annotations)OrderedDict)TYPE_CHECKINGAny   )utilN)Markdownr   r   zutil.Registry[Postprocessor])mdkwargsreturnc                 K  s.   t  }|t| dd |t dd |S )z0 Build the default postprocessors for Markdown. Zraw_html   Zamp_substitute   )r   ZRegistryregisterRawHtmlPostprocessorAndSubstitutePostprocessor)r	   r
   Zpostprocessors r   ;/tmp/pip-unpacked-wheel-wj0og6ym/markdown/postprocessors.pybuild_postprocessors(   s    r   c                   @  s    e Zd ZdZdddddZdS )Postprocessora  
    Postprocessors are run after the ElementTree it converted back into text.

    Each Postprocessor implements a `run` method that takes a pointer to a
    text string, modifies it as necessary and returns a text string.

    Postprocessors must extend `Postprocessor`.

    strtextr   c                 C  s   dS )z
        Subclasses of `Postprocessor` should implement a `run` method, which
        takes the html document as a single text string and returns a
        (possibly modified) string.

        Nr   selfr   r   r   r   run;   s    zPostprocessor.runN__name__
__module____qualname____doc__r   r   r   r   r   r   0   s   
r   c                   @  sJ   e Zd ZdZedZdddddZdddd	d
ZdddddZ	dS )r   z# Restore raw html to the document. z^\<\/?([^ >]+)r   r   c                   s   t   t| jjjD ]L}| | jjj| }| |rN| d| jj	|< | | jj	|< qddd fdd} rt
jd }td| d	| }|||}n|S ||kr|S | |S d
S )z+ Iterate over html stash and restore html. z	<p>{}</p>re.Match[str]r   mr   c                   sF   |  d}| kr>|dd  kr:d |dd   dS |S  | S )Nr      <p>z</p>)group)r"   keyreplacementsr   r   substitute_matchT   s    
z2RawHtmlPostprocessor.run.<locals>.substitute_matchz([0-9]+)r%   z</p>|N)r   ranger	   Z	htmlStashZhtml_counterstash_to_stringZrawHtmlBlocksisblocklevelformatZget_placeholderr   ZHTML_PLACEHOLDERrecompilesubr   )r   r   ihtmlr*   Zbase_placeholderpatternZprocessed_textr   r(   r   r   J   s$    

zRawHtmlPostprocessor.runbool)r3   r   c                 C  s<   | j |}|r8|dd dkr&dS | j|dS dS )z( Check is block of HTML is block-level. r   r   )!?@%TF)BLOCK_LEVEL_REGEXmatchr&   r	   Zis_block_level)r   r3   r"   r   r   r   r-   k   s    z!RawHtmlPostprocessor.isblocklevelc                 C  s   t |S )z' Convert a stashed object to a string. )r   r   r   r   r   r,   u   s    z$RawHtmlPostprocessor.stash_to_stringN)
r   r   r   r   r/   r0   r:   r   r-   r,   r   r   r   r   r   E   s
   
!
r   c                   @  s    e Zd ZdZdddddZdS )r   z Restore valid entities r   r   c                 C  s   | tjd}|S )N&)replacer   ZAMP_SUBSTITUTEr   r   r   r   r   }   s    zAndSubstitutePostprocessor.runNr   r   r   r   r   r   z   s   r   zThis class is deprecated and will be removed in the future; use [`UnescapeTreeprocessor`][markdown.treeprocessors.UnescapeTreeprocessor] instead.c                   @  sF   e Zd ZdZedejej	Z
dddddZdddd	d
ZdS )UnescapePostprocessorz Restore escaped chars. z	{}(\d+){}r    r   r!   c                 C  s   t t|dS )Nr   )chrintr&   )r   r"   r   r   r   unescape   s    zUnescapePostprocessor.unescaper   c                 C  s   | j | j|S )N)REr1   rA   r   r   r   r   r      s    zUnescapePostprocessor.runN)r   r   r   r   r/   r0   r.   r   ZSTXZETXrB   rA   r   r   r   r   r   r>      s   r>   )r   
__future__r   collectionsr   typingr   r    r   r/   Zmarkdownr   r   Z	Processorr   r   r   
deprecatedr>   r   r   r   r   <module>   s   	5