U
    Sh"                     @  s<  d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d
dlmZmZ ddddddddddddddZddddddddZddd d!d"d#Zd$ddd%d&d'Zddd$ddd(dd)d*d+Zd,ddddd-d.d/Zd0d(dd1d2d3Zd4d5d(dd6d7d8ZdS )9    )annotations)partial)AnySequence)
MarkdownIt)
StateBlock)	StateCore)StateInline)Token)is_code_block   )
ParseErrorparse)imageZcode_inlineZ
link_close
span_closeFlinkN)afterspans
span_afterallowedr   zSequence[str]boolstrzSequence[str] | NoneNone)mdr   r   r   r   returnc                C  sH   |r| j j|dt |rD| j jdtt||dkr6dnt|d dS )a  Parse inline attributes that immediately follow certain inline elements::

        ![alt](https://image.com){#id .a b=c}

    This syntax is inspired by
    `Djot spans
    <https://htmlpreview.github.io/?https://github.com/jgm/djot/blob/master/doc/syntax.html#inline-attributes>`_.

    Inside the curly braces, the following syntax is possible:

    - `.foo` specifies foo as a class.
      Multiple classes may be given in this way; they will be combined.
    - `#foo` specifies foo as an identifier.
      An element may have only one identifier;
      if multiple identifiers are given, the last one is used.
    - `key="value"` or `key=value` specifies a key-value attribute.
       Quotes are not needed when the value consists entirely of
       ASCII alphanumeric characters or `_` or `:` or `-`.
       Backslash escapes may be used inside quoted values.
    - `%` begins a comment, which ends with the next `%` or the end of the attribute (`}`).

    Multiple attribute blocks are merged.

    :param md: The MarkdownIt instance to modify.
    :param after: The names of inline elements after which attributes may be specified.
        This plugin does not support attributes after emphasis, strikethrough or text elements,
        which all require post-parse processing.
    :param spans: If True, also parse attributes after spans of text, encapsulated by `[]`.
        Note Markdown link references take precedence over this syntax.
    :param span_after: The name of an inline rule after which spans may be specified.
    :param allowed: A list of allowed attribute names.
        If not ``None``, any attributes not in this list will be removed
        and placed in the token's meta under the key "insecure_attrs".
    spanattrN)r   r   )inlinerulerr   
_span_rulepushr   _attr_inline_ruleset)r   r   r   r   r    r#   N/root/rtd-docs/venv/lib/python3.8/site-packages/mdit_py_plugins/attrs/index.pyattrs_plugin   s    +r%   r   )r   r   r   c                C  s@   | j jddt | jjddtt|dkr.dnt|d dS )a  Parse block attributes.

    Block attributes are attributes on a single line, with no other content.
    They attach the specified attributes to the block below them::

        {.a #b c=1}
        A paragraph, that will be assigned the class ``a`` and the identifier ``b``.

    Attributes can be stacked, with classes accumulating and lower attributes overriding higher::

        {#a .a c=1}
        {#b .b c=2}
        A paragraph, that will be assigned the class ``a b c``, and the identifier ``b``.

    This syntax is inspired by Djot block attributes.

    :param allowed: A list of allowed attribute names.
        If not ``None``, any attributes not in this list will be removed
        and placed in the token's meta under the key "insecure_attrs".
    Zfencer   blockNr&   )	r'   r   before_attr_block_rulecorer   r   _attr_resolve_block_ruler"   )r   r   r#   r#   r$   attrs_block_pluginI   s     r,   zSequence[Token]intz
int | None)tokensindexr   c                 C  sF   | | j dkr|S d}|dkrB|| | j 7 }|dkr8|S |d8 }qdS )z6Find the opening token index, if the token is closing.r   r   N)Znesting)r.   r/   levelr#   r#   r$   _find_openingh   s    
r2   r	   )statesilentr   c           	      C  s   | j | j dkrdS | j}| jd }| jj| | jd}|dk rDdS |d }||krXdS zt| j |d  \}}W n tk
r   Y dS X ||d 7 }|s|| _|| _| ddd}||_	| jj
|  | ddd}|| _|| _d	S )
N[Fr   r   Z	span_openr   r   r0   T)srcposZposMaxr   ZhelpersZparseLinkLabelr   r   r    attrsr   tokenize)	r3   r4   maximumZ
labelStartZlabelEndr7   new_posr8   tokenr#   r#   r$   r   u   s2    
r   zset[str] | None)r3   r4   r   r   r   c          	      C  s   | j s| jsdS | jd }|j|kr(dS zt| j| jd  \}}W n tk
rZ   Y dS X t| jt| jd }|d kr~dS |  j|d 7  _|s| j| }d|krd|j	kr|j	d  d|d  |d< t
||| dS )NFr0   r   class T)pendingr.   typer   r6   r7   r   r2   lenr8   
_add_attrs)	r3   r4   r   r   r<   r;   r8   Ztoken_indexZ
attr_tokenr#   r#   r$   r!      s&    


r!   r   )r3   	startLineendLiner4   r   c           	      C  s   t | |rdS | j| | j|  }| j| }| j| dkr>dS ||krb| j|d  dkrb|d8 }q>||krndS | j|d  dkrdS zt| j|| \}}W n tk
r   Y dS X |rdS | ddd	}||_||d g|_	|d | _
dS )
zFind a block of attributes.

    The block must be a single line that begins with a `{`, after three or less spaces,
    and end with a `}` followed by any number if spaces.
    F{r   )r>   	}Tattrs_block r   )r   ZbMarksZtShiftZeMarksr6   r   r   r    r8   mapline)	r3   rC   rD   r4   r7   r:   r;   r8   r<   r#   r#   r$   r)      s.    



r)   r   )r3   r   r   c                C  s   d}t | j}||k r| j| jdkr0|d7 }q|d |k r| j|d  }d| j| jkrd|jkr| j| jd  d|jd  | j| jd< |jdkr| j| j D ]$\}}|dks||jkr||j|< qnt|| j| j| | j| |d8 }qdS )z@Find attribute block then move its attributes to the next block.r   rH   r   r=   r>   N)rA   r.   r@   r8   itemsrB   pop)r3   r   iZ
len_tokens
next_tokenkeyvaluer#   r#   r$   r+      s$    

r+   r
   zdict[str, Any])r<   r8   r   r   c                   sR    dk	rB fdd|  D  }rB|| jd<  fdd|  D }| j| dS )z>Add attributes to a token, skipping any disallowed attributes.Nc                   s   i | ]\}}| kr||qS r#   r#   .0kvr&   r#   r$   
<dictcomp>  s       z_add_attrs.<locals>.<dictcomp>Zinsecure_attrsc                   s   i | ]\}}| kr||qS r#   r#   rR   r&   r#   r$   rV     s       )rL   metar8   update)r<   r8   r   Z
disallowedr#   r&   r$   rB     s    
rB   )
__future__r   	functoolsr   typingr   r   Zmarkdown_itr   Zmarkdown_it.rules_blockr   Zmarkdown_it.rules_corer   Zmarkdown_it.rules_inliner	   Zmarkdown_it.tokenr
   Zmdit_py_plugins.utilsr   r   r   r%   r,   r2   r   r!   r)   r+   rB   r#   r#   r#   r$   <module>   s,   8+0