a
    Sicp?                     @   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 )z0 Build the default treeprocessors for Markdown. inline   Zprettify
   unescaper   )r   RegistryregisterInlineProcessorPrettifyTreeprocessorUnescapeTreeprocessor)mdkwargstreeprocessors r   S/var/www/html/django/DPS/env/lib/python3.9/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 )Treeprocessora'  
    Treeprocessors 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   zJ
    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   INLINE_PLACEHOLDER_PREFIX$_InlineProcessor__placeholder_prefixETXZ$_InlineProcessor__placeholder_suffixlenZ$_InlineProcessor__placeholder_length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   INLINE_PLACEHOLDER)r   typeidhashr   r   r   Z__makePlaceholderP   s    
z!InlineProcessor.__makePlaceholderc                 C   s4   | j ||}|r$|d| fS d|d fS d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   sP   t |tjsLd}t| j}||k rL| | j| |||\}}}|s|d7 }q|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   r3   patternIndex
startIndexcountmatchedr   r   r   Z__handleInlinen   s    


zInlineProcessor.__handleInlineTc                 C   sv   |r|j }d|_ n|j}d|_| |||}|sL||urLt||d }nd}|  |D ]}|||d  q\dS )a4  
        Process placeholders in Element.text or Element.tail
        of Elements popped from self.stashed_nodes.

        Keywords arguments:

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

        Returns: None

        Nr   r   )texttail%_InlineProcessor__processPlaceholderslistr4   reverseinsert)r   r7   subnodeisTextr>   ZchildResultposnewChildr   r   r   Z__processElementText   s    z$InlineProcessor.__processElementTextc                    sV   fdd}g d}|rR| | j|}|dkr | ||\}}|| jv r| j|}	|dkrx||| }
||
 t|	s|	gt|	 D ]>}|jr|j r| 	|	|d |j
r|j
 r| 	|| qn||	 |}q|}|	| jdd f n"|t| j }||||  |}q||d }
t|tjrDt|
}
||
 d}qS )aI  
        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   | r|r>d d j r.d d  j | 7  _ q|| d d _ n> s`j rX j | 7  _ q|| _ njrv j| 7  _n| _d S )Nr   )r?   r>   )r>   rE   parentresultr   r   linkText   s    z7InlineProcessor.__processPlaceholders.<locals>.linkTextr   rH   FN )findr#   !_InlineProcessor__findPlaceholderr+   getr   rA   r?   strip$_InlineProcessor__processElementTextr>   appendr)   r%   r   r   r   )r   r3   rJ   rE   rL   ZstrartIndexr4   r.   Z
phEndIndexr7   r>   childr2   r   rI   r   Z__processPlaceholders   sH    




z%InlineProcessor.__processPlaceholdersc                 C   s  t |tj}|jD ] }| | jv r|ddf  S q|rd}| ||D ]>}|||\}}	}
|	du sr|
du r||	d7 }d}qL qqLn"| 
||d }|d| }|s|ddfS |s||}|d}	|	d}
|du r|d|
fS t|s|t |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 d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{}{}{}{}rH   )r   r   r   ZANCESTOR_EXCLUDESlowerr)   ZgetCompiledRegExpfinditerZhandleMatchr2   matchstartr   r>   r   r   rA   rS   tag_InlineProcessor__handleInlinepopr?   _InlineProcessor__stashNoder-   formatr1   groups)r   patternr3   r:   r;   Z	new_styleexcluderW   r7   rX   r2   ZleftDatarT   r8   r   r   r   Z__applyPattern   sd    










zInlineProcessor.__applyPatternc                 C   sH   g }|dur2|dur$| |j  | j|}q|  || dS )zBuild the ancestor list.N)rS   rY   rU   
parent_maprP   rB   extend)r   rJ   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 ]0}|jrt|jtj	s| j
|j  |j}	d|_| | |	|}
|
D ]}|| j|d < q||
7 }|
||
f | j  |jrr| |j}td}d|_| ||d}|jr.|j|_t||d }|  |D ]$}|| j|d < |||d  qLt|rj|| j|< |
|| jdd f qj|D ]2\}}
t|
D ]\}}|d }||| qqq<|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>]      z'InlineProcessor.run.<locals>.<dictcomp>r   dFr   )r+   iterra   r[   r)   !_InlineProcessor__build_ancestorsr>   r   r   r   rS   rY   rU   r@   rZ   r?   etreeElementrA   r4   rB   rC   r%   	enumerate)r   treer)   Ztree_parentsstackZcurrElementrc   ZinsertQueuerT   r>   lstitemr?   ZdumbyZ
tailResultrF   rG   elementiobjr   r   r   r   E  sX    





zInlineProcessor.run)r   )T)T)r   )N)r   r   r   r    r*   r6   rO   r\   rZ   rR   r@   r9   rk   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 linebreaks to the html document. c                 C   s   d}| j |jrn|jdvrn|jr,|j sLt|rL| j |d jrL||_|D ]}| j |jrP| | qP|jr~|j s||_dS )z5 Recursively add linebreaks to ElementTree children. 
)codeprer   N)r   is_block_levelrY   r>   rQ   r%   _prettifyETreer?   )r   elemrt   er   r   r   rz     s    z$PrettifyTreeprocessor._prettifyETreec                 C   s   |  | |d}|D ](}|jr,|j s4d|_qd|j |_q|d}|D ]J}t|rP|d jdkrP|d }t|sP|jdurPt|j	 d |_qPdS )z, Add linebreaks to ElementTree root object. brrv   z
%srx   r   rw   N)
rz   rj   r?   rQ   r%   rY   r>   r   r   rstrip)r   r   Zbrsr}   Zpresrx   rw   r   r   r   r     s    


zPrettifyTreeprocessor.runN)r   r   r   r    rz   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intr1   )r   r5   r   r   r   	_unescape  s    zUnescapeTreeprocessor._unescapec                 C   s   | j | j|S )N)REsubr   )r   r>   r   r   r   r     s    zUnescapeTreeprocessor.unescapec                 C   sh   |  D ]Z}|jr*|jdks*| |j|_|jr>| |j|_| D ]\}}||| | qFqdS )z/ Loop over all elements and unescape all text. rw   N)rj   r>   rY   r   r?   itemsset)r   r   r{   keyvaluer   r   r   r     s    zUnescapeTreeprocessor.runN)r   r   r   r    recompiler]   r   STXr$   r   r   r   r   r   r   r   r   r     s
   r   )r    r   xml.etree.ElementTreerl   ElementTreerM   r   r   r   r   	Processorr   r   r   r   r   r   r   r   <module>   s   	  N'