U
    Sh0                     @  s  d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZ erd d	lmZ d d
lmZ d dlmZmZ dddddddddddddddddddd
ddZd%dddddddZd&dddddddZed Zd'dddd!d"d#d$ZdS )(    )annotationsN)TYPE_CHECKINGAnyCallableSequence)
MarkdownIt)
escapeHtmlisWhiteSpace)
StateBlock)StateInline)is_code_block)RendererProtocol)Token)EnvTypeOptionsDictTF)allow_labelsallow_spaceallow_digitsallow_blank_linesdouble_inlinelabel_normalizerrendererlabel_rendererr   boolzCallable[[str], str] | Nonez+Callable[[str, dict[str, Any]], str] | NoneNone)
mdr   r   r   r   r   r   r   r   returnc                  s  |dkrdd }| j jddt||| | jjddt||| |dkrTdd n||dkrjd	d  n| d
ddddddfdd}	d
ddddddfdd}
d
ddddddfdd}d
dddddd fdd}| d|	 | d|
 | d| | d| dS )aa  Plugin for parsing dollar enclosed math,
    e.g. inline: ``$a=1$``, block: ``$$b=2$$``

    This is an improved version of ``texmath``; it is more performant,
    and handles ``\`` escaping properly and allows for more configuration.

    :param allow_labels: Capture math blocks with label suffix, e.g. ``$$a=1$$ (eq1)``
    :param allow_space: Parse inline math when there is space
        after/before the opening/closing ``$``, e.g. ``$ a $``
    :param allow_digits: Parse inline math when there is a digit
        before/after the opening/closing ``$``, e.g. ``1$`` or ``$2``.
        This is useful when also using currency.
    :param allow_blank_lines: Allow blank lines inside ``$$``. Note that blank lines are
        not allowed in LaTeX, executablebooks/markdown-it-dollarmath, or the Github or
        StackExchange markdown dialects. Hoever, they have special semantics if used
        within Sphinx `..math` admonitions, so are allowed for backwards-compatibility.
    :param double_inline: Search for double-dollar math within inline contexts
    :param label_normalizer: Function to normalize the label,
        by default replaces whitespace with `-`
    :param renderer: Function to render content: `(str, {"display_mode": bool}) -> str`,
        by default escapes HTML
    :param label_renderer: Function to render labels, by default creates anchor

    Nc                 S  s   t dd| S )Nz\s+-)resublabel r"   S/root/rtd-docs/venv/lib/python3.8/site-packages/mdit_py_plugins/dollarmath/index.py<lambda>8       z#dollarmath_plugin.<locals>.<lambda>escapemath_inlineZfence
math_blockc                 S  s   t | S )N)r   )content_r"   r"   r#   r$   J   r%   c                 S  s   d|  dS )Nz
<a href="#u=   " class="mathlabel" title="Permalink to this equation">¶</a>r"   r    r"   r"   r#   r$   P   r%   r   zSequence[Token]intr   r   str)selftokensidxoptionsenvr   c                   s(    t || j ddi}d| dS )Ndisplay_modeFz<span class="math inline">z</span>r,   r)   stripr-   r.   r/   r0   r1   r)   	_rendererr"   r#   render_math_inlineU   s    z-dollarmath_plugin.<locals>.render_math_inlinec                   s(    t || j ddi}d| dS )Nr2   Tz<div class="math inline">z</div>r3   r5   r6   r"   r#   render_math_inline_double_   s    z4dollarmath_plugin.<locals>.render_math_inline_doublec                   s(    t || j ddi}d| dS )Nr2   Tz<div class="math block">

</div>
r3   r5   r6   r"   r#   render_math_blocki   s    z,dollarmath_plugin.<locals>.render_math_blockc                   sL   t || j ddi}|| j} || j}d| d| d| dS )Nr2   Tz	<div id="z" class="math block">

r:   )r,   r)   r4   info)r-   r.   r/   r0   r1   r)   Z_idr!   Z_label_rendererr7   r"   r#   render_math_block_labels   s    
z2dollarmath_plugin.<locals>.render_math_block_labelmath_inline_doublemath_block_label)inlineZrulerbeforemath_inline_dollarblockmath_block_dollarZadd_render_rule)r   r   r   r   r   r   r   r   r   r8   r9   r;   r?   r"   r>   r#   dollarmath_plugin   s4    $




rG   r   r+   )stateback_posmodr   c                 C  sL   d}|dkr0|d }| j | dkr0|d7 }qq0q|s8dS |d |krHdS dS )zTest if dollar is escaped.r      \F   T)src)rH   rI   rJ   backslashesr"   r"   r#   
is_escaped   s    
rP   z#Callable[[StateInline, bool], bool])r   r   allow_doubler   c                   s   dddd fdd}|S )a  Generate inline dollar rule.

    :param allow_space: Parse inline math when there is space
        after/before the opening/closing ``$``, e.g. ``$ a $``
    :param allow_digits: Parse inline math when there is a digit
        before/after the opening/closing ``$``, e.g. ``1$`` or ``$2``.
        This is useful when also using currency.
    :param allow_double: Search for double-dollar math within inline contexts

    r   r   )rH   silentr   c                   s  | j | j dkrdS sRz"tt| j | jd  r8W dS W n tk
rP   Y dS X  sz| j | jd   rrW dS W n tk
r   Y nX t| | jrdS zo| j | jd  dk}W n tk
r   Y dS X | jd |rdnd }d}|sz| j d|}W n tk
r   Y dS X t| |r0|d }qz*|rX| j |d  dkrX|d }W qW n tk
rr   Y dS X |r|d7 }d}q|sdS sz"tt| j |d  rW dS W n tk
r   Y dS X  sz| j |d   rW dS W n tk
r   Y nX |r.| j | jd |d  n| j | jd | }|sLdS |s| 	|r`dndd	d}||_
|rzd
nd|_|d | _dS )a\  Inline dollar rule.

        - Initial check:
            - check if first character is a $
            - check if the first character is escaped
            - check if the next character is a space (if not allow_space)
            - check if the next character is a digit (if not allow_digits)
        - Advance one, if allow_double
        - Find closing (advance one, if allow_double)
        - Check closing:
            - check if the previous character is a space (if not allow_space)
            - check if the next character is a digit (if not allow_digits)
        - Check empty content
        $FrK   r   TrM   r@   r'   math$$)rN   posr	   ord
IndexErrorisdigitrP   index
ValueErrorpushr)   markup)rH   rR   Z	is_doublerV   Zfound_closingendtexttokenr   rQ   r   r"   r#   _math_inline_dollar   s    



  
z/math_inline_dollar.<locals>._math_inline_dollarr"   )r   r   rQ   rb   r"   ra   r#   rD      s    orD   z^\s*\)([^)$\r\n]+?)\(\s*\${2}z,Callable[[StateBlock, int, int, bool], bool])r   r   r   r   c                   s"   dddddd fdd}|S )zGenerate block dollar rule.r
   r+   r   )rH   	startLineendLinerR   r   c                   s^  t | |rdS d}| j| | j|  }| j| }|d |kr@dS | j| dks`| j|d  dkrddS |}d }| j|| }	t|	 dkr|	 drd}|d t|	t|	   }n@rt	|	d d d }
|
rd}|

dd d d }||
  }|s|d7 }||krq| j| | j|  }| j| }| j|| }	|	 drvd}|d t|	t|	   }q|	 d	kr sqڈr t	|	d d d }
|
r d}|

dd d d }||
  }qڐq |sdS ||rdnd
 | _| |rdnddd
}d|_| j|d | |_d|_|| jg|_|rZd krP|n||_dS )NFrM   rS   rK      rU   T r   rA   r(   rT   )r   ZbMarksZtShiftZeMarksrN   lenr4   endswithDOLLAR_EQNO_REVmatchgroupr^   liner\   rE   r)   r]   mapr=   )rH   rc   rd   rR   ZhaveEndMarkerZstartPosr^   ZnextLiner!   ZlineTextZ	eqnoMatchstartr`   r   r   r   r"   r#   _math_block_dollar&  sj    

 

z-math_block_dollar.<locals>._math_block_dollarr"   )r   r   r   rq   r"   rp   r#   rF     s    OrF   )r   )TTF)TNF) 
__future__r   r   typingr   r   r   r   Zmarkdown_itr   Zmarkdown_it.common.utilsr   r	   Zmarkdown_it.rules_blockr
   Zmarkdown_it.rules_inliner   Zmdit_py_plugins.utilsr   Zmarkdown_it.rendererr   Zmarkdown_it.tokenr   Zmarkdown_it.utilsr   r   rG   rP   rD   compilerj   rF   r"   r"   r"   r#   <module>   s>   $s      
   