o
    Zh,                     @  s   d Z ddlmZ ddlmZmZ ddlZddlmZm	Z	m
Z
mZ ddlmZ G dd	 d	e	Ze
d
ddZG dd dZdddZdS )zyA tree representation of a linear markdown-it token stream.

This module is not part of upstream JavaScript markdown-it.
    )annotations)	GeneratorSequenceN)Any
NamedTupleTypeVaroverload   )Tokenc                   @  s   e Zd ZU ded< ded< dS )_NesterTokensr
   openingclosingN)__name__
__module____qualname____annotations__ r   r   G/var/www/html/lang_env/lib/python3.10/site-packages/markdown_it/tree.pyr      s   
 r   	_NodeTypeSyntaxTreeNode)boundc                   @  s  e Zd ZdZ	dedddfddZdgddZedhddZediddZdjddZdkddZe	dldd Z
e
jdmd"d Z
e	dnd$d%Zejdod&d%Ze	dpd'd(Ze	dpd)d*Ze	dqd,d-Ze	dgd.d/Ze	dnd0d1Ze	dnd2d3Zdrd4d5Zdrd6d7Zd8d9d:d;dsd?d@ZddAdtdDdEZdudGdHZe	dgdIdJZe	dvdLdMZdwdPdQZe	dxdSdTZe	dydUdVZe	dgdWdXZe	dgdYdZZe	dgd[d\Ze	dzd^d_Z e	dpd`daZ!e	dpdbdcZ"ddS ){r   ae  A Markdown syntax tree node.

    A class that can be used to construct a tree representation of a linear
    `markdown-it-py` token stream.

    Each node in the tree represents either:
      - root of the Markdown document
      - a single unnested `Token`
      - a `Token` "_open" and "_close" token pair, and the tokens nested in
          between
    r   Tcreate_roottokensSequence[Token]r   boolreturnNonec                C  s   d| _ d| _d| _g | _|r| | dS |stdt|dkr<|d }|jr,td|| _ |jr:| |j dS dS t	|d |d | _| |dd  dS )zInitialize a `SyntaxTreeNode` from a token stream.

        If `create_root` is True, create a root node for the document.
        NzGCan only create root from empty token sequence. Set `create_root=True`.r	   r   z;Unequal nesting level at the start and end of token stream.)
tokennester_tokens_parent	_children_set_children_from_tokens
ValueErrorlennestingchildrenr   )selfr   r   Zinline_tokenr   r   r   __init__#   s.   
zSyntaxTreeNode.__init__strc                 C  s   t | j d| j  dS )N())typer   r(   r   r   r   __repr__M   s   zSyntaxTreeNode.__repr__r(   r   itemintc                 C     d S Nr   r(   r0   r   r   r   __getitem__P      zSyntaxTreeNode.__getitem__slicelist[_NodeType]c                 C  r2   r3   r   r4   r   r   r   r5   T   r6   int | slice_NodeType | list[_NodeType]c                 C  s
   | j | S r3   )r'   r4   r   r   r   r5   X   s   
list[Token]c                   s    d
 fdd g } | | |S )z Recover the linear token stream.noder   
token_listr;   r   r   c                   sz   | j dkr| jD ]} || qd S | jr|| j d S | js"J || jj | jD ]} || q,|| jj d S )Nroot)r-   r'   r   appendr    r   r   )r<   r=   childrecursive_collect_tokensr   r   rB   ^   s   



z:SyntaxTreeNode.to_tokens.<locals>.recursive_collect_tokensN)r<   r   r=   r;   r   r   r   )r(   r   r   rA   r   	to_tokens[   s   
zSyntaxTreeNode.to_tokensc                 C     | j S r3   r"   r.   r   r   r   r'   o      zSyntaxTreeNode.childrenvaluec                 C  
   || _ d S r3   rE   r(   rG   r   r   r   r'   s      
_NodeType | Nonec                 C  rD   r3   r!   r.   r   r   r   parentw   rF   zSyntaxTreeNode.parentc                 C  rH   r3   rL   rI   r   r   r   rM   {   rJ   c                 C  s   | j p| j S )z Is the node a special root node?)r   r    r.   r   r   r   is_root   s   zSyntaxTreeNode.is_rootc                 C  s
   t | jS )zIs this node nested?.

        Returns `True` if the node represents a `Token` pair and tokens in the
        sequence between them, where `Token.nesting` of the first `Token` in
        the pair is 1 and nesting of the other `Token` is -1.
        )r   r    r.   r   r   r   	is_nested   s   
zSyntaxTreeNode.is_nestedSequence[_NodeType]c                 C  s   | j s| gS | j jS )z]Get siblings of the node.

        Gets the whole group of siblings, including self.
        )rM   r'   r.   r   r   r   siblings   s   zSyntaxTreeNode.siblingsc                 C  s2   | j rdS | jr| jjS | jsJ t| jjjdS )a  Get a string type of the represented syntax.

        - "root" for root nodes
        - `Token.type` if the node represents an unnested token
        - `Token.type` of the opening token, with "_open" suffix stripped, if
            the node represents a nester token pair
        r>   _open)rN   r   r-   r    _removesuffixr   r.   r   r   r   r-      s   	
zSyntaxTreeNode.typec                 C  s0   | j | }|d t| j k r| j |d  S dS )zlGet the next node in the sequence of siblings.

        Returns `None` if this is the last sibling.
        r	   N)rQ   indexr%   r(   Z
self_indexr   r   r   next_sibling   s   zSyntaxTreeNode.next_siblingc                 C  s*   | j | }|d dkr| j |d  S dS )zqGet the previous node in the sequence of siblings.

        Returns `None` if this is the first sibling.
        r	   r   N)rQ   rT   rU   r   r   r   previous_sibling   s   zSyntaxTreeNode.previous_siblingc                 C  s&   t | |dd}| |_| j| dS )zMake a child node for `self`.Fr   N)r-   rM   r'   r?   )r(   r   r@   r   r   r   
_add_child   s   zSyntaxTreeNode._add_childc                 C  s   t t|}|rN| }|js| |g q|jdkrtd|g}d}|r:|r:| }|| ||j7 }|r:|s(|rEtd|d  | | |sdS dS )zgConvert the token stream to a tree structure and set the resulting
        nodes as children of `self`.r	   zInvalid token nestingzunclosed tokens starting r   N)listreversedpopr&   rX   r$   r?   )r(   r   Zreversed_tokensr   Znested_tokensr&   r   r   r   r#      s&   



z(SyntaxTreeNode._set_children_from_tokens   Fr   indent	show_text_currentr^   r_   r`   c                C  s   d| }|d| j   }| js#| jr#|dddd | j D  7 }|d7 }|rC| jsC| j dv rC| jrC|dt| j|d|   7 }| jD ]}|d|j	|||| d 7 }qF|S )	z'Create an XML style string of the tree. <c                 s  s"    | ]\}}| d |V  qdS )=Nr   ).0kvr   r   r   	<genexpr>   s     z(SyntaxTreeNode.pretty.<locals>.<genexpr>>)textZtext_special
r]   )
r-   rN   attrsjoinitemscontenttextwrapr^   r'   pretty)r(   r^   r_   r`   prefixri   r@   r   r   r   rp      s$   "


zSyntaxTreeNode.prettyinclude_selfrs    Generator[_NodeType, None, None]c                c  s.    |r| V  | j D ]}|jddE dH  q	dS )zRecursively yield all descendant nodes in the tree starting at self.

        The order mimics the order of the underlying linear token
        stream (i.e. depth first).
        Trr   N)r'   walk)r(   rs   r@   r   r   r   ru      s   
zSyntaxTreeNode.walkr
   c                 C  s"   | j r| j S | jr| jjS td)z\Return the `Token` that is used as the data source for the
        properties defined below.z.Root node does not have the accessed attribute)r   r    r   AttributeErrorr.   r   r   r   _attribute_token  s
   zSyntaxTreeNode._attribute_tokenc                 C  
   |   jS )zhtml tag name, e.g. "p" )rw   tagr.   r   r   r   ry        
zSyntaxTreeNode.tagdict[str, str | int | float]c                 C  rx   )zHtml attributes.)rw   rk   r.   r   r   r   rk     rz   zSyntaxTreeNode.attrsnameNone | str | int | floatc                 C  s   |   |S )z@Get the value of attribute `name`, or null if it does not exist.)rw   attrGet)r(   r|   r   r   r   r~     s   zSyntaxTreeNode.attrGettuple[int, int] | Nonec                 C  s   |   j}|rt|S dS )z8Source map info. Format: `tuple[ line_begin, line_end ]`N)rw   maptuple)r(   Zmap_r   r   r   r   "  s   
zSyntaxTreeNode.mapc                 C  rx   )z(nesting level, the same as `state.level`)rw   levelr.   r   r   r   r   +  rz   zSyntaxTreeNode.levelc                 C  rx   )z]In a case of self-closing tag (code, html, fence, etc.), it
        has contents of this tag.)rw   rn   r.   r   r   r   rn   0     
zSyntaxTreeNode.contentc                 C  rx   )z5'*' or '_' for emphasis, fence string for fence, etc.)rw   markupr.   r   r   r   r   6  rz   zSyntaxTreeNode.markupc                 C  rx   )zfence infostring)rw   infor.   r   r   r   r   ;  rz   zSyntaxTreeNode.infodict[Any, Any]c                 C  rx   )z/A place for plugins to store an arbitrary data.)rw   metar.   r   r   r   r   @  rz   zSyntaxTreeNode.metac                 C  rx   )z5True for block-level tokens, false for inline tokens.)rw   blockr.   r   r   r   r   E  rz   zSyntaxTreeNode.blockc                 C  rx   )zbIf it's true, ignore this element when rendering.
        Used for tight lists to hide paragraphs.)rw   hiddenr.   r   r   r   r   J  r   zSyntaxTreeNode.hiddenN)r   )r   r   r   r   r   r   )r   r*   )r(   r   r0   r1   r   r   )r(   r   r0   r7   r   r8   )r(   r   r0   r9   r   r:   )r(   r   r   r;   )r(   r   r   r8   )r(   r   rG   r8   r   r   )r(   r   r   rK   )r(   r   rG   rK   r   r   )r   r   )r(   r   r   rP   )r   r   r   r   )r^   r1   r_   r   r`   r1   r   r*   )r(   r   rs   r   r   rt   )r   r
   )r   r{   )r|   r*   r   r}   )r   r   )r   r1   )r   r   )#r   r   r   __doc__r)   r/   r   r5   rC   propertyr'   setterrM   rN   rO   rQ   r-   rV   rW   rX   r#   rp   ru   rw   ry   rk   r~   r   r   rn   r   r   r   r   r   r   r   r   r   r      sz    
*

		



	
	
stringr*   suffixr   c                 C  s$   |r|  |r| dt|  S | S )zRemove a suffix from a string.

    Replace this with str.removesuffix() from stdlib when minimum Python
    version is 3.9.
    N)endswithr%   )r   r   r   r   r   rS   Q  s   rS   )r   r*   r   r*   r   r*   )r   
__future__r   collections.abcr   r   ro   typingr   r   r   r   r   r
   r   r   r   rS   r   r   r   r   <module>   s      =