o
    ¼>h…i  ã                   @  s@  d Z ddlmZ ddlZddlZddlm  mZ ddlm	Z	m
Z
 ddlmZ ddlmZ e	r5ddlmZ e d	¡Zd*dd„ZG dd„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d d!„ d!eƒZG d"d#„ d#eƒZG d$d%„ d%eƒZG d&d'„ d'eƒZG d(d)„ d)eƒZdS )+a;  
A block processor parses blocks of text and adds new elements to the ElementTree. Blocks of text,
separated from other text by blank lines, may have a different syntax and produce a differently
structured tree than other Markdown. Block processors excel at handling code formatting, equation
layouts, tables, etc.
é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyé   )Úutil)ÚBlockParser)ÚMarkdownÚMARKDOWNÚmdr   Úkwargsr   Úreturnr   c                 K  sè   t | ƒ}|j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒd	d
¡ |j t|ƒdd¡ |j t	|ƒdd¡ |j t
|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |S )z2 Build the default block parser used by Markdown. Úemptyéd   ÚindentéZ   ÚcodeéP   Ú
hashheaderéF   Úsetextheaderé<   Úhré2   Úolisté(   Úulisté   Úquoteé   Ú	referenceé   Ú	paragraphé
   )r   ÚblockprocessorsÚregisterÚEmptyBlockProcessorÚListIndentProcessorÚCodeBlockProcessorÚHashHeaderProcessorÚSetextHeaderProcessorÚHRProcessorÚOListProcessorÚUListProcessorÚBlockQuoteProcessorÚReferenceProcessorÚParagraphProcessor)r
   r   Úparser© r1   úx/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/markdown/blockprocessors.pyÚbuild_block_parser*   s   r3   c                   @  sP   e Zd ZdZd"dd„Zd#d
d„Zd$d%dd„Zd&d'dd„Zd(dd„Zd)d d!„Z	dS )*ÚBlockProcessoraY   Base class for block processors.

    Each subclass will provide the methods below to work with the source and
    tree. Each processor will need to define it's own `test` and `run`
    methods. The `test` method should return True or False, to indicate
    whether the current block should be processed by this processor. If the
    test passes, the parser will call the processors `run` method.

    Attributes:
        BlockProcessor.parser (BlockParser): The `BlockParser` instance this is attached to.
        BlockProcessor.tab_length (int): The tab length set on the `Markdown` instance.

    r0   r   c                 C  s   || _ |jj| _d S ©N)r0   r
   Ú
tab_length©Úselfr0   r1   r1   r2   Ú__init__J   s   zBlockProcessor.__init__Úparentúetree.Elementr   úetree.Element | Nonec                 C  s   t |ƒr|d S dS )z. Return the last child of an `etree` element. éÿÿÿÿN)Úlen)r8   r:   r1   r1   r2   Ú	lastChildN   s   zBlockProcessor.lastChildNÚtextÚstrÚlengthú
int | Noneútuple[str, str]c                 C  s|   |du r| j }g }| d¡}|D ]}| d| ¡r#| ||d… ¡ q| ¡ s-| d¡ q d |¡d |t|ƒd… ¡fS )z= Remove a tab from the front of each line of the given text. NÚ
ú Ú )r6   ÚsplitÚ
startswithÚappendÚstripÚjoinr>   )r8   r@   rB   ÚnewtextÚlinesÚliner1   r1   r2   ÚdetabU   s   
 zBlockProcessor.detabr   ÚlevelÚintc                 C  sX   |  d¡}tt|ƒƒD ]}||  d| j | ¡r&|| | j| d… ||< qd |¡S )z? Remove a tab from front of lines but allowing dedented lines. rE   rF   N)rH   Úranger>   rI   r6   rL   )r8   r@   rQ   rN   Úir1   r1   r2   Ú
looseDetabd   s   
€
zBlockProcessor.looseDetabÚblockÚboolc                 C  ó   dS )aZ   Test for block type. Must be overridden by subclasses.

        As the parser loops through processors, it will call the `test`
        method on each to determine if the given block of text is of that
        type. This method must return a boolean `True` or `False`. The
        actual method of testing is left to the needs of that particular
        block type. It could be as simple as `block.startswith(some_string)`
        or a complex regular expression. As the block type may be different
        depending on the parent of the block (i.e. inside a list), the parent
        `etree` element is also provided and may be used as part of the test.

        Keyword arguments:
            parent: An `etree` element which will be the parent of the block.
            block: A block of text from the source which has been split at blank lines.
        Nr1   ©r8   r:   rV   r1   r1   r2   Útestl   s   zBlockProcessor.testÚblocksú	list[str]úbool | Nonec                 C  rX   )a3   Run processor. Must be overridden by subclasses.

        When the parser determines the appropriate type of a block, the parser
        will call the corresponding processor's `run` method. This method
        should parse the individual lines of the block and append them to
        the `etree`.

        Note that both the `parent` and `etree` keywords are pointers
        to instances of the objects which should be edited in place. Each
        processor must make changes to the existing objects as there is no
        mechanism to return new/different objects to replace them.

        This means that this method should be adding `SubElements` or adding text
        to the parent, and should remove (`pop`) or add (`insert`) items to
        the list of blocks.

        If `False` is returned, this will have the same effect as returning `False`
        from the `test` method.

        Keyword arguments:
            parent: An `etree` element which is the parent of the current block.
            blocks: A list of all remaining blocks of the document.
        Nr1   )r8   r:   r[   r1   r1   r2   Úrun~   s   zBlockProcessor.run©r0   r   )r:   r;   r   r<   r5   )r@   rA   rB   rC   r   rD   )r   )r@   rA   rQ   rR   r   rA   ©r:   r;   rV   rA   r   rW   )r:   r;   r[   r\   r   r]   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   r?   rP   rU   rZ   r^   r1   r1   r1   r2   r4   ;   s    


r4   c                      sZ   e Zd ZdZdgZ	 ddgZ	 ‡ fdd„Zddd„Zddd„Zddd„Z	ddd„Z
‡  ZS )r&   z‚ Process children of list items.

    Example

        * a list item
            process this part

            or this part

    ÚliÚulÚolc                   s"   t ƒ j|Ž  t d| j ¡| _d S )Nz^(([ ]{%s})+))Úsuperr9   ÚreÚcompiler6   Ú	INDENT_RE)r8   Úargs©Ú	__class__r1   r2   r9   ª   s   zListIndentProcessor.__init__r:   r;   rV   rA   r   rW   c                 C  sP   |  d| j ¡o'| jj d¡ o'|j| jv p't|ƒo'|d d uo'|d j| jv S )NrF   Údetabbedr=   )	rI   r6   r0   ÚstateÚisstateÚtagÚ
ITEM_TYPESr>   Ú
LIST_TYPESrY   r1   r1   r2   rZ   ®   s   ÿüzListIndentProcessor.testr[   r\   ÚNonec                 C  s   |  d¡}|  ||¡\}}|  ||¡}| jj d¡ |j| jv r@t|ƒr7|d j| j	v r7| j 
|d |g¡ nQ| j 
||g¡ nH|j| jv rO| j 
||g¡ n9t|ƒr‚|d j| jv r‚|d jrxt d¡}|d j|_d|d _|d  d|¡ | j |d |¡ n|  ||¡ | jj ¡  d S )Nr   ro   r=   ÚprG   )ÚpopÚ	get_levelrU   r0   rp   Úsetrr   rs   r>   rt   ÚparseBlocksr@   ÚetreeÚElementÚinsertÚ
parseChunkÚcreate_itemÚreset)r8   r:   r[   rV   rQ   Úsiblingrv   r1   r1   r2   r^   µ   s&   



zListIndentProcessor.runc                 C  s    t  |d¡}| j ||g¡ dS )z> Create a new `li` and parse the block with it as the parent. re   N)r{   Ú
SubElementr0   rz   )r8   r:   rV   re   r1   r1   r2   r   Ù   s   zListIndentProcessor.create_itemútuple[int, etree.Element]c                 C  s¨   | j  |¡}|rt| d¡ƒ| j }nd}| jj d¡rd}nd}||krP|  |¡}|durG|j	| j
v s:|j	| jv rG|j	| j
v rD|d7 }|}n	 ||fS ||ks%||fS )z/ Get level of indentation based on list level. r   r   ÚlistN)rk   Úmatchr>   Úgroupr6   r0   rp   rq   r?   rr   rt   rs   )r8   r:   rV   ÚmÚindent_levelrQ   Úchildr1   r1   r2   rx   Þ   s$   
õzListIndentProcessor.get_levelr`   ©r:   r;   r[   r\   r   ru   )r:   r;   rV   rA   r   ru   )r:   r;   rV   rA   r   rƒ   )ra   rb   rc   rd   rs   rt   r9   rZ   r^   r   rx   Ú__classcell__r1   r1   rm   r2   r&   ™   s    


$r&   c                   @  ó$   e Zd ZdZddd	„Zddd„ZdS )r'   z Process code blocks. r:   r;   rV   rA   r   rW   c                 C  s   |  d| j ¡S )NrF   )rI   r6   rY   r1   r1   r2   rZ   þ   ó   zCodeBlockProcessor.testr[   r\   ru   c              	   C  sÐ   |   |¡}| d¡}d}|d ur<|jdkr<t|ƒr<|d jdkr<|d }|  |¡\}}t d |jt 	| 
¡ ¡¡¡|_n t |d¡}t |d¡}|  |¡\}}t dt 	| 
¡ ¡ ¡|_|rf| d|¡ d S d S )Nr   rG   Úprer   z{}
{}
z%s
)r?   rw   rr   r>   rP   r   ÚAtomicStringÚformatr@   Úcode_escapeÚrstripr{   r‚   r}   )r8   r:   r[   r   rV   ÚtheRestr   rŽ   r1   r1   r2   r^     s&   

ÿÿüzCodeBlockProcessor.runNr`   rŠ   ©ra   rb   rc   rd   rZ   r^   r1   r1   r1   r2   r'   û   ó    
r'   c                   @  s8   e Zd ZdZe d¡Zdd	d
„Zddd„Zddd„Z	dS )r-   z Process blockquotes. z(^|\n)[ ]{0,3}>[ ]?(.*)r:   r;   rV   rA   r   rW   c                 C  s   t | j |¡ƒot ¡  S r5   )rW   ÚREÚsearchr   Únearing_recursion_limitrY   r1   r1   r2   rZ   !  s   zBlockQuoteProcessor.testr[   r\   ru   c                   s¾   |  d¡}ˆ j |¡}|r2|d | ¡ … }ˆ j ||g¡ d ‡ fdd„|| ¡ d …  d¡D ƒ¡}ˆ  |¡}|d urC|j	dkrC|}nt
 |d¡}ˆ jj d¡ ˆ j ||¡ ˆ jj ¡  d S )Nr   rE   c                   s   g | ]}ˆ   |¡‘qS r1   )Úclean)Ú.0rO   ©r8   r1   r2   Ú
<listcomp>-  s    z+BlockQuoteProcessor.run.<locals>.<listcomp>Ú
blockquote)rw   r–   r—   Ústartr0   rz   rL   rH   r?   rr   r{   r‚   rp   ry   r~   r€   )r8   r:   r[   rV   r‡   Úbeforer   r   r1   r›   r2   r^   $  s   
"ÿ
zBlockQuoteProcessor.runrO   c                 C  s.   | j  |¡}| ¡ dkrdS |r| d¡S |S )z& Remove `>` from beginning of a line. ú>rG   é   )r–   r…   rK   r†   )r8   rO   r‡   r1   r1   r2   r™   <  s   
zBlockQuoteProcessor.cleanNr`   rŠ   )rO   rA   r   rA   )
ra   rb   rc   rd   ri   rj   r–   rZ   r^   r™   r1   r1   r1   r2   r-     s    


r-   c                      s~   e Zd ZU dZdZded< 	 dZded< 	 dZded	< 	 dd
gZded< 	 d‡ fdd„Z	ddd„Z
ddd„Zd dd„Z‡  ZS )!r+   z Process ordered list blocks. rg   rA   ÚTAGÚ1Ú
STARTSWITHTrW   ÚLAZY_OLrf   r\   ÚSIBLING_TAGSr0   r   c                   s\   t ƒ  |¡ t d| jd  ¡| _t d| jd  ¡| _t d| j| jd d f ¡| _d S )Nz^[ ]{0,%d}\d+\.[ ]+(.*)r   z!^[ ]{0,%d}((\d+\.)|[*+-])[ ]+(.*)z ^[ ]{%d,%d}((\d+\.)|[*+-])[ ]+.*r¡   )rh   r9   ri   rj   r6   r–   ÚCHILD_RErk   r7   rm   r1   r2   r9   Y  s   ÿÿzOListProcessor.__init__r:   r;   rV   r   c                 C  ó   t | j |¡ƒS r5   ©rW   r–   r…   rY   r1   r1   r2   rZ   d  r   zOListProcessor.testr[   ru   c                 C  s  |   | d¡¡}|  |¡}|d urv|j| jv rv|}|d jr6t d¡}|d j|_d|d _|d  d|¡ |  |d ¡}|d urU|j	rUt 
|d d¡}|j	 ¡ |_d|_	t 
|d¡}| jj d¡ | d¡}	| j ||	g¡ | jj ¡  n|jdv r~|}nt 
|| j¡}| js“| jdkr“| j|jd	< | jj d
¡ |D ]#}
|
 d| j ¡r±| j |d |
g¡ qœt 
|d¡}| j ||
g¡ qœ| jj ¡  d S )Nr   r=   rv   rG   re   Ú	looselist)rg   rf   r£   rž   r„   rF   )Ú	get_itemsrw   r?   rr   r¦   r@   r{   r|   r}   Útailr‚   Úlstripr0   rp   ry   rz   r€   r¢   r¥   r¤   ÚattribrI   r6   )r8   r:   r[   Úitemsr   Úlstrv   Úlchre   Ú	firstitemÚitemr1   r1   r2   r^   g  s>   





zOListProcessor.runc                 C  s¾   g }|  d¡D ]U}| j |¡}|r1|s(| jdkr(t d¡}| | d¡¡ ¡ | _| | d¡¡ q| j	 |¡rR|d  
d| j ¡rLd |d |¡|d< q| |¡ qd |d |¡|d< q|S )	z  Break a block into list items. rE   rg   z(\d+)r   é   r=   rF   ú{}
{})rH   r§   r…   r¢   ri   rj   r†   r¤   rJ   rk   rI   r6   r   )r8   rV   r¯   rO   r‡   Ú
INTEGER_REr1   r1   r2   r«   ¢  s   
zOListProcessor.get_itemsr_   r`   rŠ   )rV   rA   r   r\   )ra   rb   rc   rd   r¢   Ú__annotations__r¤   r¥   r¦   r9   rZ   r^   r«   r‹   r1   r1   rm   r2   r+   G  s   
 

;r+   c                      s2   e Zd ZU dZdZded< 	 d	‡ fdd„Z‡  ZS )
r,   z  Process unordered list blocks. rf   rA   r¢   r0   r   c                   s&   t ƒ  |¡ t d| jd  ¡| _d S )Nz^[ ]{0,%d}[*+-][ ]+(.*)r   )rh   r9   ri   rj   r6   r–   r7   rm   r1   r2   r9   Ã  s   zUListProcessor.__init__r_   )ra   rb   rc   rd   r¢   r·   r9   r‹   r1   r1   rm   r2   r,   ½  s
   
 r,   c                   @  s.   e Zd ZdZe d¡Zdd	d
„Zddd„ZdS )r(   z Process Hash Headers. z>(?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\])*?)#*(?:\n|$)r:   r;   rV   rA   r   rW   c                 C  r¨   r5   )rW   r–   r—   rY   r1   r1   r2   rZ   Ï  r   zHashHeaderProcessor.testr[   r\   ru   c                 C  sº   |  d¡}| j |¡}|rT|d | ¡ … }|| ¡ d … }|r'| j ||g¡ t |dt	| 
d¡ƒ ¡}| 
d¡ ¡ |_|rR| jj d¡rJ|  |¡}| d|¡ d S d S t d| ¡ d S )Nr   úh%drQ   Úheaderrª   zWe've got a problem header: %r)rw   r–   r—   rž   Úendr0   rz   r{   r‚   r>   r†   rK   r@   rp   rq   rU   r}   ÚloggerÚwarn)r8   r:   r[   rV   r‡   rŸ   ÚafterÚhr1   r1   r2   r^   Ò  s   

ù
zHashHeaderProcessor.runNr`   rŠ   )	ra   rb   rc   rd   ri   rj   r–   rZ   r^   r1   r1   r1   r2   r(   É  s
    

r(   c                   @  s2   e Zd ZdZe dej¡Zdd	d
„Zddd„Z	dS )r)   z Process Setext-style Headers. z^.*?\n[=-]+[ ]*(\n|$)r:   r;   rV   rA   r   rW   c                 C  r¨   r5   r©   rY   r1   r1   r2   rZ   ó  r   zSetextHeaderProcessor.testr[   r\   ru   c                 C  st   |  d¡ d¡}|d  d¡rd}nd}t |d| ¡}|d  ¡ |_t|ƒdkr8| dd 	|dd … ¡¡ d S d S )Nr   rE   r   ú=r¡   r¸   )
rw   rH   rI   r{   r‚   rK   r@   r>   r}   rL   )r8   r:   r[   rN   rQ   r¾   r1   r1   r2   r^   ö  s   þzSetextHeaderProcessor.runNr`   rŠ   ©
ra   rb   rc   rd   ri   rj   Ú	MULTILINEr–   rZ   r^   r1   r1   r1   r2   r)   í  s
    
r)   c                   @  s6   e Zd ZdZdZe eej¡Zdd	d
„Z	ddd„Z
dS )r*   z Process Horizontal Rules. zf^[ ]{0,3}(?=(?P<atomicgroup>(-+[ ]{0,2}){3,}|(_+[ ]{0,2}){3,}|(\*+[ ]{0,2}){3,}))(?P=atomicgroup)[ ]*$r:   r;   rV   rA   r   rW   c                 C  s   | j  |¡}|r|| _dS dS )NTF)Ú	SEARCH_REr—   r…   )r8   r:   rV   r‡   r1   r1   r2   rZ     s
   zHRProcessor.testr[   r\   ru   c                 C  st   |  d¡}| j}|d | ¡ …  d¡}|r| j ||g¡ t |d¡ || ¡ d …  	d¡}|r8| 
d|¡ d S d S )Nr   rE   r   )rw   r…   rž   r’   r0   rz   r{   r‚   rº   r­   r}   )r8   r:   r[   rV   r…   ÚprelinesÚ	postlinesr1   r1   r2   r^     s   
þzHRProcessor.runNr`   rŠ   )ra   rb   rc   rd   r–   ri   rj   rÁ   rÂ   rZ   r^   r1   r1   r1   r2   r*     s    
r*   c                   @  rŒ   )r%   z< Process blocks that are empty or start with an empty line. r:   r;   rV   rA   r   rW   c                 C  s   | p|  d¡S )NrE   )rI   rY   r1   r1   r2   rZ   )  r   zEmptyBlockProcessor.testr[   r\   ru   c                 C  s–   |  d¡}d}|rd}|dd … }|r| d|¡ |  |¡}|d urC|jdkrEt|ƒrG|d jdkrIt d |d j|¡¡|d _d S d S d S d S d S )Nr   z

rE   r   rŽ   r   z{}{})	rw   r}   r?   rr   r>   r   r   r   r@   )r8   r:   r[   rV   Úfillerr“   r   r1   r1   r2   r^   ,  s"   

ÿÿýzEmptyBlockProcessor.runNr`   rŠ   r”   r1   r1   r1   r2   r%   &  r•   r%   c                   @  s2   e Zd ZdZe dej¡Zdd	d
„Zddd„Z	dS )r.   z Process link references. z\^[ ]{0,3}\[([^\[\]]*)\]:[ ]*\n?[ ]*([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$r:   r;   rV   rA   r   rW   c                 C  rX   ©NTr1   rY   r1   r1   r2   rZ   G  ó   zReferenceProcessor.testr[   r\   c                 C  sà   |  d¡}| j |¡}|rh| d¡ ¡  ¡ }| d¡ d¡ d¡}| d¡p*| d¡}||f| jj	j
|< || ¡ d …  ¡ rM| d|| ¡ d …  d¡¡ |d | ¡ …  ¡ rf| d|d | ¡ …  d¡¡ d	S | d|¡ d
S )Nr   r   r¡   ú<r    é   é   rE   TF)rw   r–   r—   r†   rK   Úlowerr­   r’   r0   r
   Ú
referencesrº   r}   rž   )r8   r:   r[   rV   r‡   ÚidÚlinkÚtitler1   r1   r2   r^   J  s   
zReferenceProcessor.runNr`   )r:   r;   r[   r\   r   rW   rÀ   r1   r1   r1   r2   r.   A  s    ÿ
r.   c                   @  rŒ   )r/   z Process Paragraph blocks. r:   r;   rV   rA   r   rW   c                 C  rX   rÆ   r1   rY   r1   r1   r2   rZ   a  rÇ   zParagraphProcessor.testr[   r\   ru   c                 C  s    |  d¡}| ¡ rN| jj d¡rA|  |¡}|d ur-|jr&d |j|¡|_d S d| |_d S |jr:d |j|¡|_d S | 	¡ |_d S t
 |d¡}| 	¡ |_d S d S )Nr   r„   rµ   z
%srv   )rw   rK   r0   rp   rq   r?   r¬   r   r@   r­   r{   r‚   )r8   r:   r[   rV   r   rv   r1   r1   r2   r^   d  s   

	åzParagraphProcessor.runNr`   rŠ   r”   r1   r1   r1   r2   r/   ^  r•   r/   )r
   r   r   r   r   r   ) rd   Ú
__future__r   Úloggingri   Úxml.etree.ElementTreer{   ÚElementTreeÚtypingr   r   rG   r   Úblockparserr   Úmarkdownr   Ú	getLoggerr»   r3   r4   r&   r'   r-   r+   r,   r(   r)   r*   r%   r.   r/   r1   r1   r1   r2   Ú<module>   s0   

^b!+v$"