a
    'ñ»j�,  ã                   @  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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   ú6E:\soccer\ml_env\Lib\site-packages\markdown_it/tree.pyr      s   
r   Ú	_NodeTypeÚSyntaxTreeNode)Úboundc                   @  sx  e Zd ZdZdcddœddddœd	d
„Zddœdd„Zeddddœdd„ƒZeddddœdd„ƒZddddœdd„Zdddœdd„Ze	dddœdd„ƒZ
e
jdddd œd!d„ƒZ
e	dd"dœd#d$„ƒZejdd"dd œd%d$„ƒZe	ddœd&d'„ƒZe	ddœd(d)„ƒZe	dd*dœd+d,„ƒZe	ddœd-d.„ƒZe	dd"dœd/d0„ƒZe	dd"dœd1d2„ƒZddd3œd4d5„Zddd3œd6d7„Zd8d9d:d;œddddd<œd=d>„Zdd?œddd@dAœdBdC„ZdDdœdEdF„Ze	ddœdGdH„ƒZe	dIdœdJdK„ƒZddLdMœdNdO„Ze	dPdœdQdR„ƒZe	ddœdSdT„ƒZe	ddœdUdV„ƒZe	ddœdWdX„ƒZe	ddœdYdZ„ƒZe	d[dœd\d]„ƒZ e	ddœd^d_„ƒZ!e	ddœd`da„ƒZ"dbS )dr   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_rootzSequence[Token]ÚboolÚNone)Útokensr   Úreturnc                C  sž   d| _ d| _d| _g | _|r*|  |¡ dS |s8tdƒ‚nbt|ƒdkrt|d }|jrZtdƒ‚|| _ |jrš|  |j¡ n&t	|d |d ƒ| _|  |dd… ¡ dS )zƒInitialize 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__Ústr)r   c                 C  s   t | ƒj› d| j › d�S )Nú(ú))Útyper   ©r'   r   r   r   Ú__repr__M   s    zSyntaxTreeNode.__repr__r   Úint)r'   Úitemr   c                 C  s   d S ©Nr   ©r'   r0   r   r   r   Ú__getitem__P   s    zSyntaxTreeNode.__getitem__Úslicezlist[_NodeType]c                 C  s   d S r1   r   r2   r   r   r   r3   T   s    zint | slicez_NodeType | list[_NodeType]c                 C  s
   | j | S r1   )r&   r2   r   r   r   r3   X   s    úlist[Token])r'   r   c                   s(   ddddœ‡ fdd„‰ g }ˆ | |ƒ |S )z Recover the linear token stream.r   r5   r   )ÚnodeÚ
token_listr   c                   sv   | j dkr"| jD ]}ˆ ||ƒ qnP| jr6| | j¡ n<| js@J ‚| | jj¡ | jD ]}ˆ ||ƒ qT| | jj¡ d S )NÚroot)r,   r&   r   Úappendr   r   r   )r6   r7   Úchild©Úrecursive_collect_tokensr   r   r<   ^   s    



z:SyntaxTreeNode.to_tokens.<locals>.recursive_collect_tokensr   )r'   r   r   r;   r   Ú	to_tokens[   s    
zSyntaxTreeNode.to_tokensc                 C  s   | j S r1   ©r!   r-   r   r   r   r&   o   s    zSyntaxTreeNode.children)r'   Úvaluer   c                 C  s
   || _ d S r1   r>   ©r'   r?   r   r   r   r&   s   s    z_NodeType | Nonec                 C  s   | j S r1   ©r    r-   r   r   r   Úparentw   s    zSyntaxTreeNode.parentc                 C  s
   || _ d S r1   rA   r@   r   r   r   rB   {   s    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 )zùIs 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_nestedz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.
        )rB   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
        r8   Ú_open)rC   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)rE   Ú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)rE   rH   rI   r   r   r   Úprevious_sibling³   s    zSyntaxTreeNode.previous_sibling)r   r   c                 C  s&   t | ƒ|dd�}| |_| j |¡ dS )zMake a child node for `self`.Fr   N)r,   rB   r&   r9   )r'   r   r:   r   r   r   Ú
_add_child¾   s    zSyntaxTreeNode._add_childc                 C  s”   t t|ƒƒ}|r�| ¡ }|js,|  |g¡ q|jdkr>tdƒ‚|g}d}|rn|rn| ¡ }| |¡ ||j7 }qH|r„td|d › �ƒ‚|  |¡ q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%   rL   r#   r9   )r'   r   Zreversed_tokensr   Znested_tokensr%   r   r   r   r"   Ç   s"    

z(SyntaxTreeNode._set_children_from_tokensé   Fr   ©ÚindentÚ	show_textÚ_current)rR   rS   rT   r   c                C  s²   d| }|d| j › � }| jsF| jrF|dd dd„ | j ¡ D ƒ¡ 7 }|d7 }|r†| js†| j dv r†| jr†|dt | j|d|  ¡ 7 }| jD ] }|d|j	|||| d� 7 }qŒ|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>æ   ó    z(SyntaxTreeNode.pretty.<locals>.<genexpr>ú>)ÚtextZtext_specialÚ
rQ   )
r,   rC   ÚattrsÚjoinÚitemsÚcontentÚtextwraprR   r&   Úpretty)r'   rR   rS   rT   Úprefixr^   r:   r   r   r   re   ß   s&    "ÿþýü

ÿzSyntaxTreeNode.pretty©Úinclude_selfz Generator[_NodeType, None, None])r'   rh   r   c                c  s,   |r
| V  | j D ]}|jdd�E dH  qdS )z³Recursively 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).
        Trg   N)r&   Úwalk)r'   rh   r:   r   r   r   ri   õ   s    
zSyntaxTreeNode.walkr
   c                 C  s&   | j r| j S | jr| jjS tdƒ‚dS )z\Return the `Token` that is used as the data source for the
        properties defined below.z.Root node does not have the accessed attributeN)r   r   r   ÚAttributeErrorr-   r   r   r   Ú_attribute_token  s
    zSyntaxTreeNode._attribute_tokenc                 C  s
   |   ¡ jS )zhtml tag name, e.g. "p" )rk   Útagr-   r   r   r   rl     s    zSyntaxTreeNode.tagzdict[str, str | int | float]c                 C  s
   |   ¡ jS )zHtml attributes.)rk   r`   r-   r   r   r   r`     s    zSyntaxTreeNode.attrszNone | str | int | float)Únamer   c                 C  s   |   ¡  |¡S )z@Get the value of attribute `name`, or null if it does not exist.)rk   ÚattrGet)r'   rm   r   r   r   rn     s    zSyntaxTreeNode.attrGetztuple[int, int] | Nonec                 C  s   |   ¡ j}|rt|ƒS dS )z8Source map info. Format: `tuple[ line_begin, line_end ]`N)rk   ÚmapÚtuple)r'   Zmap_r   r   r   ro   "  s    
zSyntaxTreeNode.mapc                 C  s
   |   ¡ jS )z(nesting level, the same as `state.level`)rk   Úlevelr-   r   r   r   rq   +  s    zSyntaxTreeNode.levelc                 C  s
   |   ¡ jS )z]In a case of self-closing tag (code, html, fence, etc.), it
        has contents of this tag.)rk   rc   r-   r   r   r   rc   0  s    zSyntaxTreeNode.contentc                 C  s
   |   ¡ jS )z5'*' or '_' for emphasis, fence string for fence, etc.)rk   Úmarkupr-   r   r   r   rr   6  s    zSyntaxTreeNode.markupc                 C  s
   |   ¡ jS )zfence infostring)rk   Úinfor-   r   r   r   rs   ;  s    zSyntaxTreeNode.infozdict[Any, Any]c                 C  s
   |   ¡ jS )z/A place for plugins to store an arbitrary data.)rk   Úmetar-   r   r   r   rt   @  s    zSyntaxTreeNode.metac                 C  s
   |   ¡ jS )z5True for block-level tokens, false for inline tokens.)rk   Úblockr-   r   r   r   ru   E  s    zSyntaxTreeNode.blockc                 C  s
   |   ¡ jS )zbIf it's true, ignore this element when rendering.
        Used for tight lists to hide paragraphs.)rk   Úhiddenr-   r   r   r   rv   J  s    zSyntaxTreeNode.hiddenN)r   )#r   r   r   Ú__doc__r(   r.   r   r3   r=   Úpropertyr&   ÚsetterrB   rC   rD   rE   r,   rJ   rK   rL   r"   re   ri   rk   rl   r`   rn   ro   rq   rc   rr   rs   rt   ru   rv   r   r   r   r   r      sx    ÿÿ*		

	ÿÿ	r)   )ÚstringÚsuffixr   c                 C  s$   |r |   |¡r | dt|ƒ … S | S )z�Remove a suffix from a string.

    Replace this with str.removesuffix() from stdlib when minimum Python
    version is 3.9.
    N)Úendswithr$   )rz   r{   r   r   r   rG   Q  s    rG   )rw   Ú
__future__r   Úcollections.abcr   r   rd   Útypingr   r   r   r   r   r
   r   r   r   rG   r   r   r   r   Ú<module>   s     =