
    :Qg                    &   d Z ddlmZ ddlmZ ddlm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Zddlmc mZ erddlmZ ddlmZ  G d de      Z G d de
      Z G d de      Z  ed      e      Z ed       G d de             Zd Zy)z
This extension adds abbreviation handling to Python-Markdown.

See the [documentation](https://Python-Markdown.github.io/extensions/abbreviations)
for details.
    )annotations   )	Extension   )BlockProcessor)InlineProcessor)Treeprocessor)AtomicString
deprecated)TYPE_CHECKINGN)Markdown)BlockParserc                  <     e Zd ZdZ fdZd Zd ZddZd Z xZ	S )AbbrExtensionz- Abbreviation Extension for Python-Markdown. c                X    di dgi| _         	 t        |   di | i | _        i | _        y)z( Initiate Extension and set up configs. glossaryz`A dictionary where the `key` is the abbreviation and the `value` is the definition.Default: `{}`N )configsuper__init__abbrsr   )selfkwargs	__class__s     U/var/www/html/answerous/venv/lib/python3.12/site-packages/markdown/extensions/abbr.pyr   zAbbrExtension.__init__*   sA      
 	/"6"
    c                    | j                   j                          | j                  r&| j                   j                  | j                         yy)z- Clear all previously defined abbreviations. N)r   clearr   updater   s    r   resetzAbbrExtension.reset8   s4    

MMJJdmm, r   c                8    | j                   j                          y)z, Clear all abbreviations from the glossary. N)r   r   r    s    r   reset_glossaryzAbbrExtension.reset_glossary>   s    r   c                4    |ri || j                   | _         yy)z\Adds `dictionary` to our glossary. Any abbreviations that already exist will be overwritten.N)r   )r   
dictionarys     r   load_glossaryzAbbrExtension.load_glossaryB   s    ;z;T]];DM r   c                   | j                   d   d   r!| j                  | j                   d   d          | j                  j                  | j                         |j                  |        |j                  j                  t        || j                        dd       |j                  j                  j                  t        |j                  | j                        dd       y)z6 Insert `AbbrTreeprocessor` and `AbbrBlockprocessor`. r   r   abbr      N)r   r&   r   r   r   registerExtensiontreeprocessorsregisterAbbrTreeprocessorparserblockprocessorsAbbrBlockprocessor)r   mds     r   extendMarkdownzAbbrExtension.extendMarkdownG   s    KK
#A&t{{:6q9:

$--(
T"
""#4R#DfaP
		!!**+=bii+TV\^`ar   )r%   zdict[str, str])
__name__
__module____qualname____doc__r   r!   r#   r&   r3   __classcell__r   s   @r   r   r   '   s     7-<
br   r   c                  8     e Zd ZdZdd fdZdddZd	dZ xZS )
r.   z3 Replace abbreviation text with `<abbr>` elements. c                H    ||ni | _         d | _        t        |   |       y N)r   REr   r   )r   r2   r   r   s      r   r   zAbbrTreeprocessor.__init__T   s%    $)$552
)-r   c           	     :   t        |      D ]  }| j                  ||        |j                  x}rt        t        | j                  j                  |                  D ]  }| j                  |j                  d         s"t        j                  dd| j                  |j                  d         i      }t        |j                  d            |_        ||j                         d |_        |j                  d|       |d|j                          } ||_        | |j                  r|j                  }t        |      j                  |      dz   }t        t        | j                  j                  |                  D ]  }t        j                  dd| j                  |j                  d         i      }t        |j                  d            |_        ||j                         d |_        |j                  ||       |d|j                          } ||_        yyy)zW Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags. r   r(   titleNr   )reversediter_elementtextlistr=   finditerr   groupetreeElementr
   endtailinsertstartindex)	r   elparentchildrB   mr(   rI   rL   s	            r   rA   zAbbrTreeprocessor.iter_elementY   s   b\ 	)EeR(	)77?4?d477#3#3D#9:; ,::aggaj) =='4::aggaj;Q1RSD ,QWWQZ 8DI $QUUWXDIIIa&
+D, BG"''77DL&&r*Q.Ed477#3#3D#9:; (}}Vgtzz!''!*7M-NO(4	 N	eT*JQWWY'( BG #*r   c                   | j                   syt        | j                   j                               }|j                  t        d       t        j                  ddj                  d |D               d      | _        | j                  |       y)z0 Step through tree to find known abbreviations. NT)keyreversez\b(?:|c              3  F   K   | ]  }t        j                  |        y wr<   )reescape).0rR   s     r   	<genexpr>z(AbbrTreeprocessor.run.<locals>.<genexpr>y   s     /T3		#/Ts   !z)\b)
r   rC   keyssortlenrV   compilejoinr=   rA   )r   root	abbr_lists      r   runzAbbrTreeprocessor.runq   si    zz*+	3-**vsxx/T)/T'T%VVZ[\$r   )NN)r2   zMarkdown | Noner   zdict | Noner<   )rM   etree.ElementrN   etree.Element | NonereturnNone)r_   rb   rd   rc   )r4   r5   r6   r7   r   rA   ra   r8   r9   s   @r   r.   r.   Q   s    =
0
 r   r.   c                  n     e Zd ZdZ ej
                  dej                        Zd fdZddZ	ddZ
 xZS )	r1   z) Parse text for abbreviation references. z7^[*]\[(?P<abbr>[^\\]*?)\][ ]?:[ ]*\n?[ ]*(?P<title>.*)$c                2    || _         t        | 	  |       y r<   )r   r   r   )r   r/   r   r   s      r   r   zAbbrBlockprocessor.__init__   s     
 r   c                     y)NTr   )r   rN   blocks      r   testzAbbrBlockprocessor.test   s    r   c                   |j                  d      }| j                  j                  |      }|r|j                  d      j	                         }|j                  d      j	                         }|r|r|dk(  s|dk(  r| j
                  j                  |       n|| j
                  |<   ||j                         d j	                         r2|j                  d||j                         d j                  d             |d|j                          j	                         r2|j                  d|d|j                          j                  d             y|j                  d|       y	)
z
        Find and remove all abbreviation references from the text.
        Each reference is added to the abbreviation collection.

        r   r(   r?   z''z""N
TF)popr=   searchrE   stripr   rH   rJ   lstriprK   rstrip)r   rN   blocksri   rP   r(   r?   s          r   ra   zAbbrBlockprocessor.run   s    

1GGNN5!776?((*DGGG$**,ED=ETMJJNN4(',DJJt$?((*MM!U15578_%;%;D%AB!'')$**,MM!U:AGGI%6%=%=d%CDar   )r/   r   r   dict)rN   rb   ri   strrd   bool)rN   rb   rr   z	list[str]rd   ru   )r4   r5   r6   r7   rV   r]   	MULTILINEr=   r   rj   ra   r8   r9   s   @r   r1   r1   ~   s/    3	NPRP\P\	]B!r   r1   z4This class has been renamed to `AbbrBlockprocessor`.zJThis class will be removed in the future; use `AbbrTreeprocessor` instead.c                  ,     e Zd ZdZd fdZddZ xZS )AbbrInlineProcessorz Abbreviation inline pattern. c                2    t         |   |       || _        y r<   )r   r   r?   )r   patternr?   r   s      r   r   zAbbrInlineProcessor.__init__   s    !
r   c                    t        j                  d      }t        |j                  d            |_        |j                  d| j                         ||j                  d      |j                  d      fS )Nr(   r?   r   )	rF   rG   r
   rE   rB   setr?   rK   rH   )r   rP   datar(   s       r   handleMatchzAbbrInlineProcessor.handleMatch   sS    }}V$ 1	$**%QWWQZq))r   )rz   rt   r?   rt   )rP   zre.Match[str]r}   rt   rd   ztuple[etree.Element, int, int])r4   r5   r6   r7   r   r~   r8   r9   s   @r   rx   rx      s    (*r   rx   c                     t        di | S )Nr   )r   )r   s    r   makeExtensionr      s    "6""r   )r7   
__future__r    r   r0   r   inlinepatternsr   r,   r	   utilr
   r   typingr   rV   xml.etree.ElementTreerF   ElementTreer   blockparsersr   r   r.   r1   AbbrPreprocessorrx   r   r   r   r   <module>r      s     #  , , * +   	 % %*'bI 'bT*  * Z% %P V:TUVhi  XY*/ * Z*#r   