o
    Rh,                     @  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   R/home/air/sanwanet/backup_V2/venv/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   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(   r1   r   r   r   __getitem__P      zSyntaxTreeNode.__getitem__slicelist[_NodeType]c                 C  r3   r4   r   r5   r   r   r   r6   T   r7   int | slice_NodeType | list[_NodeType]c                 C  s
   | j | S r4   )r'   r5   r   r   r   r6   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   rC   ^   s   



z:SyntaxTreeNode.to_tokens.<locals>.recursive_collect_tokensN)r=   r   r>   r<   r   r   r   )r(   r   r   rB   r   	to_tokens[   s   
zSyntaxTreeNode.to_tokensc                 C     | j S r4   r"   r/   r   r   r   r'   o      zSyntaxTreeNode.childrenvaluec                 C  
   || _ d S r4   rF   r(   rH   r   r   r   r'   s      
_NodeType | Nonec                 C  rE   r4   r!   r/   r   r   r   parentw   rG   zSyntaxTreeNode.parentc                 C  rI   r4   rM   rJ   r   r   r   rN   {   rK   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.
        )rN   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)rO   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)rR   indexr%   r(   
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)rR   rU   rV   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.   rN   r'   r@   )r(   r   rA   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&   rZ   r$   r@   )r(   r   reversed_tokensr   nested_tokensr&   r   r   r   r#      s&   



z(SyntaxTreeNode._set_children_from_tokens   Fr   indent	show_text_currentrb   rc   rd   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>>)texttext_special
ra   )
r.   rO   attrsjoinitemscontenttextwraprb   r'   pretty)r(   rb   rc   rd   prefixrm   rA   r   r   r   ru      s$   "


zSyntaxTreeNode.prettyinclude_selfrx    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).
        Trw   N)r'   walk)r(   rx   rA   r   r   r   rz      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" )r|   tagr/   r   r   r   r~        
zSyntaxTreeNode.tagdict[str, str | int | float]c                 C  r}   )zHtml attributes.)r|   rp   r/   r   r   r   rp     r   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.)r|   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)r|   maptuple)r(   map_r   r   r   r   "  s   
zSyntaxTreeNode.mapc                 C  r}   )z(nesting level, the same as `state.level`)r|   levelr/   r   r   r   r   +  r   zSyntaxTreeNode.levelc                 C  r}   )z]In a case of self-closing tag (code, html, fence, etc.), it
        has contents of this tag.)r|   rs   r/   r   r   r   rs   0     
zSyntaxTreeNode.contentc                 C  r}   )z5'*' or '_' for emphasis, fence string for fence, etc.)r|   markupr/   r   r   r   r   6  r   zSyntaxTreeNode.markupc                 C  r}   )zfence infostring)r|   infor/   r   r   r   r   ;  r   zSyntaxTreeNode.infodict[Any, Any]c                 C  r}   )z/A place for plugins to store an arbitrary data.)r|   metar/   r   r   r   r   @  r   zSyntaxTreeNode.metac                 C  r}   )z5True for block-level tokens, false for inline tokens.)r|   blockr/   r   r   r   r   E  r   zSyntaxTreeNode.blockc                 C  r}   )zbIf it's true, ignore this element when rendering.
        Used for tight lists to hide paragraphs.)r|   hiddenr/   r   r   r   r   J  r   zSyntaxTreeNode.hiddenN)r   )r   r   r   r   r   r   )r   r+   )r(   r   r1   r2   r   r   )r(   r   r1   r8   r   r9   )r(   r   r1   r:   r   r;   )r(   r   r   r<   )r(   r   r   r9   )r(   r   rH   r9   r   r   )r(   r   r   rL   )r(   r   rH   rL   r   r   )r   r   )r(   r   r   rQ   )r   r   r   r   )rb   r2   rc   r   rd   r2   r   r+   )r(   r   rx   r   r   ry   )r   r
   )r   r   )r   r+   r   r   )r   r   )r   r2   )r   r   )#r   r   r   __doc__r*   r0   r   r6   rD   propertyr'   setterrN   rO   rP   rR   r.   rX   rY   rZ   r#   ru   rz   r|   r~   rp   r   r   r   rs   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   rT   Q  s   rT   )r   r+   r   r+   r   r+   )r   
__future__r   collections.abcr   r   rt   typingr   r   r   r   r   r
   r   r   r   rT   r   r   r   r   <module>   s      =