o
    7d?                     @   s   d Z ddlZddlm  mZ ddlmZ ddlmZ dd Zdd	 Z	G d
d dej
ZG dd deZG dd deZG dd deZdS )a  
Python Markdown

A Python implementation of John Gruber's Markdown.

Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).
    N   )util)inlinepatternsc                 K   sB   t  }|t| dd |t| dd |t| dd |S )z3 Build the default  `treeprocessors` for Markdown. inline   Zprettify
   unescaper   )r   ZRegistryregisterInlineProcessorPrettifyTreeprocessorUnescapeTreeprocessor)mdkwargsZtreeprocessors r   nC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\markdown/treeprocessors.pybuild_treeprocessors   s
   r   c                 C   s   t | tjst | tS dS )z Check if it's string F)
isinstancer   AtomicStringstr)sr   r   r   isString%   s   
r   c                   @   s   e Zd ZdZdd ZdS )Treeprocessora5  
    `Treeprocessor`s are run on the `ElementTree` object before serialization.

    Each `Treeprocessor` implements a `run` method that takes a pointer to an
    `ElementTree`, modifies it as necessary and returns an `ElementTree`
    object.

    `Treeprocessors` must extend `markdown.Treeprocessor`.

    c                 C   s   dS )a*  
        Subclasses of `Treeprocessor` should implement a `run` method, which
        takes a root `ElementTree`. This method can return another `ElementTree`
        object, and the existing root `ElementTree `will be replaced, or it can
        modify the current tree and return `None`.
        Nr   )selfrootr   r   r   run7   s   zTreeprocessor.runN)__name__
__module____qualname____doc__r   r   r   r   r   r   ,   s    
r   c                   @   sj   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdddZ	dddZ
dddZdd ZdddZdS )r
   zL
    A `Treeprocessor` that traverses a tree, applying inline patterns.
    c                 C   sJ   t j| _t j| _dt| j t| j | _t j| _|| _	|j
| _
g | _d S )N   )r   ZINLINE_PLACEHOLDER_PREFIX$_InlineProcessor__placeholder_prefixETXZ$_InlineProcessor__placeholder_suffixlenZ$_InlineProcessor__placeholder_lengthZINLINE_PLACEHOLDER_RE _InlineProcessor__placeholder_rer   inlinePatterns	ancestors)r   r   r   r   r   __init__F   s   
zInlineProcessor.__init__c                 C   s    dt | j }tj| }||fS )z Generate a placeholder z%04d)r"   stashed_nodesr   ZINLINE_PLACEHOLDER)r   typeidhashr   r   r   Z__makePlaceholderP   s   
z!InlineProcessor.__makePlaceholderc                 C   s0   | j ||}|r|d| fS d|d fS )z
        Extract id from data string, start from index

        Keyword arguments:

        * `data`: string
        * `index`: index, from which we start search

        Returns: placeholder id and string index, after the found placeholder.

        r   N)r#   searchgroupend)r   dataindexmr   r   r   Z__findPlaceholderV   s   z!InlineProcessor.__findPlaceholderc                 C   s   |  |\}}|| j|< |S )z Add node to stash )!_InlineProcessor__makePlaceholderr'   )r   noder(   placeholderr)   r   r   r   Z__stashNodeh   s   
zInlineProcessor.__stashNoder   c                 C   sV   t |tjs)d}t| j}||k r)| | j| |||\}}}|s%|d7 }||k s|S )a  
        Process string with inline patterns and replace it
        with placeholders

        Keyword arguments:

        * `data`: A line of Markdown text
        * `patternIndex`: The index of the `inlinePattern` to start with

        Returns: String with placeholders.

        r   r   )r   r   r   r"   r$   _InlineProcessor__applyPattern)r   r.   patternIndex
startIndexcountZmatchedr   r   r   Z__handleInlinen   s   

zInlineProcessor.__handleInlineTc                 C   sv   |r	|j }d|_ n|j}d|_| |||}|s&||ur&t||d }nd}|  |D ]
}|||d  q.dS )aE  
        Process placeholders in `Element.text` or `Element.tail`
        of Elements popped from `self.stashed_nodes`.

        Keywords arguments:

        * `node`: parent node
        * `subnode`: processing node
        * `isText`: boolean variable, True - it's text, False - it's a tail

        Returns: None

        Nr   r   )texttail%_InlineProcessor__processPlaceholderslistr/   reverseinsert)r   r2   ZsubnodeisTextr8   ZchildResultposnewChildr   r   r   Z__processElementText   s   z$InlineProcessor.__processElementTextc                    sR   fdd}g d}|r| | j|}|dkr| ||\}}|| jv r|| j|}	|dkr:||| }
||
 t|	sf|	gt|	 D ]}|jrV|j rV| 	|	|d |j
rd|j
 rd| 	|| qEn||	 |}q|}|	| jdd f n)|t| j }||||  |}n||d }
t|tjrt|
}
||
 d}|sS )aS  
        Process string with placeholders and generate `ElementTree` tree.

        Keyword arguments:

        * `data`: string with placeholders instead of `ElementTree` elements.
        * `parent`: Element, which contains processing inline data

        Returns: list with `ElementTree` elements with applied inline patterns.

        c                    s   | rEr!d d j rd d  j | 7  _ d S | d d _ d S  s4j r/ j | 7  _ d S | _ d S jr@ j| 7  _d S | _d S d S )Nr   )r9   r8   )r8   r>   parentresultr   r   linkText   s   

z7InlineProcessor.__processPlaceholders.<locals>.linkTextr   rA   FN )findr    !_InlineProcessor__findPlaceholderr'   getr   r;   r9   strip$_InlineProcessor__processElementTextr8   appendr%   r"   r   r   r   )r   r.   rC   r>   rE   ZstrartIndexr/   r)   Z
phEndIndexr2   r8   childr-   r   rB   r   Z__processPlaceholders   sN   




*z%InlineProcessor.__processPlaceholdersc                 C   s  t |tj}|jD ]}| | jv r|ddf  S q	|rEd}| ||D ]}|||\}}	}
|	du s9|
du rC||	d7 }d}q& n| 
||d }|d| }|s]|ddfS |sn||}|d}	|	d}
|du rw|d|
fS t|st |jtjs|gt| D ],}t|s|jr| j|j  | |j|d |_| j  |jr| |j||_q| || }|rd|d|	 |||
d ddfS d||d|| d ddfS )	a  
        Check if the line fits the pattern, create the necessary
        elements, add it to `stashed_nodes`.

        Keyword arguments:

        * `data`: the text to be processed
        * `pattern`: the pattern to be checked
        * `patternIndex`: index of current pattern
        * `startIndex`: string index, from which we start searching

        Returns: String with placeholders instead of `ElementTree` elements.

        Fr   NTr   z{}{}{}z{}{}{}{}rA   )r   r   r
   ZANCESTOR_EXCLUDESlowerr%   ZgetCompiledRegExpfinditerZhandleMatchr-   matchstartr   r8   r   r   r;   rL   tag_InlineProcessor__handleInlinepopr9   _InlineProcessor__stashNoder(   formatr,   groups)r   patternr.   r5   r6   	new_styleexcluderP   r2   rQ   r-   ZleftDatarM   r3   r   r   r   Z__applyPattern   sj   







zInlineProcessor.__applyPatternc                 C   sN   g }|dur|dur| |j  | j|}|dus|  || dS )zBuild the ancestor list.N)rL   rR   rN   
parent_maprI   r<   extend)r   rC   parentsr%   r   r   r   Z__build_ancestors;  s   z!InlineProcessor.__build_ancestorsNc                 C   s  i | _ |du r	g n|dd }dd | D | _||fg}|r| \}}|| _| || j g }|D ]}|jrtt|jtj	st| j
|j  |j}	d|_| | |	|}
|
D ]	}|| j|d < qZ||
7 }|
||
f | j  |jr| |j}td}d|_| ||d}|jr|j|_t||d }|  |D ]}|| j|d < |||d  qt|r|| j|< |
|| jdd f q4|D ]\}}
t|
D ]\}}|d }||| qq|s |S )a  Apply inline patterns to a parsed Markdown tree.

        Iterate over `ElementTree`, find elements with inline tag, apply inline
        patterns and append newly created Elements to tree.  If you don't
        want to process your data with inline patterns, instead of normal
        string, use subclass `AtomicString`:

            node.text = markdown.AtomicString("This will not be processed.")

        Arguments:

        * `tree`: `ElementTree` object, representing Markdown tree.
        * `ancestors`: List of parent tag names that precede the tree node (if needed).

        Returns: `ElementTree` object with applied inline patterns.

        Nc                 S   s   i | ]
}|D ]}||qqS r   r   ).0pcr   r   r   
<dictcomp>]  s    z'InlineProcessor.run.<locals>.<dictcomp>r   dFr   )r'   iterr[   rT   r%   !_InlineProcessor__build_ancestorsr8   r   r   r   rL   rR   rN   r:   rS   r9   etreeElementr;   r/   r<   r=   r"   	enumerate)r   treer%   Ztree_parentsstackZcurrElementr]   ZinsertQueuerM   r8   lstitemr9   ZdumbyZ
tailResultr?   r@   elementiobjr   r   r   r   E  s^   




*zInlineProcessor.run)r   )TN)r   r   r   r   r&   r1   rH   rU   rS   rK   r:   r4   rd   r   r   r   r   r   r
   A   s    



 
KJ
r
   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z' Add line breaks to the html document. c                 C   s   d}| j |jr7|jdvr7|jr|j s&t|r&| j |d jr&||_|D ]}| j |jr6| | q(|jr?|j sD||_dS dS )z8 Recursively add line breaks to `ElementTree` children. 
)codeprer   N)r   Zis_block_levelrR   r8   rJ   r"   _prettifyETreer9   )r   elemrm   er   r   r   rs     s   

z$PrettifyTreeprocessor._prettifyETreec                 C   s   |  | |d}|D ]}|jr|j sd|_qd|j |_q|d}|D ]%}t|rM|d jdkrM|d }t|sM|jdurMt|j	 d |_q(dS )z/ Add line breaks to `ElementTree` root object. brrp   z
%srr   r   rq   N)
rs   rc   r9   rJ   r"   rR   r8   r   r   rstrip)r   r   Zbrsrv   Zpresrr   rq   r   r   r   r     s   


zPrettifyTreeprocessor.runN)r   r   r   r   rs   r   r   r   r   r   r     s    r   c                   @   s>   e Zd ZdZedejej	Z
dd Zdd Zdd Zd	S )
r   z Restore escaped chars z	{}(\d+){}c                 C   s   t t|dS )Nr   )chrintr,   )r   r0   r   r   r   	_unescape  s   zUnescapeTreeprocessor._unescapec                 C   s   | j | j|S ro   )REsubrz   )r   r8   r   r   r   r     s   zUnescapeTreeprocessor.unescapec                 C   sh   |  D ]-}|jr|jdks| |j|_|jr| |j|_| D ]\}}||| | q#qdS )z/ Loop over all elements and unescape all text. rq   N)rc   r8   rR   r   r9   itemsset)r   r   rt   keyvaluer   r   r   r     s   zUnescapeTreeprocessor.runN)r   r   r   r   recompilerV   r   ZSTXr!   r{   rz   r   r   r   r   r   r   r     s    r   )r   r   xml.etree.ElementTreere   ElementTreerF   r   r   r   r   Z	Processorr   r
   r   r   r   r   r   r   <module>   s    	  N'