
    'Wd,                        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	e	      Z e
d
d      Z G d d      ZddZy)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                  "    e Zd ZU ded<   ded<   y)_NesterTokensr   openingclosingN)__name__
__module____qualname____annotations__     2/usr/lib/python3/dist-packages/markdown_it/tree.pyr   r      s    NNr   r   	_NodeTypeSyntaxTreeNode)boundc                  X   e Zd ZdZ	 d*dd	 	 	 	 	 d+dZd,dZed-d       Zed.d       Zd/dZd0d	Ze	d1d
       Z
e
j                  d2d       Z
e	d3d       Zej                  d4d       Ze	d5d       Ze	d5d       Ze	d6d       Ze	d,d       Ze	d3d       Ze	d3d       Z	 	 	 	 d7dZd7dZdddd	 	 	 	 	 	 	 d8dZdd	 	 	 	 	 d9dZd:dZe	d,d       Ze	d;d       Zd<d Ze	d=d!       Ze	d>d"       Ze	d,d#       Ze	d,d$       Ze	d,d%       Ze	d?d&       Z e	d5d'       Z!e	d5d(       Z"y))@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
    Tcreate_rootc                  d| _         d| _        d| _        g | _        |r| j	                  |       y|st        d      t        |      dk(  rL|d   }|j                  rt        d      || _         |j                  r| j	                  |j                         yyt        |d   |d         | _        | j	                  |dd        y)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   )selftokensr   inline_tokens       r   __init__zSyntaxTreeNode.__init__#   s     $(
 48 ! %'**62+  [A!!9L## Q  &DJ$$..|/D/DE % "/vay&*!ED**6!B<8r   c                L    t        |       j                   d| j                    dS )N())typer   r(   s    r   __repr__zSyntaxTreeNode.__repr__M   s$    t*%%&a		{!44r   c                     y Nr   r(   items     r   __getitem__zSyntaxTreeNode.__getitem__P       r   c                     y r3   r   r4   s     r   r6   zSyntaxTreeNode.__getitem__T   r7   r   c                     | j                   |   S r3   )r'   r4   s     r   r6   zSyntaxTreeNode.__getitem__X   s    }}T""r   c                *    dfdg } | |       |S )z Recover the linear token stream.c                   | j                   dk(  r| j                  D ]  } ||        y | j                  r|j                  | j                         y | j                  sJ |j                  | j                  j
                         | j                  D ]  } ||        |j                  | j                  j                         y )Nroot)r/   r'   r   appendr    r   r   )node
token_listchildrecursive_collect_tokenss      r   rA   z:SyntaxTreeNode.to_tokens.<locals>.recursive_collect_tokens^   s    yyF"!]] @E,UJ?@!!$**-))))!!$"4"4"<"<=!]] @E,UJ?@!!$"4"4"<"<=r   )r>   r   r?   list[Token]returnNoner   )r(   r)   rA   s     @r   	to_tokenszSyntaxTreeNode.to_tokens[   s    	> ! v.r   c                    | j                   S r3   r"   r0   s    r   r'   zSyntaxTreeNode.childreno   s    ~~r   c                    || _         y r3   rG   r(   values     r   r'   zSyntaxTreeNode.childrens   s	    r   c                    | j                   S r3   r!   r0   s    r   parentzSyntaxTreeNode.parentw   s    ||r   c                    || _         y r3   rL   rI   s     r   rM   zSyntaxTreeNode.parent{   s	    r   c                8    | j                   xs | j                   S )z Is the node a special root node?)r   r    r0   s    r   is_rootzSyntaxTreeNode.is_root   s     JJ4$"4"455r   c                ,    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.
        )boolr    r0   s    r   	is_nestedzSyntaxTreeNode.is_nested   s     D&&''r   c                L    | j                   s| gS | j                   j                  S )z]Get siblings of the node.

        Gets the whole group of siblings, including self.
        )rM   r'   r0   s    r   siblingszSyntaxTreeNode.siblings   s"     {{6M{{###r   c                    | j                   ry| 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)rP   r   r/   r    _removesuffixr   r0   s    r   r/   zSyntaxTreeNode.type   sQ     <<::::??"!!!!T//77<<gFFr   c                    | j                   j                  |       }|dz   t        | j                         k  r| j                   |dz      S y)zlGet the next node in the sequence of siblings.

        Returns `None` if this is the last sibling.
        r
   N)rU   indexr%   r(   
self_indexs     r   next_siblingzSyntaxTreeNode.next_sibling   sC     ]]((.
>C..==a00r   c                n    | j                   j                  |       }|dz
  dk\  r| j                   |dz
     S y)zqGet the previous node in the sequence of siblings.

        Returns `None` if this is the first sibling.
        r
   r   N)rU   rZ   r[   s     r   previous_siblingzSyntaxTreeNode.previous_sibling   s:     ]]((.
>Q==a00r   c                n     t        |       |d      }| |_        | j                  j                  |       y)zMake a child node for `self`.Fr   N)r/   rM   r'   r=   )r(   r)   r@   s      r   
_add_childzSyntaxTreeNode._add_child   s/    
 T
6u5U#r   c                   t        t        |            }|r|j                         }|j                  s| j	                  |g       1|j                  dk7  rt        d      |g}d}|r7|r5|j                         }|j                  |       ||j                  z  }|r|r5|rt        d|d          | j	                  |       |ryy)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&   ra   r$   r=   )r(   r)   reversed_tokensr   nested_tokensr&   s         r   r#   z(SyntaxTreeNode._set_children_from_tokens   s     x/0#'')E==(}}! !899"GMG!g'++-$$U+5==( "g  #<]1=M<N!OPPOOM*% r      Fr   indent	show_text_currentc          	        d|z  }|d| j                    z   }| j                  sB| j                  r6|ddj                  d | j                  j	                         D              z   z  }|dz  }|rR| j                  sF| j                   dv r8| j
                  r,|dt        j                  | j
                  |d|z  z         z   z  }| j                  D ]  }|d|j                  ||||z         z   z  }! |S )z'Create an XML style string of the tree. <c              3  0   K   | ]  \  }}| d |  yw)=Nr   ).0kvs      r   	<genexpr>z(SyntaxTreeNode.pretty.<locals>.<genexpr>   s     "ODAqaS!<"Os   >)texttext_special
ri   )
r/   rP   attrsjoinitemscontenttextwraprj   r'   pretty)r(   rj   rk   rl   prefixrw   r@   s          r   r   zSyntaxTreeNode.pretty   s     x!DII;'||

C#(("ODJJ<L<L<N"OOOODLL		55D8??4<<#,9NOOOD]] 	ED5<<X=N (   D	 r   include_selfc             #  r   K   |r|  | j                   D ]  }|j                  d      E d{     y7 w)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).
        Tr   N)r'   walk)r(   r   r@   s      r   r   zSyntaxTreeNode.walk   s:      J]] 	5Ezztz444	54s   +757c                    | 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   AttributeErrorr0   s    r   _attribute_tokenzSyntaxTreeNode._attribute_token  s<     ::::%%---MNNr   c                6    | j                         j                  S )zhtml tag name, e.g. "p" )r   tagr0   s    r   r   zSyntaxTreeNode.tag  s     $$&***r   c                6    | j                         j                  S )zHtml attributes.)r   rz   r0   s    r   rz   zSyntaxTreeNode.attrs       $$&,,,r   c                @    | j                         j                  |      S )z@Get the value of attribute `name`, or null if it does not exist.)r   attrGet)r(   names     r   r   zSyntaxTreeNode.attrGet  s    $$&..t44r   c                R    | j                         j                  }|rt        |      S y)z8Source map info. Format: `tuple[ line_begin, line_end ]`N)r   maptuple)r(   map_s     r   r   zSyntaxTreeNode.map"  s(     $$&**;r   c                6    | j                         j                  S )z(nesting level, the same as `state.level`)r   levelr0   s    r   r   zSyntaxTreeNode.level+  r   r   c                6    | j                         j                  S )z]In a case of self-closing tag (code, html, fence, etc.), it
        has contents of this tag.)r   r}   r0   s    r   r}   zSyntaxTreeNode.content0  s     $$&...r   c                6    | j                         j                  S )z5'*' or '_' for emphasis, fence string for fence, etc.)r   markupr0   s    r   r   zSyntaxTreeNode.markup6  s     $$&---r   c                6    | j                         j                  S )zfence infostring)r   infor0   s    r   r   zSyntaxTreeNode.info;       $$&+++r   c                6    | j                         j                  S )z/A place for plugins to store an arbitrary data.)r   metar0   s    r   r   zSyntaxTreeNode.meta@  r   r   c                6    | j                         j                  S )z5True for block-level tokens, false for inline tokens.)r   blockr0   s    r   r   zSyntaxTreeNode.blockE  r   r   c                6    | j                         j                  S )zbIf it's true, ignore this element when rendering.
        Used for tight lists to hide paragraphs.)r   hiddenr0   s    r   r   zSyntaxTreeNode.hiddenJ  s     $$&---r   N)r   )r)   Sequence[Token]r   rR   rC   rD   )rC   str)r(   r   r5   intrC   r   )r(   r   r5   slicerC   list[_NodeType])r(   r   r5   zint | slicerC   z_NodeType | list[_NodeType])r(   r   rC   rB   )r(   r   rC   r   )r(   r   rJ   r   rC   rD   )r(   r   rC   _NodeType | None)r(   r   rJ   r   rC   rD   )rC   rR   )r(   r   rC   zSequence[_NodeType])r)   r   rC   rD   )rj   r   rk   rR   rl   r   rC   r   )r(   r   r   rR   rC   z Generator[_NodeType, None, None])rC   r   )rC   zdict[str, str | int | float])r   r   rC   zNone | str | int | float)rC   ztuple[int, int] | None)rC   r   )rC   zdict[Any, Any])#r   r   r   __doc__r+   r1   r	   r6   rE   propertyr'   setterrM   rP   rS   rU   r/   r]   r_   ra   r#   r   r   r   r   rz   r   r   r   r}   r   r   r   r   r   r   r   r   r   r      sz   
 )+(9CG(9%(9<@(9	(9T5    #(   __    ]]  6 6 ( ( $ $ G G    $$ 
$+2  !E1-1EH	. 2655*.5	)5,O + + - -5   - - / /
 . . , , , , - - . .r   c                J    |r | j                  |      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%   )stringsuffixs     r   rX   rX   Q  s*     &//&)nV%%Mr   )r   r   r   r   rC   r   )r   
__future__r   collections.abcr   r   r~   typingr   r   r   r	   r   r   r   r   r   rX   r   r   r   <module>r      sK    # /  5 5 J 
 K'78	x. x.v	r   