
    dd                         d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZ ddlmZ erddlmZ  G d d	          ZeZ edi e G d
 d                      Z G d d          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)CallableIterableMutableMapping)	dataclassfield)TYPE_CHECKING)DATACLASS_KWARGS)
MarkdownItc                  \    e Zd ZU ded<   dd	Zedd            Zej        dd            ZdS )	StateBaseztuple[int, ...]srcCharCodesrcstrmdr   envr   c                0    || _         || _        || _        d S N)r   r   r   )selfr   r   r   s       1lib/python3.11/site-packages/markdown_it/ruler.py__init__zStateBase.__init__!   s        returnc                    | j         S r   )_srcr   s    r   r   zStateBase.src&   s
    yr   valueNonec                Z    || _         t          d | j        D                       | _        d S )Nc              3  4   K   | ]}t          |          V  d S r   )ord).0cs     r   	<genexpr>z StateBase.src.<locals>.<genexpr>-   s(       : :AQ : : : : : :r   )r   tupler   r   )r   r   s     r   r   zStateBase.src*   s0    	  : : : : :::r   N)r   r   r   r   r   r   )r   r   )r   r   r   r   )__name__
__module____qualname____annotations__r   propertyr   setter r   r   r   r      sx                
    X 	Z; ; ; Z; ; ;r   r   c                  P    e Zd ZU ded<   ded<    ed          Zded<   d	ed
<   dS )Ruler   nameboolenabledF)reprRuleFuncfn	list[str]altN)r&   r'   r(   r)   r   r4   r,   r   r   r.   r.   7   sI         IIIMMM5e$$$B$$$$NNNNNr   r.   c                      e Zd Zd Zd!dZd"dZd#d$dZd#d%dZd#d&dZd#d$dZ	d'd(dZ
d'd(dZd'd(dZd)dZd*dZd*d Zd	S )+Rulerc                "    g | _         d | _        d S r   )	__rules__	__cache__r   s    r   r   zRuler.__init__@   s    %' <@r   r/   r   r   intc                X    t          | j                  D ]\  }}|j        |k    r|c S dS )zFind rule index by name)	enumerater:   r/   )r   r/   irules       r   __find__zRuler.__find__H   s?     00 	 	GAtyD   !rr   r   c                    dh}| j         D ])}|j        s
|j        D ]}|                    |           *i | _        |D ]O}g | j        |<   | j         D ];}|j        s
|r
||j        vr| j        |                             |j                   <PdS )zBuild rules lookup cache N)r:   r1   r6   addr;   appendr4   )r   chainsrA   r/   chains        r   __compile__zRuler.__compile__O   s    N 	! 	!D<  ! !

4    ! 	6 	6E$&DN5! 6 6|  e4833u%,,TW55556	6 	6r   NruleNamer4   r3   c                    |                      |          }|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: r6   N)rB   KeyErrorr:   r4   getr6   r;   )r   rJ   r4   optionsindexs        r   atzRuler.atb   su     h''-RB;;?X??@@@#%u $+KKr$:$:u!r   
beforeNamec                    |                      |          }|pi }|dk    rt          d|           | j                            |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>   rL   Tr6   NrB   rM   r:   insertr.   rN   r;   )r   rR   rJ   r4   rO   rP   s         r   beforezRuler.beforer   s~     j))-RB;;AZAABBBeT(D"gkk%QS>T>T%U%UVVVr   	afterNamec                    |                      |          }|pi }|dk    rt          d|           | j                            |dz   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>   rL      Tr6   NrT   )r   rW   rJ   r4   rO   rP   s         r   afterzRuler.after   s     i((-RB;;@Y@@AAAAItHdBE20F0FGG	
 	
 	
 r   c                    | j                             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).

        Tr6   N)r:   rF   r.   rN   r;   )r   rJ   r4   rO   s       r   pushz
Ruler.push   sH     	d8T227J7J5RT7U7UVVWWWr   Fnamesstr | Iterable[str]ignoreInvalidr0   c                   t          |t                    r|g}g }|D ]a}|                     |          }|dk     r|r |dk     r|st          d|           d| j        |         _        |                    |           bd| _        |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   rB   rM   r:   r1   rF   r;   r   r]   r_   resultr/   idxs         r   enablezRuler.enable   s     eS!! 	GE 	  	 D--%%Ca]aI4IIJJJ*.DN3'MM$r   c                    t          |t                    r|g}| j        D ]	}d|_        
|                     ||           d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
        FN)rc   r   r:   r1   rg   )r   r]   r_   rA   s       r   
enableOnlyzRuler.enableOnly   sQ     eS!! 	GEN 	! 	!D DLLE=)))))r   c                   t          |t                    r|g}g }|D ]a}|                     |          }|dk     r|r |dk     r|st          d|           d| j        |         _        |                    |           bd| _        |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   ra   FNrb   rd   s         r   disablezRuler.disable   s     eS!! 	GE 	  	 D--%%Ca]aI4IIJJJ*/DN3'MM$r   	chainNamelist[RuleFunc]c                    | j         |                                  | j         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.

        )r;   rI   rN   )r   rl   s     r   getRuleszRuler.getRules   sI     >!>---~!!)R006B6r   r5   c                $    d | j         D             S )z Return all available rule names.c                    g | ]	}|j         
S r,   )r/   r"   rs     r   
<listcomp>z'Ruler.get_all_rules.<locals>.<listcomp>   s    ///1///r   r:   r   s    r   get_all_ruleszRuler.get_all_rules   s    //////r   c                $    d | j         D             S )zReturn the active rule names.c                *    g | ]}|j         	|j        S r,   )r1   r/   rr   s     r   rt   z*Ruler.get_active_rules.<locals>.<listcomp>   s!    <<<1!)<<<<r   ru   r   s    r   get_active_ruleszRuler.get_active_rules   s    <<<<<<r   )r/   r   r   r<   )r   r   r   )rJ   r   r4   r3   )rR   r   rJ   r   r4   r3   )rW   r   rJ   r   r4   r3   )F)r]   r^   r_   r0   )rl   r   r   rm   )r   r5   )r&   r'   r(   r   rB   rI   rQ   rV   rZ   r\   rg   ri   rk   ro   rv   ry   r,   r   r   r8   r8   ?   s9       @ @ @   6 6 6 6&              $	 	 	 	 	    ,* * * * *    ,7 7 7 70 0 0 0= = = = = =r   r8   Nr,   )__doc__
__future__r   collections.abcr   r   r   dataclassesr   r   typingr	   markdown_it._compatr
   markdown_itr   r   r3   r.   r8   r,   r   r   <module>r      sH   " # " " " " " > > > > > > > > > > ( ( ( ( ( ( ( (             0 0 0 0 0 0 '&&&&&&; ; ; ; ; ; ; ;,         n= n= n= n= n= n= n= n= n= n=r   