a
    'ñ»jï#  ã                   @  sØ   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ ddlZddlmZ dd	lmZ e	rtdd
lmZ G dd„ dƒZG dd„ dedd�ZedƒZef i e¤ŽG dd„ de
e ƒƒZG dd„ de
e ƒZdS )aü  
class Ruler

Helper class, used by [[MarkdownIt#core]], [[MarkdownIt#block]] and
[[MarkdownIt#inline]] to manage sequences of functions (rules):

- keep rules in defined order
- assign the name to each rule
- enable/disable rules
- add/replace rules
- allow assign rules to additional named chains (in the same)
- caching lists of active rules

You will not need use this class directly until write plugins. For simple
rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
[[MarkdownIt.use]].
é    )Úannotations)ÚIterable)Ú	dataclassÚfield)ÚTYPE_CHECKINGÚGenericÚ	TypedDictÚTypeVarN)ÚDATACLASS_KWARGSé   )ÚEnvType)Ú
MarkdownItc                   @  sX   e Zd Zddddœdd„Zeddœdd	„ƒZejdd
dœdd	„ƒZeddœdd„ƒZdS )Ú	StateBaseÚstrr   r   )ÚsrcÚmdÚenvc                 C  s   || _ || _|| _d S ©N)r   r   r   )Úselfr   r   r   © r   ú7E:\soccer\ml_env\Lib\site-packages\markdown_it/ruler.pyÚ__init__"   s    zStateBase.__init__©Úreturnc                 C  s   | j S r   )Ú_src©r   r   r   r   r   '   s    zStateBase.srcÚNone)Úvaluer   c                 C  s   || _ d | _d S r   )r   Ú_srcCharCode)r   r   r   r   r   r   +   s    ztuple[int, ...]c                 C  s6   t jdtdd� | jd u r0tdd„ | jD ƒƒ| _| jS )Nz?StateBase.srcCharCode is deprecated. Use StateBase.src instead.é   )Ú
stacklevelc                 s  s   | ]}t |ƒV  qd S r   )Úord)Ú.0Úcr   r   r   Ú	<genexpr>8   ó    z(StateBase.srcCharCode.<locals>.<genexpr>)ÚwarningsÚwarnÚDeprecationWarningr   Útupler   r   r   r   r   ÚsrcCharCode0   s    ý
zStateBase.srcCharCodeN)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   Úsetterr*   r   r   r   r   r   !   s   r   c                   @  s   e Zd ZU ded< dS )ÚRuleOptionsTypeú	list[str]ÚaltN)r+   r,   r-   Ú__annotations__r   r   r   r   r0   <   s   
r0   F)ÚtotalÚ
RuleFuncTvc                   @  s8   e Zd ZU ded< ded< edd�Zded< d	ed
< dS )ÚRuler   ÚnameÚboolÚenabledF)Úreprr5   Úfnr1   r2   N)r+   r,   r-   r3   r   r;   r   r   r   r   r6   D   s   
r6   c                   @  sþ   e Zd Zddœdd„Zdddœdd	„Zddœd
d„Zd.dddddœdd„Zd/ddddddœdd„Zd0ddddddœdd„Zd1dddddœdd„Z	d2ddddœdd „Z
d3ddddœd!d"„Zd4ddddœd#d$„Zd5dd&d'œd(d)„Zddœd*d+„Zddœd,d-„ZdS )6ÚRulerr   r   c                 C  s   g | _ d | _d S r   )Ú	__rules__Ú	__cache__r   r   r   r   r   M   s    zRuler.__init__r   Úint)r7   r   c                 C  s*   t | jƒD ]\}}|j|kr
|  S q
dS )zFind rule index by nameéÿÿÿÿ)Ú	enumerater=   r7   )r   r7   ÚiÚruler   r   r   Ú__find__U   s    

zRuler.__find__c                 C  s„   dh}| j D ]"}|jsq|jD ]}| |¡ qqi | _|D ]D}g | j|< | j D ].}|jsZqN|rj||jvrjqN| j|  |j¡ qNq:dS )zBuild rules lookup cacheÚ N)r=   r9   r2   Úaddr>   Úappendr;   )r   ÚchainsrC   r7   Úchainr   r   r   Ú__compile__\   s    



zRuler.__compile__Nr5   zRuleOptionsType | None)ÚruleNamer;   Úoptionsr   c                 C  sR   |   |¡}|pi }|dkr(td|› �ƒ‚|| j| _| dg ¡| j| _d| _dS )zöReplace rule by name with new function & options.

        :param ruleName: rule name to replace.
        :param fn: new rule function.
        :param options: new rule options (not mandatory).
        :raises: KeyError if name not found
        r@   úParser rule not found: r2   N)rD   ÚKeyErrorr=   r;   Úgetr2   r>   )r   rK   r;   rL   Úindexr   r   r   Úato   s    

zRuler.at)Ú
beforeNamerK   r;   rL   r   c                 C  sV   |   |¡}|pi }|dkr(td|› �ƒ‚| j |tt |d|| dg ¡ƒ¡ d| _dS )aK  Add new rule to chain before one with given name.

        :param beforeName: new rule will be added before this one.
        :param ruleName: new rule will be added before this one.
        :param fn: new rule function.
        :param options: new rule options (not mandatory).
        :raises: KeyError if name not found
        r@   rM   Tr2   N©rD   rN   r=   Úinsertr6   r5   rO   r>   )r   rR   rK   r;   rL   rP   r   r   r   Úbefore�   s    
ÿzRuler.before)Ú	afterNamerK   r;   rL   r   c                 C  sZ   |   |¡}|pi }|dkr(td|› �ƒ‚| j |d tt |d|| dg ¡ƒ¡ d| _dS )aG  Add new rule to chain after one with given name.

        :param afterName: new rule will be added after this one.
        :param ruleName: new rule will be added after this one.
        :param fn: new rule function.
        :param options: new rule options (not mandatory).
        :raises: KeyError if name not found
        r@   rM   r   Tr2   NrS   )r   rV   rK   r;   rL   rP   r   r   r   Úafter™   s    
ÿzRuler.afterc              
   C  s0   | j  tt |d||pi  dg ¡ƒ¡ d| _dS )zÒPush new rule to the end of chain.

        :param ruleName: new rule will be added to the end of chain.
        :param fn: new rule function.
        :param options: new rule options (not mandatory).

        Tr2   N)r=   rG   r6   r5   rO   r>   )r   rK   r;   rL   r   r   r   Úpush±   s    
ÿz
Ruler.pushFzstr | Iterable[str]r8   r1   )ÚnamesÚignoreInvalidr   c                 C  sp   t |tƒr|g}g }|D ]L}|  |¡}|dk r4|r4q|dk rN|sNtd|› �ƒ‚d| j| _| |¡ qd| _|S )a  Enable rules with given names.

        :param names: name or list of rule names to enable.
        :param ignoreInvalid: ignore errors when rule not found
        :raises: KeyError if name not found and not ignoreInvalid
        :return: list of found rule names
        r   ú!Rules manager: invalid rule name TN©Ú
isinstancer   rD   rN   r=   r9   rG   r>   ©r   rY   rZ   Úresultr7   Úidxr   r   r   ÚenableÀ   s    


zRuler.enablec                 C  s.   t |tƒr|g}| jD ]
}d|_q|  ||¡S )a-  Enable rules with given names, and disable everything else.

        :param names: name or list of rule names to enable.
        :param ignoreInvalid: ignore errors when rule not found
        :raises: KeyError if name not found and not ignoreInvalid
        :return: list of found rule names
        F)r]   r   r=   r9   ra   )r   rY   rZ   rC   r   r   r   Ú
enableOnlyØ   s
    


zRuler.enableOnlyc                 C  sp   t |tƒr|g}g }|D ]L}|  |¡}|dk r4|r4q|dk rN|sNtd|› �ƒ‚d| j| _| |¡ qd| _|S )a  Disable rules with given names.

        :param names: name or list of rule names to enable.
        :param ignoreInvalid: ignore errors when rule not found
        :raises: KeyError if name not found and not ignoreInvalid
        :return: list of found rule names
        r   r[   FNr\   r^   r   r   r   Údisableè   s    


zRuler.disablerE   zlist[RuleFuncTv])Ú	chainNamer   c                 C  s2   | j du r |  ¡  | j dus J ‚| j  |g ¡p0g S )a=  Return array of active functions (rules) for given chain name.
        It analyzes rules configuration, compiles caches if not exists and returns result.

        Default chain name is `''` (empty string). It can't be skipped.
        That's done intentionally, to keep signature monomorphic for high speed.

        N)r>   rJ   rO   )r   rd   r   r   r   ÚgetRules   s    
zRuler.getRulesc                 C  s   dd„ | j D ƒS )z Return all available rule names.c                 S  s   g | ]
}|j ‘qS r   )r7   ©r"   Úrr   r   r   Ú
<listcomp>  r%   z'Ruler.get_all_rules.<locals>.<listcomp>©r=   r   r   r   r   Úget_all_rules  s    zRuler.get_all_rulesc                 C  s   dd„ | j D ƒS )zReturn the active rule names.c                 S  s   g | ]}|j r|j‘qS r   )r9   r7   rf   r   r   r   rh     r%   z*Ruler.get_active_rules.<locals>.<listcomp>ri   r   r   r   r   Úget_active_rules  s    zRuler.get_active_rules)N)N)N)N)F)F)F)rE   )r+   r,   r-   r   rD   rJ   rQ   rU   rW   rX   ra   rb   rc   re   rj   rk   r   r   r   r   r<   L   s(    ÿ û û ÿ ÿ ÿ ÿr<   )Ú__doc__Ú
__future__r   Úcollections.abcr   Údataclassesr   r   Útypingr   r   r   r	   r&   Zmarkdown_it._compatr
   Úutilsr   Zmarkdown_itr   r   r0   r5   r6   r<   r   r   r   r   Ú<module>   s   