
    /Vh                    &   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     H/home/dcms/DCMS/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                  @     e Zd ZdZdd fdZd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                d    t        j                  dd|i      }t        |      |_        ||_        |S )z Create an `abbr` element. r(   title)etreeElementr
   texttail)r   r?   rB   rC   r(   s        r   create_elementz AbbrTreeprocessor.create_elementY   s/    }}Vgu%56 &		r   c                
   t        |      D ]  }| j                  ||        |j                  x}rt        |t              st        t        | j                  j                  |                  D ]  }| j                  |j                  d         s"| j                  | j                  |j                  d         |j                  d      ||j                         d       }|j                  d|       |d|j                          } ||_        ||j                  r|j                  }t        |      j                  |      dz   }t        |t              st        t        | j                  j                  |                  D ]v  }| j                  | j                  |j                  d         |j                  d      ||j                         d       }|j                  ||       |d|j                          }x ||_        yyyy)zW Recursively iterate over elements, run regex on text and wrap matches in `abbr` tags. r   Nr   )reversediter_elementrB   
isinstancer
   listr=   finditerr   grouprD   endinsertstartrC   index)	r   elparentchildrB   mr(   rC   rO   s	            r   rG   zAbbrTreeprocessor.iter_element`   s   b\ 	)EeR(	)77?4?dL1!$tww'7'7'=">? 0Azz!''!*-#224::aggaj3I177ST:W[\]\a\a\c\dWef		!T*#JQWWY/	0
 "''77DL&&r*Q.EdL1!$tww'7'7'=">? ,A..tzz!''!*/EqwwqzSWXYX]X]X_X`SabDMM%.
+D,  2 #*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).0rU   s     r   	<genexpr>z(AbbrTreeprocessor.run.<locals>.<genexpr>~   s     /T3		#/Ts   !z)\b)
r   rI   keyssortlenrY   compilejoinr=   rG   )r   root	abbr_lists      r   runzAbbrTreeprocessor.runv   si    zz*+	3-**vsxx/T)/T'T%VVZ[\$r   )NN)r2   zMarkdown | Noner   zdict | None)r?   strrB   re   rC   re   returnetree.Elementr<   )rP   rg   rQ   etree.Element | Nonerf   None)rb   rg   rf   rh   )	r4   r5   r6   r7   r   rD   rG   rd   r8   r9   s   @r   r.   r.   Q   s    =
,
 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   rQ   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=   searchrK   stripr   rL   rM   lstriprN   rstrip)r   rQ   blocksrm   rS   r(   r?   s          r   rd   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)rQ   rg   rm   re   rf   bool)rQ   rg   rv   z	list[str]rf   rx   )r4   r5   r6   r7   rY   r`   	MULTILINEr=   r   rn   rd   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   )	r@   rA   r
   rK   rB   setr?   rN   rL   )r   rS   datar(   s       r   handleMatchzAbbrInlineProcessor.handleMatch   sS    }}V$ 1	$**%QWWQZq))r   )r}   re   r?   re   )rS   zre.Match[str]r   re   rf   ztuple[etree.Element, int, int])r4   r5   r6   r7   r   r   r8   r9   s   @r   r{   r{      s    (*r   r{   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   rY   xml.etree.ElementTreer@   ElementTreer   blockparserr   r   r.   r1   AbbrPreprocessorr{   r   r   r   r   <module>r      s     #  , , * +   	 % %)'bI 'bT/  / d% %P V:TUVhi  XY*/ * Z*#r   