
    !ShD                        d Z ddlmZ ddlZddlmc mZ ddlmZm	Z	 ddl
mZ ddl
mZ erddlmZ ddZddZ G d dej                  Z G d de          Z G d de          Z G d de          ZdS )z
Tree processors manipulate the tree created by block processors. They can even create an entirely
new `ElementTree` object. This is an excellent place for creating summaries, adding collected
references, or last minute adjustments.

    )annotationsN)TYPE_CHECKINGAny   )util)inlinepatterns)Markdownmdr	   kwargsr   returnutil.Registry[Treeprocessor]c                   t          j                    }|                    t          |           dd           |                    t	          |           dd           |                    t          |           dd           |S )z3 Build the default  `treeprocessors` for Markdown. inline   prettify
   unescaper   )r   RegistryregisterInlineProcessorPrettifyTreeprocessorUnescapeTreeprocessor)r
   r   treeprocessorss      ]/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/markdown/treeprocessors.pybuild_treeprocessorsr   '   su    ]__NOB//2>>>1"55z2FFF1"55z1EEE    sobjectboolc                d    t          | t          j                  st          | t                    S dS )z_ Return `True` if object is a string but not an  [`AtomicString`][markdown.util.AtomicString]. F)
isinstancer   AtomicStringstr)r   s    r   isStringr$   0   s,    a*++ "!S!!!5r   c                      e Zd ZdZddZdS )	Treeprocessora  
    `Treeprocessor`s are run on the `ElementTree` object before serialization.

    Each `Treeprocessor` implements a `run` method that takes a pointer to an
    `Element` and modifies it as necessary.

    `Treeprocessors` must extend `markdown.Treeprocessor`.

    rootetree.Elementr   etree.Element | Nonec                    dS )a  
        Subclasses of `Treeprocessor` should implement a `run` method, which
        takes a root `Element`. This method can return another `Element`
        object, and the existing root `Element` will be replaced, or it can
        modify the current tree and return `None`.
        N )selfr'   s     r   runzTreeprocessor.runA   s	     	r   N)r'   r(   r   r)   )__name__
__module____qualname____doc__r-   r+   r   r   r&   r&   7   s2              r   r&   c                  p    e Zd ZdZd.dZd/d	Zd0dZd1dZd2d3dZd4d5dZ		 d4d6dZ
	 d2d7d$Zd8d(Zd9d:d-Zd)S );r   zL
    A `Treeprocessor` that traverses a tree, applying inline patterns.
    r
   r	   c                   t           j        | _        t           j        | _        dt          | j                  z   t          | j                  z   | _        t           j        | _        || _	        |j
        | _
        g | _        d S )N   )r   INLINE_PLACEHOLDER_PREFIX$_InlineProcessor__placeholder_prefixETX$_InlineProcessor__placeholder_suffixlen$_InlineProcessor__placeholder_lengthINLINE_PLACEHOLDER_RE _InlineProcessor__placeholder_rer
   inlinePatterns	ancestors)r,   r
   s     r   __init__zInlineProcessor.__init__P   sk    $($B!$(H!$%D,E(F(F$F(+D,E(F(F%G! $ : /$&r   typer#   r   tuple[str, str]c                V    dt          | j                  z  }t          j        |z  }||fS )z Generate a placeholder z%04d)r9   stashed_nodesr   INLINE_PLACEHOLDER)r,   r@   idhashs       r   __makePlaceholderz!InlineProcessor.__makePlaceholderZ   s-    c$,---&+Rxr   dataindexinttuple[str | None, int]c                    | j                             ||          }|r)|                    d          |                                fS d|dz   fS )a  
        Extract id from data string, start from index.

        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,   rH   rI   ms       r   __findPlaceholderz!InlineProcessor.__findPlaceholder`   sO     !((u55 	#771::quuww&&?"r   nodeetree.Element | strc                J    |                      |          \  }}|| j        |<   |S )z Add node to stash. )!_InlineProcessor__makePlaceholderrC   )r,   rR   r@   placeholderrE   s        r   __stashNodezInlineProcessor.__stashNoder   s,    0066R!%2r   r   patternIndexc                    t          |t          j                  sPd}t          | j                  }||k     r4|                     | j        |         |||          \  }}}|s|dz  }||k     4|S )a  
        Process string with inline patterns and replace it with placeholders.

        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"   r9   r=   _InlineProcessor__applyPattern)r,   rH   rX   
startIndexcountmatcheds         r   __handleInlinezInlineProcessor.__handleInlinex   s     $ 122 	&J+,,E&&,0,?,?'5t\:- -)gz  & A%L && r   Tr(   subnodeisTextr   Nonec                8   |r|j         }d|_         n|j        }d|_        |                     |||          }|s*||ur&t          |                              |          dz   }nd}|                                 |D ]}|                    ||d                    dS )a'  
        Process placeholders in `Element.text` or `Element.tail`
        of Elements popped from `self.stashed_nodes`.

        Arguments:
            node: Parent node.
            subnode: Processing node.
            isText: Boolean variable, True - it's text, False - it's a tail.

        Nr   r   )texttail%_InlineProcessor__processPlaceholderslistrI   reverseinsert)r,   rR   r_   r`   rc   childResultposnewChilds           r   __processElementTextz$InlineProcessor.__processElementText   s      	 <DGLL<DGL00wGG 	$g--t**""7++a/CCC# 	* 	*HKKXa[))))	* 	*r   
str | Noneparent%list[tuple[etree.Element, list[str]]]c                   dfd}g d}|r|                     | j        |          }|dk    rT|                     ||          \  }}|| j        v r| j                            |          }	|dk    r|||         }
 ||
           t          |	t                    s|	gt          |	          z   D ]o}|j        r0|j        	                                r| 
                    |	|d           |j        r/|j        	                                r| 
                    ||           pn ||	           |}%|}                    |	| j        d	d	         f           nr|t          | j                  z   } ||||                    |}nE||d	         }
t          |t          j                  rt          j        |
          }
 ||
           d
}|S )a  
        Process string with placeholders and generate `ElementTree` tree.

        Arguments:
            data: String with placeholders instead of `ElementTree` elements.
            parent: Element, which contains processing inline data.
            isText: Boolean variable, True - it's text, False - it's a tail.

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

        rc   rm   r   ra   c                (   | rrFd         d         j         rd         d         xj         | z  c_         d S | d         d         _         d S s"j         rxj         | z  c_         d S | _         d S j        rxj        | z  c_        d S | _        d S d S )Nr   )rd   rc   )rc   r`   rn   results    r   linkTextz7InlineProcessor.__processPlaceholders.<locals>.linkText   s     + +bz!}) 2r
1**d2****-1r
1*** 	+{ +t+&*{ +t+&*+ +r   r   rr   FN )rc   rm   r   ra   )findr6   !_InlineProcessor__findPlaceholderrC   getr!   r#   rf   rd   strip$_InlineProcessor__processElementTextrc   appendr>   r9   r   r"   )r,   rH   rn   r`   rt   strartIndexrI   rE   
phEndIndexrR   rc   childrO   rs   s     ``         @r   __processPlaceholdersz%InlineProcessor.__processPlaceholders   sB   $	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+"  (	IId7EEE{{!%!7!7e!D!DJ+++-11"55Dqyy#K$56 %dC00 !&*Vd4jj%8 L LE$z &#(:#3#3#5#5 !&$($=$=(,eU%& %& %&  %z L#(:#3#3#5#5 !L$($=$=eU$K$K$KL !&0 ",KMM4):";<<<<  #d&?"@"@@CHT+c/2333"%KKKLL)dD$566 3,T22DQ  (	T r   patterninlinepatterns.Patternr[   tuple[str, bool, int]c                   t          |t          j                  }|j        D ]$}|                                | j        v r|ddfc S %|rgd}|                                                    ||          D ];}|                    ||          \  }}	}
|	|
||	                    d          z  }d}; n9|                                
                    ||d                   }|d|         }|s|ddfS |s?|                    |          }|                    d          }	|	                    d          }
||d|
fS t          |t                    st          |j        t          j                  s|gt!          |          z   D ]}t#          |          s|j        rm| j                            |j                                                   |                     |j        |dz             |_        | j                                         |j        r |                     |j        |          |_        |                     ||                                          }|r*d                    |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`.

        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{}{}{}{}rr   )r!   r   r   ANCESTOR_EXCLUDESlowerr>   getCompiledRegExpfinditerhandleMatchrO   matchstartr#   rc   r   r"   rf   r$   r{   tag_InlineProcessor__handleInlinepoprd   _InlineProcessor__stashNoder@   formatrN   groups)r,   r   rH   rX   r[   	new_styleexcluder   rR   r   rO   leftDatar~   rV   s                 r   __applyPatternzInlineProcessor.__applyPattern   s   * w(FGG	0 	& 	&G}}$.00UA~%%% 1  	)E !2244==dJOO  #*#6#6ud#C#C eS=CK%))A,,.J E--//55d:;;6GHHEKZK(H 	">! 	&&u--DKKNNE))A,,C<s?"$$$ 	di):;; "Vd4jj0  E#D>> 
 : 1 N11%)//2C2CDDD)-)<)< %
L1,<* *EJ !N..000 : )-)<)< %
L* *EJ &&tW\\^^<< 	O??4<#.STT
< <=A1E E $$X%*[[^^%0%,,..2DF FGKQO Or   r)   parents	list[str]c                    g }|J|,|                     |j                                                   | j                            |          }|J|                                 |                    |           dS )zBuild the ancestor list.N)r{   r   r   
parent_maprx   rg   extend)r,   rn   r   r>   s       r   __build_ancestorsz!InlineProcessor.__build_ancestorsM  s~    	 !  !1!1!3!3444_((00F   	y!!!!!r   Ntreer>   list[str] | Nonec                R   i | _         |g n	|dd         }d |                                D             | _        ||fg}|rf|                    d          \  }}|| _        |                     || j                   g }|D ]}|j        rt          |j        t          j	                  s| j        
                    |j                                                   |j        }	d|_        |                     |                     |	          |          }
|
D ]}|| j        |d         <   ||
z  }|
                    ||
f           | j                                         |j        r|                     |j                  }t!          j        d          }d|_        |                     ||d          }|j        r|j        |_        t%          |                              |          dz   }|                                 |D ].}|| j        |d         <   |                    ||d                    /t-          |          r.|| j        |<   |
                    || j        dd         f           |D ]8\  }}
t/          |
          D ]#\  }}|d         }|                    ||           $9|f|S )a  Apply inline patterns to a parsed Markdown tree.

        Iterate over `Element`, find elements with inline tag, apply inline
        patterns and append newly created Elements to tree.  To avoid further
        processing of string with inline patterns, instead of normal string,
        use subclass [`AtomicString`][markdown.util.AtomicString]:

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

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

        Returns:
            An element tree object with applied inline patterns.

        Nc                    i | ]
}|D ]}||S r+   r+   ).0pcs      r   
<dictcomp>z'InlineProcessor.run.<locals>.<dictcomp>o  s'    @@@Aa@@1a@@@@r   r   dFr   )rC   iterr   r   r>   !_InlineProcessor__build_ancestorsrc   r!   r   r"   r{   r   r   re   r   rd   etreeElementrf   rI   rg   rh   r9   	enumerate)r,   r   r>   tree_parentsstackcurrElementr   insertQueuer~   rc   lstitemrd   dumby
tailResultrj   rk   elementiobjs                       r   r-   zInlineProcessor.runW  s   $ >@ '.rrIaaaL@@@@@%& )	0#(99Q<< K$DN"";???K$ = =: )jJ 1' ' ) N))%)//*;*;<<< :D!%EJ44++D115 C !$ 9 938Q00SLE&&s|444N&&(((: =..uz::D!M#..E!%EJ!%!;!;D%!O!OJz 0%*Z
{++11%881<C&&((($. = =7B4#**3<<<<u:: =-8DOE*LL%):!;<<< + 0 0'nn 0 0FAs"1vHNN1h////0O  )	0T r   )r
   r	   )r@   r#   r   rA   )rH   r#   rI   rJ   r   rK   )rR   rS   r@   r#   r   r#   )r   )rH   r#   rX   rJ   r   r#   )T)rR   r(   r_   r(   r`   r   r   ra   )rH   rm   rn   r(   r`   r   r   ro   )
r   r   rH   r#   rX   rJ   r[   rJ   r   r   )rn   r)   r   r   r   ra   N)r   r(   r>   r   r   r(   )r.   r/   r0   r1   r?   rU   rw   r   r   rz   re   rZ   r   r-   r+   r   r   r   r   K   s"        ' ' ' '   # # # #$       .* * * * *B 	O O O O Ol NO NO NO NO NO`" " " "E E E E E E Er   r   c                  "    e Zd ZdZd
dZddZd	S )r   z' Add line breaks to the html document. elemr(   r   ra   c                   d}| j                             |j                  r|j        dvr|j        r|j                                        s;t          |          r,| j                             |d         j                  r||_        |D ]6}| j                             |j                  r|                     |           7|j        r|j                                        s	||_        dS dS )z8 Recursively add line breaks to `ElementTree` children. 
)codeprer   N)r
   is_block_levelr   rc   ry   r9   _prettifyETreerd   )r,   r   r   es       r   r   z$PrettifyTreeprocessor._prettifyETree  s     7!!$(++ 	+0O0OI TY__%6%6 D		&*g&<&<T!W[&I&I	 + +7))!%00 +''***y 		 1 1 	DIII	 	r   r'   c                   |                      |           |                    d          }|D ]9}|j        r|j                                        sd|_        *d|j        z  |_        :|                    d          }|D ]s}t	          |          rb|d         j        dk    rQ|d         }t	          |          s:|j        3t          j        |j        	                                dz             |_        tdS )z7 Add line breaks to `Element` object and its children. brr   z
%sr   r   r   N)
r   r   rd   ry   r9   r   rc   r   r"   rstrip)r,   r'   brsr   presr   r   s          r   r-   zPrettifyTreeprocessor.run  s     	D!!! iioo 	+ 	+B7 +"'--// + 27*yy 	M 	MC3xx MCFJ&001v4yy MTY%: $ 1$)2B2B2D2Dt2K L LDI	M 	Mr   N)r   r(   r   ra   r'   r(   r   ra   )r.   r/   r0   r1   r   r-   r+   r   r   r   r     sH        11   M M M M M Mr   r   c                      e Zd ZdZ ej        d                    ej        ej	                            Z
ddZdd	ZddZdS )r   z Restore escaped chars z	{}(\d+){}rP   re.Match[str]r   r#   c                `    t          t          |                    d                              S )Nr   )chrrJ   rN   )r,   rP   s     r   	_unescapezUnescapeTreeprocessor._unescape  s     3qwwqzz??###r   rc   c                B    | j                             | j        |          S r   )REsubr   )r,   rc   s     r   r   zUnescapeTreeprocessor.unescape  s    w{{4>4000r   r'   r(   ra   c                h   |                                 D ]}|j        r*|j        dk    s|                     |j                  |_        |j        r|                     |j                  |_        |                                D ].\  }}|                    ||                     |                     /dS )z/ Loop over all elements and unescape all text. r   N)r   rc   r   r   rd   itemsset)r,   r'   r   keyvalues        r   r-   zUnescapeTreeprocessor.run  s    IIKK 		4 		4Dy 5V!3!3 MM$)44	y 5 MM$)44	"jjll 4 4
UdmmE2233334		4 		4r   N)rP   r   r   r#   )rc   r#   r   r#   r   )r.   r/   r0   r1   recompiler   r   STXr7   r   r   r   r-   r+   r   r   r   r     s{        !!	L''$(;;	<	<B$ $ $ $1 1 1 14 4 4 4 4 4r   r   )r
   r	   r   r   r   r   )r   r   r   r   )r1   
__future__r   r   xml.etree.ElementTreer   ElementTreetypingr   r   ru   r   r   markdownr	   r   r$   	Processorr&   r   r   r   r+   r   r   <module>r      s  (  # " " " " " 				 % % % % % % % % % % % % % % % % %             "!!!!!!          DN   (Q Q Q Q Qm Q Q Qh
$M $M $M $M $MM $M $M $MN4 4 4 4 4M 4 4 4 4 4r   