U
    \>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rd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                   @  s0   e Zd ZddddddZdddd	d
dZdS )SubstituteTextPatternstrz#Sequence[int | str | etree.Element]r   )patternreplacemdc                 C  s   t | | || _|| _dS )z" Replaces matches with some text. N)r   __init__r   r   )selfr   r   r    r    >/tmp/pip-unpacked-wheel-wj0og6ym/markdown/extensions/smarty.pyr      s    zSubstituteTextPattern.__init__zre.Match[str]ztuple[str, int, int])mdatareturnc                 C  sR   d}| j D ]0}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)__name__
__module____qualname__r   r.   r    r    r    r!   r      s   r   c                      s|   e Zd ZdZ fddZdddddd	d
ZdddddZdddddZdddddZdddddZ	dd Z
  ZS )SmartyExtensionz Add Smarty to Markdown. c                   sX   ddgddgddgddgi dgd| _ t jf | 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substitutionsr7   )default)configsuperr   dictr7   updateZ	getConfig)r   kwargs	__class__r    r!   r      s    
zSmartyExtension.__init__r   z9Sequence[tuple[str, Sequence[int | str | etree.Element]]]r   r'   )r   patternsseriepriorityc                 C  sH   t |D ]:\}}||f7 }t| }d||f }| j||||  qd S )Nzsmarty-%s-%d)	enumerater   inlinePatternsregister)r   r   r@   rA   rB   indr   namer    r    r!   _addPatterns   s
    
zSmartyExtension._addPatternsNone)r   r$   c                 C  sL   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   r7   rD   rE   )r   r   ZemDashesPatternZenDashesPatternr    r    r!   educateDashes   s     
  
 zSmartyExtension.educateDashesc                 C  s(   t d| jd f|}| j|dd d S )Nz(?<!\.)\.{3}(?!\.)r   zsmarty-ellipses
   rL   )r   r   ZellipsesPatternr    r    r!   educateEllipses   s     
 zSmartyExtension.educateEllipsesc                 C  sL   t d| jd f|}t d| jd f|}| j|dd | j|dd d S )	Nz\<\<r   z\>\>r   zsmarty-left-angle-quotes(   zsmarty-right-angle-quotes#   rL   )r   r   ZleftAngledQuotePatternZrightAngledQuotePatternr    r    r!   educateAngledQuotes   s     
  
 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   quotes   )r7   singleQuoteStartRedoubleQuoteStartRedoubleQuoteSetsResingleQuoteSetsRedecadeAbbrReopeningSingleQuotesRegexclosingSingleQuotesRegexclosingSingleQuotesRegex2remainingSingleQuotesRegexopeningDoubleQuotesRegexclosingDoubleQuotesRegexclosingDoubleQuotesRegex2remainingDoubleQuotesRegexrH   )r   r   lsquorsquoldquordquor@   r    r    r!   educateQuotes   s&    






zSmartyExtension.educateQuotesc                 C  s   |   }t | _|d r"| | |d r4| | |d r\| | |jtt|dd |d rn| 	| t
|}| j|_|j|dd |jd	d
g d S )Nr6   r3   r4   htmlZ   r5   Zsmartyr   r   r   )Z
getConfigsr	   rD   rO   rf   rR   rE   r   HTML_STRICT_RErM   r   treeprocessorsZESCAPED_CHARSextend)r   r   ZconfigsZinlineProcessorr    r    r!   extendMarkdown   s    



zSmartyExtension.extendMarkdown)r/   r0   r1   __doc__r   rH   rM   rO   rR   rf   rl   __classcell__r    r    r>   r!   r2      s   

r2   c                  K  s
   t f | S )N)r2   )r=   r    r    r!   makeExtension  s    ro   )+rm   
__future__r   r%   r   r   r   r   rj   r   utilr	   typingr
   r   Zmarkdownr   rexml.etree.ElementTreeetreeElementTreeZ
punctClassZendOfWordClassZ
closeClassZopeningQuotesBaser7   rU   rV   rW   rX   rY   r^   r_   r`   rZ   r[   r\   r]   ra   ri   r   r2   ro   r    r    r    r!   <module>O   sV   `