
    c,                        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dddddd	d
edededededeeegef                  deeeeeef         gef                  deeegef                  ddfdZddedededefdZ	 ddedededeeegef         fdZ e j        d          Z	 	 ddedeeegef                  deeeeegef         fdZdS )    N)AnyCallableDictOptional)
MarkdownIt)
escapeHtmlisWhiteSpace)
StateBlock)StateInlineTF)allow_labelsallow_spaceallow_digitsdouble_inlinelabel_normalizerrendererlabel_renderermdr   r   r   r   r   r   r   returnc          	         |d }| j         j                            ddt          |||                     | j        j                            ddt          ||                     |d n||d n|d	t          ffd
}d	t          ffd}	d	t          ffd}
d	t          ffd}|                     d|           |                     d|	           |                     d|
           |                     d|           dS )a  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 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                 .    t          j        dd|           S )Nz\s+-)resublabels    @lib/python3.11/site-packages/mdit_py_plugins/dollarmath/index.py<lambda>z#dollarmath_plugin.<locals>.<lambda>*   s    U)C)C     escapemath_inlinefence
math_blockc                      t          |           S )N)r   )content_s     r   r   z#dollarmath_plugin.<locals>.<lambda>:   s    z'':': r   c                     d|  dS )Nz
<a href="#u=   " class="mathlabel" title="Permalink to this equation">¶</a> r   s    r   r   z#dollarmath_plugin.<locals>.<lambda>@   s    kukkk r   r   c                      t          ||         j                                                  ddi          }d| dS )Ndisplay_modeFz<span class="math inline">z</span>strr$   stripselftokensidxoptionsenvr$   	_renderers         r   render_math_inlinez-dollarmath_plugin.<locals>.render_math_inlineE   sE    )Cs 344::<<~u>UVV<G<<<<r   c                      t          ||         j                                                  ddi          }d| dS )Nr)   Tz<div class="math inline">z</div>r*   r-   s         r   render_math_inline_doublez4dollarmath_plugin.<locals>.render_math_inline_doubleI   sE    )Cs 344::<<~t>TUU:7::::r   c                      t          ||         j                                                  ddi          }d| dS )Nr)   Tz<div class="math block">

</div>
r*   r-   s         r   render_math_blockz,dollarmath_plugin.<locals>.render_math_blockM   sE    )Cs 344::<<~t>TUU?G????r   c                      	t          ||         j                                                  ddi          }||         j        } ||         j                  }d| d| d| dS )Nr)   Tz	<div id="z" class="math block">

r8   )r+   r$   r,   info)
r.   r/   r0   r1   r2   r$   _idr   _label_rendererr3   s
           r   render_math_block_labelz2dollarmath_plugin.<locals>.render_math_block_labelQ   sw    )Cs 344::<<~t>TUUSks 011S3SSuSSSSSSr   math_inline_doublemath_block_label)inlinerulerbeforemath_inline_dollarblockmath_block_dollarr+   add_render_rule)r   r   r   r   r   r   r   r   r4   r6   r9   r?   r>   r3   s               @@r   dollarmath_pluginrI   
   s   >  DCCIO;mDD  
 HN0?OPP    ::			 )kk 	 )=s = = = = = =;c ; ; ; ; ; ;@c @ @ @ @ @ @TC T T T T T T T }&8999+-FGGG|%6777)+BCCCCCr   stateback_posmodc                 z    d}|dk    r#|dz
  }| j         |         dk    r|dz  }nn|dk    #|sdS |dz  |k    rdS dS )zTest if dollar is escaped.r      \   F   T)srcCharCode)rJ   rK   rL   backslashess       r   
is_escapedrS   ^   s|     K
a- a<X&$. 	1KK a-   u 	aC t5r   allow_doublec                 F     dt           dt          dt          f f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

    rJ   silentr   c                    | j         | j                 dk    rdS 
s7	 t          | j         | j        dz                      rdS n# t          $ r Y dS w xY ws;	 | j        | j        dz
                                           rdS n# t          $ r Y nw xY wt          | | j                  rdS 	 	o| j         | j        dz            dk    }n# t          $ r Y dS w xY w| j        dz   |rdndz   }d}|s~	 | j                             d|          }n# t          $ r Y dS w xY wt          | |          r|dz   }F	 |r| j         |dz            dk    s|dz   }cn# t          $ r Y dS w xY w|r|dz  }d}|~|sdS 
s2	 t          | j         |dz
                     rdS n# t          $ r Y dS w xY ws6	 | j        |dz                                            rdS n# t          $ r Y nw xY w|r| j        | j        dz   |dz
           n| j        | j        dz   |         }|sdS |s-| 	                    |rdndd	d          }||_
        |rd
nd|_        |dz   | _        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
        $   FrN   r   TrP   r@   r    math$$$)rQ   posr	   
IndexErrorsrcisdigitrS   index
ValueErrorpushr$   markup)rJ   rV   	is_doubler\   found_closingendtexttokenr   rT   r   s           r   _math_inline_dollarz/math_inline_dollar.<locals>._math_inline_dollar   s   & UY'4/ 	5 	 1%)a- @AA ! 5!   uu  	9UY]+3355 ! 5!    eUY'' 	5	$Q):59q=)IT)QII 	 	 	55	 i!mI4qq15 	!'--dC88   uu %%% Ag U%6sQw%?4%G 'C   uu  q M)   	!,  	5 	 1#' :;; ! 5!   uu  	9S1W%--// ! 5!   
 0EIei!mcAg-..59q=3./ 	  	5 	6JJ(1D$$}fa E !EM#,544#EL!G	ts}   "A 
AA'B   
BB(C 
CC+D 
DD/E 
EE/F 
FF#"G 
GG)r   bool)r   r   rT   ri   s   ``` r   rE   rE   s   sV    m; m m m m m m m m m m^ r   z^\s*\)([^)$\r\n]+?)\(\s*\${2}c           
      ^     dt           dt          dt          dt          dt          f
 fd}|S )zGenerate block dollar rule.rJ   	startLineendLinerV   r   c                    d}| j         |         | j        |         z   }| j        |         }| j        |         | j        z
  dk    rdS |dz   |k    rdS | j        |         dk    s| j        |dz            dk    rdS |}d }| j        ||         }	t          |	                                          dk    r|	                                	                    d          r:d}|dz
  t          |	          t          |	                                          z
  z
  }n^r\t                              |	d d d	                   }
|
r7d}|
                    d          d d d	         }||
                                z
  }|s	 |dz  }||k    rn| j         |         | j        |         z   }| j        |         }||z
  dk     r@| j        ||         }	|	                                	                    d          r:d}|dz
  t          |	          t          |	                                          z
  z
  }nar]t                              |	d d d	                   }
|
r8d}|
                    d          d d d	         }||
                                z
  }n|sdS ||rdnd
z   | _        |                     |rdnddd
          }d|_        | j        |dz   |         |_        d|_        || j        g|_        |r|n
 |          |_        dS )NF   rP   rX   rN      rZ   Tr   rA   r"   rY   )bMarkstShifteMarkssCount	blkIndentrQ   r^   lenr,   endswithDOLLAR_EQNO_REVmatchgrouprf   linerb   rF   r$   rc   mapr<   )rJ   rl   rm   rV   haveEndMarkerstartPosrf   nextLiner   lineText	eqnoMatchstartrh   r   r   s                r   _math_block_dollarz-math_block_dollar.<locals>._math_block_dollar   s[    <	*U\)-DDl9% <	"U_49 	5a<# 	5 h'4/	 A.$6	 5  9Xc\*x~~  1$ 	0~~((.. 	0 $AgXX^^5E5E1F1F!FG 0+11(44R4.AA	 0$(M%OOA..ttt4E	/C  	Aw& X.h1GGl8,;?  9U3Y/>>##,,T22 $(M'S]]S9I9I5J5J%JKC    / 5 5htttn E EI  (, ) 2 244R4 8!IMMOO336  	5m!:;


H--L&RSTT	(Q,"45
+	 	X"2W@P@PQV@W@WEJtr   )r
   intrj   )r   r   r   s   `` r   rG   rG      sd    TT&)T47TAET	T T T T T T Tl r   )r   )TTF)TN)r   typingr   r   r   r   markdown_itr   markdown_it.common.utilsr   r	   markdown_it.rules_blockr
   markdown_it.rules_inliner   rj   r+   rI   r   rS   rE   compilery   rG   r'   r   r   <module>r      s|   				 0 0 0 0 0 0 0 0 0 0 0 0 " " " " " " = = = = = = = = . . . . . . 0 0 0 0 0 0 7;?C59QD QD QDQD QD 	QD
 QD QD xs
34QD xd38n 5s :;<QD XseSj12QD 
QD QD QD QDh k S s 4    , OT} }},0}GK}{D!4'(} } } }B "*=>> 7;\ \\xs
34\ z3T*D01\ \ \ \ \ \r   