o
    Rh#                     @  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	r:dd
lmZ G dd dZG dd deddZedZ	 ed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                   @  sB   e Zd ZdddZedd
dZejdddZedddZdS )	StateBasesrcstrmdr   envr   c                 C  s   || _ || _|| _d S N)r   r   r   )selfr   r   r    r   S/home/air/sanwanet/backup_V2/venv/lib/python3.10/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valueNonec                 C  s   || _ d | _d S r   )r   _srcCharCode)r   r   r   r   r   r   +   s   
tuple[int, ...]c                 C  s6   t jdtdd | jd u rt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   s    z(StateBase.srcCharCode.<locals>.<genexpr>)warningswarnDeprecationWarningr   tupler   r   r   r   r   srcCharCode0   s   
zStateBase.srcCharCodeN)r   r   r   r   r   r   )r   r   )r   r   r   r   )r   r   )__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   r/   <   s   
 r/   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)reprr4   fnr0   r1   N)r*   r+   r,   r2   r   r:   r   r   r   r   r5   D   s
   
 r5   c                   @  s   e Zd Zd1ddZd2dd	Zd1d
dZ	d3d4ddZ	d3d5ddZ	d3d6ddZ	d3d4ddZ		d7d8d"d#Z
	d7d8d$d%Z	d7d8d&d'Zd9d:d+d,Zd;d-d.Zd;d/d0ZdS )<Rulerr   r   c                 C  s   g | _ d | _d S r   )	__rules__	__cache__r   r   r   r   r   M   s   
zRuler.__init__r6   r   intc                 C  s*   t | jD ]\}}|j|kr|  S qdS )zFind rule index by name)	enumerater<   r6   )r   r6   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 ]"}g | j|< | j D ]}|js-q'|r5||jvr5q'| j| |j q'qdS )zBuild rules lookup cache N)r<   r8   r1   addr=   appendr:   )r   chainsrB   r6   chainr   r   r   __compile__\   s$   



zRuler.__compile__NruleNamer:   r4   optionsRuleOptionsType | Nonec                 C  sR   |  |}|pi }|dkrtd| || j| _|dg | j| _d| _dS )zReplace 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: r1   N)rC   KeyErrorr<   r:   getr1   r=   )r   rJ   r:   rK   indexr   r   r   ato   s   


zRuler.at
beforeNamec                 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   Tr1   NrC   rN   r<   insertr5   r4   rO   r=   )r   rR   rJ   r:   rK   rP   r   r   r   before   s   

zRuler.before	afterNamec                 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   Tr1   NrS   )r   rV   rJ   r:   rK   rP   r   r   r   after   s   

zRuler.afterc              
   C  s0   | j tt |d||pi dg  d| _dS )zPush 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).

        Tr1   N)r<   rF   r5   r4   rO   r=   )r   rJ   r:   rK   r   r   r   push   s   

z
Ruler.pushFnamesstr | Iterable[str]ignoreInvalidr7   r0   c                 C  p   t |tr|g}g }|D ]&}| |}|dk r|rq|dk r'|s't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   rC   rN   r<   r8   rF   r=   r   rY   r[   resultr6   idxr   r   r   enable      


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<   r8   rc   )r   rY   r[   rB   r   r   r   
enableOnly   s
   


zRuler.enableOnlyc                 C  r\   )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   rd   zRuler.disablerD   	chainNamelist[RuleFuncTv]c                 C  s2   | j du r|   | j dusJ | j |g pg 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=   rI   rO   )r   rg   r   r   r   getRules   s   
zRuler.getRulesc                 C     dd | j D S )z Return all available rule names.c                 S  s   g | ]}|j qS r   )r6   r"   rr   r   r   
<listcomp>  s    z'Ruler.get_all_rules.<locals>.<listcomp>r<   r   r   r   r   get_all_rules     zRuler.get_all_rulesc                 C  rj   )zReturn the active rule names.c                 S  s   g | ]}|j r|jqS r   )r8   r6   rk   r   r   r   rm     s    z*Ruler.get_active_rules.<locals>.<listcomp>rn   r   r   r   r   get_active_rules  rp   zRuler.get_active_rules)r   r   )r6   r   r   r>   r   )rJ   r   r:   r4   rK   rL   r   r   )
rR   r   rJ   r   r:   r4   rK   rL   r   r   )
rV   r   rJ   r   r:   r4   rK   rL   r   r   )F)rY   rZ   r[   r7   r   r0   )rD   )rg   r   r   rh   )r   r0   )r*   r+   r,   r   rC   rI   rQ   rU   rW   rX   rc   re   rf   ri   ro   rq   r   r   r   r   r;   L   s*    



r;   r   )__doc__
__future__r   collections.abcr   dataclassesr   r   typingr   r   r   r	   r%   markdown_it._compatr
   utilsr   markdown_itr   r   r/   r4   r5   r;   r   r   r   r   <module>   s"    