U
    Sh,                     @  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   C/root/rtd-docs/venv/lib/python3.8/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 )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__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@t|| jj | jD ]} || qT|| jj d S )Nroot)r,   r&   r   appendr   AssertionErrorr   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   rB   rA   r   r   r   rC   {   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 )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_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.
        )rC   r&   r-   r   r   r   siblings   s    zSyntaxTreeNode.siblingsc                 C  s2   | j r
dS | jr| jjS | js"t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)rD   r   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)rF   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)rF   rI   rJ   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,   rC   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|rt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%   rM   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)rS   rT   rU   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k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>   s     z(SyntaxTreeNode.pretty.<locals>.<genexpr>>)textZtext_special
rR   )
r,   rD   attrsjoinitemscontenttextwraprS   r&   pretty)r'   rS   rT   rU   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 )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).
        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&   setterrC   rD   rE   rF   r,   rK   rL   rM   r"   re   ri   rk   rl   r`   rn   ro   rq   rc   rr   rs   rt   ru   rv   r   r   r   r   r      s|    *		

	  	r)   )string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$   )rz   r{   r   r   r   rH   Q  s    rH   )rw   
__future__r   collections.abcr   r   rd   typingr   r   r   r   r   r
   r   r   r   rH   r   r   r   r   <module>   s     =