U
    Sh#                     @  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 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   D/root/rtd-docs/venv/lib/python3.8/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 k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   s     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   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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)r6   r   c                 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 ]D}g | j|< | j D ].}|jsZqN|rj||jkrjqN| j| |j qNq:dS )zBuild rules lookup cache N)r<   r8   r1   addr=   appendr:   )r   chainsrB   r6   chainr   r   r   __compile__\   s    



zRuler.__compile__Nr4   zRuleOptionsType | None)ruleNamer:   optionsr   c                 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)
beforeNamerJ   r:   rK   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?   rL   Tr1   NrC   rM   r<   insertr5   r4   rN   r=   )r   rQ   rJ   r:   rK   rO   r   r   r   before   s    
 zRuler.before)	afterNamerJ   r:   rK   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?   rL   r   Tr1   NrR   )r   rU   rJ   r:   rK   rO   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   rN   r=   )r   rJ   r:   rK   r   r   r   push   s    
z
Ruler.pushFzstr | Iterable[str]r7   r0   )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   rC   rM   r<   r8   rF   r=   r   rX   rY   resultr6   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<   r8   r`   )r   rX   rY   rB   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   rZ   FNr[   r]   r   r   r   disable   s    


zRuler.disablerD   zlist[RuleFuncTv])	chainNamer   c                 C  s2   | j dkr |   | j dk	s t| 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=   rI   AssertionErrorrN   )r   rc   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   )r6   r"   rr   r   r   
<listcomp>  s     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   )r8   r6   rf   r   r   r   rh     s      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)rD   )r*   r+   r,   r   rC   rI   rP   rT   rV   rW   r`   ra   rb   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   r/   r4   r5   r;   r   r   r   r   <module>   s   