o
    +e(+                  
   @  s2  d Z ddl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mZ erEdd
lmZ ddlmZ ddlZddlm  mZ dZdZdZdZdddddddddd	Zde Zde ZdZdZdZd e Zd!Z d"e Z!d#e Z"d$e Z#d%Z$d&Z%d'Z&ed( Z'G d)d* d*eZ(G d+d, d,eZ)d-d. Z*dS )/z
Adds conversion of ASCII dashes, quotes and ellipses to their HTML
entity equivalents.

See the [documentation](https://Python-Markdown.github.io/extensions/smarty)
for details.
    )annotations   )	Extension   )HtmlInlineProcessorHTML_RE)InlineProcessor)Registry)TYPE_CHECKINGSequence)Markdown)inlinepatternsNz)[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]z[\s.,;:!?)]z[^\ \t\r\n\[\{\(\-\u0002\u0003]u1   (\s|&nbsp;|--|–|—|&[mn]dash;|&#8211;|&#8212;)z&mdash;z&ndash;z&hellip;z&laquo;z&raquo;z&lsquo;z&rsquo;z&ldquo;z&rdquo;)	mdashndashellipsisleft-angle-quoteright-angle-quoteleft-single-quoteright-single-quoteleft-double-quoteright-double-quotez
^'(?=%s\B)z
^"(?=%s\B)z"'(?=\w)z'"(?=\w)z(?<!\w)'(?=\d{2}s)z	%s"(?=\w)z"(?=\s)z(?<=%s)"z	%s'(?=\w)z(?<=%s)'(?!\s|s\b|\d)z	'(\s|s\b)'"z(?!\>)c                   @  s    e Zd ZdddZdddZdS )SubstituteTextPatternpatternstrreplace#Sequence[int | str | etree.Element]mdr   c                 C  s   t | | || _|| _dS )z" Replaces matches with some text. N)r   __init__r   r   )selfr   r   r    r!   :lib/python3.10/site-packages/markdown/extensions/smarty.pyr      s   
zSubstituteTextPattern.__init__mre.Match[str]datareturntuple[str, int, int]c                 C  sR   d}| j D ]}t|tr|||7 }q|| jj|7 }q||d|dfS )N r   )	r   
isinstanceintgroupr   Z	htmlStashstorestartend)r    r#   r%   resultpartr!   r!   r"   handleMatch   s   

z!SubstituteTextPattern.handleMatchN)r   r   r   r   r   r   )r#   r$   r%   r   r&   r'   )__name__
__module____qualname__r   r1   r!   r!   r!   r"   r      s    
r   c                      sZ   e Zd ZdZ f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
  ZS )SmartyExtensionz Add Smarty to Markdown. c                   s^   ddgddgddgddgi dgd| _ 	 t jdi | tt| _| j| jd	i d
 d S )NTzEducate quotesFzEducate angled quoteszEducate dasheszEducate ellipseszOverwrite default substitutions)smart_quotessmart_angled_quotessmart_dashessmart_ellipsessubstitutionsr:   )defaultr!   )Zconfigsuperr   dictr:   updateZ	getConfig)r    kwargs	__class__r!   r"   r      s   
zSmartyExtension.__init__r   r   patterns9Sequence[tuple[str, Sequence[int | str | etree.Element]]]serier   priorityr*   c                 C  sH   t |D ]\}}||f7 }t| }d||f }| j||||  qd S )Nzsmarty-%s-%d)	enumerater   inlinePatternsregister)r    r   rB   rD   rE   Zindr   namer!   r!   r"   _addPatterns   s   
zSmartyExtension._addPatternsr&   Nonec                 C  L   t d| jd f|}t d| jd f|}| j|dd | j|dd d S )	Nz(?<!-)---(?!-)r   z(?<!-)--(?!-)r   zsmarty-em-dashes2   zsmarty-en-dashes-   r   r:   rG   rH   )r    r   ZemDashesPatternZenDashesPatternr!   r!   r"   educateDashes      zSmartyExtension.educateDashesc                 C  s(   t d| jd f|}| j|dd d S )Nz(?<!\.)\.{3}(?!\.)r   zsmarty-ellipses
   rO   )r    r   ZellipsesPatternr!   r!   r"   educateEllipses   s   zSmartyExtension.educateEllipsesc                 C  rL   )	Nz\<\<r   z\>\>r   zsmarty-left-angle-quotes(   zsmarty-right-angle-quotes#   rO   )r    r   ZleftAngledQuotePatternZrightAngledQuotePatternr!   r!   r"   educateAngledQuotes   rQ   z#SmartyExtension.educateAngledQuotesc                 C  s   | j d }| j d }| j d }| j d }t|fft|fft|| fft|| fft|fftd|fft|fft|dfft	|fft
d|fft|fft|fft|fff}| ||dd d S )Nr   r   r   r   r   Zquotes   )r:   singleQuoteStartRedoubleQuoteStartRedoubleQuoteSetsResingleQuoteSetsRedecadeAbbrReopeningSingleQuotesRegexclosingSingleQuotesRegexclosingSingleQuotesRegex2remainingSingleQuotesRegexopeningDoubleQuotesRegexclosingDoubleQuotesRegexclosingDoubleQuotesRegex2remainingDoubleQuotesRegexrJ   )r    r   ZlsquoZrsquoZldquoZrdquorB   r!   r!   r"   educateQuotes   s&   






zSmartyExtension.educateQuotesc                 C  s   |   }t | _|d r| | |d r| | |d r.| | |jtt|dd |d r7| 	| t
|}| j|_|j|dd |jd	d
g d S )Nr9   r6   r7   ZhtmlZ   r8   Zsmarty   r   r   )Z
getConfigsr	   rG   rS   re   rV   rH   r   HTML_STRICT_RErP   r   treeprocessorsZESCAPED_CHARSextend)r    r   ZconfigsZinlineProcessorr!   r!   r"   extendMarkdown   s   



zSmartyExtension.extendMarkdown)r   r   rB   rC   rD   r   rE   r*   )r   r   r&   rK   )r2   r3   r4   __doc__r   rJ   rP   rS   rV   re   rk   __classcell__r!   r!   r@   r"   r5      s    






r5   c                  K  s   t di | S )Nr!   )r5   )r?   r!   r!   r"   makeExtension  s   rn   )+rl   Z
__future__r   r(   r   r   r   r   ri   r   utilr	   typingr
   r   Zmarkdownr   reZxml.etree.ElementTreeZetreeZElementTreeZ
punctClassZendOfWordClassZ
closeClassZopeningQuotesBaser:   rX   rY   rZ   r[   r\   ra   rb   rc   r]   r^   r_   r`   rd   rh   r   r5   rn   r!   r!   r!   r"   <module>   sX   N`