U
    Sh'                     @  sv   d dl mZ d dlmZmZ d dlZd dlmZm	Z	 d dl
Z
d dlmZ ddddd	Zejf eG d
d dZdS )    )annotations)CallableMutableMappingN)AnyLiteral)DATACLASS_KWARGSr   )valuereturnc                 C  s   | si S t | trt| S | S )zConvert Token.attrs set as ``None`` or ``[[key, value], ...]`` to a dict.

    This improves compatibility with upstream markdown-it.
    )
isinstancelistdict)r    r   D/root/rtd-docs/venv/lib/python3.8/site-packages/markdown_it/token.pyconvert_attrs   s
    
r   c                   @  sh  e Zd ZU ded< ded< ded< ejedZded< d	Zd
ed< dZ	ded< d	Z
ded< dZded< dZded< dZded< ejedZded< dZded< dZded< ddddZdddd d!Zd"dd#d$Zd%dd&d'd(Zdd)dd*d+d,Zdd-dd.d/Zdddd*d0d1Zd2d d3d4d5Zd6d6d	d	ed7ddd8d9d:d;d<d=d>Zed;d d?d@dAZd	S )BTokenstrtypetagzLiteral[(-1, 0, 1)]Znesting)default_factoryzdict[str, str | int | float]attrsNzlist[int] | Nonemapr   intlevelzlist[Token] | Nonechildren contentmarkupinfozdict[Any, Any]metaFboolblockhiddenNone)r	   c                 C  s   t | j| _d S N)r   r   selfr   r   r   __post_init__O   s    zToken.__post_init__)namer	   c                 C  s.   t dt || jkrdS t| j |S )NzEToken.attrIndex should not be used, since Token.attrs is a dictionary)warningswarnUserWarningr   r   keysindexr%   r'   r   r   r   	attrIndexR   s    
zToken.attrIndexz#list[tuple[str, str | int | float]]c                 C  s   t | j S )zGet (key, value) list of attrs.)r   r   itemsr$   r   r   r   	attrItems[   s    zToken.attrItemsztuple[str, str | int | float])attrDatar	   c                 C  s   |\}}|  || dS )zAAdd `[ name, value ]` attribute to list. Init attrs if necessary.N)attrSet)r%   r2   r'   r   r   r   r   attrPush_   s    zToken.attrPushzstr | int | float)r'   r   r	   c                 C  s   || j |< dS )z>Set `name` attribute to `value`. Override old value if exists.N)r   )r%   r'   r   r   r   r   r3   d   s    zToken.attrSetzNone | str | int | floatc                 C  s   | j |dS )z@Get the value of attribute `name`, or null if it does not exist.N)r   getr.   r   r   r   attrGeth   s    zToken.attrGetc                 C  sV   || j krH| j | }t|ts2td| j |  | d| | j |< n
|| j |< dS )zJoin value to existing attribute via space.
        Or create new attribute if not exists.
        Useful to operate with token classes.
        z#existing attr 'name' is not a str:  N)r   r
   r   	TypeError)r%   r'   r   currentr   r   r   attrJoinl   s    


zToken.attrJoinr   )changesr	   c                 K  s   t j| f|S )z&Return a shallow copy of the instance.)dcreplace)r%   r;   r   r   r   copy{   s    z
Token.copyT)r   as_upstreammeta_serializerfilterdict_factoryz&Callable[[dict[Any, Any]], Any] | Nonez!Callable[[str, Any], bool] | Nonez'Callable[..., MutableMapping[str, Any]]zMutableMapping[str, Any])r   r?   r@   rA   rB   r	   c                  s   fddt D }r:fdd| D } rld|krl|d sRdndd |d  D |d< rd|kr|d |d< r|d	dr fd
d|d	 D |d	< |S )a  Return the token as a dictionary.

        :param children: Also convert children to dicts
        :param as_upstream: Ensure the output dictionary is equal to that created by markdown-it
            For example, attrs are converted to null or lists
        :param meta_serializer: hook for serializing ``Token.meta``
        :param filter: A callable whose return code determines whether an
            attribute or element is included (``True``) or dropped (``False``).
            Is called with the (key, value) pair.
        :param dict_factory: A callable to produce dictionaries from.
            For example, to produce ordered dictionaries instead of normal Python
            dictionaries, pass in ``collections.OrderedDict``.

        c                 3  s    | ]}|j t |j fV  qd S r#   )r'   getattr).0fr$   r   r   	<genexpr>   s     z Token.as_dict.<locals>.<genexpr>c                 3  s$   | ]\}} ||r||fV  qd S r#   r   rD   kv)rA   r   r   rF      s     
 r   Nc                 S  s   g | ]\}}||gqS r   r   rG   r   r   r   
<listcomp>   s     z!Token.as_dict.<locals>.<listcomp>r   r   c              	     s    g | ]}|j  d qS ))r   rA   rB   r?   r@   )as_dict)rD   child)r?   r   rB   rA   r@   r   r   rJ      s   )r<   fieldsr0   r5   )r%   r   r?   r@   rA   rB   mappingr   )r?   r   rB   rA   r@   r%   r   rK      s    

zToken.as_dict)dctr	   c                   s*    f |}|j r& fdd|j D |_ |S )zConvert a dict to a Token.c                   s   g | ]}  |qS r   )	from_dict)rD   cclsr   r   rJ      s     z#Token.from_dict.<locals>.<listcomp>)r   )rS   rO   tokenr   rR   r   rP      s    
zToken.from_dict)__name__
__module____qualname____annotations__r<   fieldr   r   r   r   r   r   r   r   r   r    r!   r&   r/   r1   r4   r3   r6   r:   r>   rK   classmethodrP   r   r   r   r   r      s:   
	/r   )
__future__r   collections.abcr   r   dataclassesr<   typingr   r   r)   Zmarkdown_it._compatr   r   	dataclassr   r   r   r   r   <module>   s   
