
    /Vhi                       d Z ddlmZ ddlZddlZddlmc mZ ddlm	Z	m
Z
 ddlmZ ddlmZ e	rddlmZ  ej"                  d	      Zd#d
Z G d d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Zy)$a;  
A block processor parses blocks of text and adds new elements to the ElementTree. Blocks of text,
separated from other text by blank lines, may have a different syntax and produce a differently
structured tree than other Markdown. Block processors excel at handling code formatting, equation
layouts, tables, etc.
    )annotationsN)TYPE_CHECKINGAny   )util)BlockParser)MarkdownMARKDOWNc                `   t        |       }|j                  j                  t        |      dd       |j                  j                  t	        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      d	d
       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |j                  j                  t        |      dd       |S )z2 Build the default block parser used by Markdown. emptyd   indentZ   codeP   
hashheaderF   setextheader<   hr2   olist(   ulist   quote   	reference   	paragraph
   )r   blockprocessorsregisterEmptyBlockProcessorListIndentProcessorCodeBlockProcessorHashHeaderProcessorSetextHeaderProcessorHRProcessorOListProcessorUListProcessorBlockQuoteProcessorReferenceProcessorParagraphProcessor)mdkwargsparsers      H/home/dcms/DCMS/lib/python3.12/site-packages/markdown/blockprocessors.pybuild_block_parserr3   *   sX   _F
##$7$?#N
##$7$?2N
##$6v$>K
##$7$?rR
##$9&$A>SUV
##K$7rB
##N6$:GRH
##N6$:GRH
##$7$?"M
##$6v$>RP
##$6v$>RPM    c                  D    e Zd ZdZd	dZd
dZdddZdddZddZddZ	y)BlockProcessoraY   Base class for block processors.

    Each subclass will provide the methods below to work with the source and
    tree. Each processor will need to define it's own `test` and `run`
    methods. The `test` method should return True or False, to indicate
    whether the current block should be processed by this processor. If the
    test passes, the parser will call the processors `run` method.

    Attributes:
        BlockProcessor.parser (BlockParser): The `BlockParser` instance this is attached to.
        BlockProcessor.tab_length (int): The tab length set on the `Markdown` instance.

    c                H    || _         |j                  j                  | _        y N)r1   r/   
tab_length)selfr1   s     r2   __init__zBlockProcessor.__init__J   s     ))..r4   c                $    t        |      r|d   S y)z. Return the last child of an `etree` element. N)len)r:   parents     r2   	lastChildzBlockProcessor.lastChildN   s    v;":r4   Nc                F   || j                   }g }|j                  d      }|D ]N  }|j                  d|z        r|j                  ||d        ,|j	                         s|j                  d       N n dj                  |      dj                  |t        |      d       fS )z= Remove a tab from the front of each line of the given text. N
  )r9   split
startswithappendstripjoinr>   )r:   textlengthnewtextlineslines         r2   detabzBlockProcessor.detabU   s    >__F

4  	DsV|,tFG}-ZZ\r"	 yy!499U3w<=-A#BBBr4   c                    |j                  d      }t        t        |            D ]?  }||   j                  d| j                  z  |z        s(||   | j                  |z  d ||<   A dj                  |      S )z? Remove a tab from front of lines but allowing dedented lines. rB   rC   N)rE   ranger>   rF   r9   rI   )r:   rJ   levelrM   is        r2   
looseDetabzBlockProcessor.looseDetabd   sw    

4 s5z" 	<AQx""3t#6u#<= 8DOOE$9$:;a	< yyr4   c                     y)aZ   Test for block type. Must be overridden by subclasses.

        As the parser loops through processors, it will call the `test`
        method on each to determine if the given block of text is of that
        type. This method must return a boolean `True` or `False`. The
        actual method of testing is left to the needs of that particular
        block type. It could be as simple as `block.startswith(some_string)`
        or a complex regular expression. As the block type may be different
        depending on the parent of the block (i.e. inside a list), the parent
        `etree` element is also provided and may be used as part of the test.

        Keyword arguments:
            parent: An `etree` element which will be the parent of the block.
            block: A block of text from the source which has been split at blank lines.
        N r:   r?   blocks      r2   testzBlockProcessor.testl   s      	r4   c                     y)a3   Run processor. Must be overridden by subclasses.

        When the parser determines the appropriate type of a block, the parser
        will call the corresponding processor's `run` method. This method
        should parse the individual lines of the block and append them to
        the `etree`.

        Note that both the `parent` and `etree` keywords are pointers
        to instances of the objects which should be edited in place. Each
        processor must make changes to the existing objects as there is no
        mechanism to return new/different objects to replace them.

        This means that this method should be adding `SubElements` or adding text
        to the parent, and should remove (`pop`) or add (`insert`) items to
        the list of blocks.

        If `False` is returned, this will have the same effect as returning `False`
        from the `test` method.

        Keyword arguments:
            parent: An `etree` element which is the parent of the current block.
            blocks: A list of all remaining blocks of the document.
        NrV   )r:   r?   blockss      r2   runzBlockProcessor.run~   s    0 	r4   r1   r   )r?   etree.Elementreturnzetree.Element | Noner8   )rJ   strrK   z
int | Noner_   ztuple[str, str])r   )rJ   r`   rR   intr_   r`   r?   r^   rX   r`   r_   bool)r?   r^   r[   	list[str]r_   zbool | None)
__name__
__module____qualname____doc__r;   r@   rO   rT   rY   r\   rV   r4   r2   r6   r6   ;   s&    /C $r4   r6   c                  T     e Zd ZdZdgZ	 ddgZ	  fdZd
dZddZddZ	dd	Z
 xZS )r%   z Process children of list items.

    Example

        * a list item
            process this part

            or this part

    liulolc                l    t        |   |  t        j                  d| j                  z        | _        y )Nz^(([ ]{%s})+))superr;   recompiler9   	INDENT_RE)r:   args	__class__s     r2   r;   zListIndentProcessor.__init__   s*    $$4t$FGr4   c                (   |j                  d| j                  z        xrs | j                  j                  j	                  d       xrK |j
                  | j                  v xs1 t        |      xr$ |d   d uxr |d   j
                  | j                  v S )NrC   detabbedr=   )	rF   r9   r1   stateisstatetag
ITEM_TYPESr>   
LIST_TYPESrW   s      r2   rY   zListIndentProcessor.test   s    DOO 34 :!!))*55:ZZ4??* 9V 84!7 8BZ^^t6		:r4   c                   |j                  d      }| j                  ||      \  }}| j                  ||      }| j                  j                  j                  d       |j                  | j                  v rft        |      r=|d   j                  | j                  v r"| j                  j                  |d   |g       n| j                  j                  ||g       n|j                  | j                  v r| j                  j                  ||g       nt        |      r|d   j                  | j                  v rw|d   j                  rHt        j                  d      }|d   j                  |_        d|d   _        |d   j                  d|       | j                  j                  |d   |       n| j!                  ||       | j                  j                  j#                          y )Nr   ru   r=   prD   )pop	get_levelrT   r1   rv   setrx   ry   r>   rz   parseBlocksrJ   etreeElementinsert
parseChunkcreate_itemreset)r:   r?   r[   rX   rR   siblingr|   s          r2   r\   zListIndentProcessor.run   sw   

16wu-j)::( 6{vbz~~@''r
UG< ''8[[DOO+KK##GeW5\gbkoo@ r{ MM#& ))#% ""1a(KK""72;6We,!r4   c                j    t        j                  |d      }| j                  j                  ||g       y)z> Create a new `li` and parse the block with it as the parent. rj   N)r   
SubElementr1   r   )r:   r?   rX   rj   s       r2   r   zListIndentProcessor.create_item   s*    fd+UG,r4   c                   | j                   j                  |      }|r(t        |j                  d            | j                  z  }nd}| j
                  j                  j                  d      rd}nd}||kD  rn| j                  |      }|P|j                  | j                  v s|j                  | j                  v r |j                  | j                  v r|dz  }|}n	 ||fS ||kD  rn||fS )z/ Get level of indentation based on list level. r   r   list)rq   matchr>   groupr9   r1   rv   rw   r@   rx   rz   ry   )r:   r?   rX   mindent_levelrR   childs          r2   r~   zListIndentProcessor.get_level   s     NN  'qwwqz?4??:LL;;$$V,E EU"NN6*E!		T__,		T__0L99/QJE f} U" f}r4   rb   r?   r^   r[   rd   r_   None)r?   r^   rX   r`   r_   r   )r?   r^   rX   r`   r_   ztuple[int, etree.Element])re   rf   rg   rh   ry   rz   r;   rY   r\   r   r~   __classcell__rs   s   @r2   r%   r%      s;    	 J-J9H:""H-
r4   r%   c                       e Zd ZdZddZddZy)r&   z Process code blocks. c                >    |j                  d| j                  z        S )NrC   )rF   r9   rW   s      r2   rY   zCodeBlockProcessor.test   s    DOO 344r4   c           	        | j                  |      }|j                  d      }d}||j                  dk(  rt        |      r|d   j                  dk(  ro|d   }| j	                  |      \  }}t        j                  dj                  |j                  t        j                  |j                                           |_        n~t        j                  |d      }t        j                  |d      }| j	                  |      \  }}t        j                  dt        j                  |j                               z        |_        |r|j                  d|       y y )Nr   rD   prer   z{}
{}
z%s
)r@   r}   rx   r>   rO   r   AtomicStringformatrJ   code_escaperstripr   r   r   )r:   r?   r[   r   rX   theRestr   r   s           r2   r\   zCodeBlockProcessor.run  s   ..(

1GKK5$8w<GAJNNf4 1:D!ZZ.NE7))!!$))T-=-=elln-MNDI
 ""651C##C0D!ZZ.NE7))&43C3CELLN3S*STDI MM!W%	 r4   Nrb   r   re   rf   rg   rh   rY   r\   rV   r4   r2   r&   r&      s     5&r4   r&   c                  L    e Zd ZdZ ej
                  d      ZddZddZd	dZ	y)
r,   z Process blockquotes. z(^|\n)[ ]{0,3}>[ ]?(.*)c                x    t        | j                  j                  |            xr t        j                          S r8   )rc   REsearchr   nearing_recursion_limitrW   s      r2   rY   zBlockQuoteProcessor.test!  s+    DGGNN5)*Q43O3O3Q/QQr4   c                   |j                  d      }| j                  j                  |      }|r~|d |j                          }| j                  j                  ||g       dj                  ||j                         d  j                  d      D cg c]  }| j                  |       c}      }| j                  |      }||j                  dk(  r|}nt        j                  |d      }| j                  j                  j                  d       | j                  j                  ||       | j                  j                  j!                          y c c}w )Nr   rB   
blockquote)r}   r   r   startr1   r   rI   rE   cleanr@   rx   r   r   rv   r   r   r   )	r:   r?   r[   rX   r   beforerN   r   r   s	            r2   r\   zBlockQuoteProcessor.run$  s   

1GGNN5!:AGGI&FKK##FVH5II.3AGGIJ.?.E.Ed.KLdD!LE ..(7;;,#>E $$V\:E 	l+ue,! Ms   Ec                    | j                   j                  |      }|j                         dk(  ry|r|j                  d      S |S )z& Remove `>` from beginning of a line. >rD      )r   r   rH   r   )r:   rN   r   s      r2   r   zBlockQuoteProcessor.clean<  s;    GGMM$::<3771:Kr4   Nrb   r   )rN   r`   r_   r`   )
re   rf   rg   rh   ro   rp   r   rY   r\   r   rV   r4   r2   r,   r,     s&     	.	/BR"0r4   r,   c                       e Zd ZU dZdZded<   	 dZded<   	 dZded	<   	 dd
gZded<   	 d fdZ	ddZ
ddZddZ xZS )r*   z Process ordered list blocks. rl   r`   TAG1
STARTSWITHTrc   LAZY_OLrk   rd   SIBLING_TAGSc                >   t         |   |       t        j                  d| j                  dz
  z        | _        t        j                  d| j                  dz
  z        | _        t        j                  d| j                  | j                  dz  dz
  fz        | _        y )Nz^[ ]{0,%d}\d+\.[ ]+(.*)r   z!^[ ]{0,%d}((\d+\.)|[*+-])[ ]+(.*)z ^[ ]{%d,%d}((\d+\.)|[*+-])[ ]+.*r   )rn   r;   ro   rp   r9   r   CHILD_RErq   r:   r1   rs   s     r2   r;   zOListProcessor.__init__Y  s     **74??Q;NOP

#G$(OOa$7$9 : $G%)__doo6IA6M$N%O Pr4   c                J    t        | j                  j                  |            S r8   rc   r   r   rW   s      r2   rY   zOListProcessor.testd      DGGMM%())r4   c                |   | j                  |j                  d            }| j                  |      }|a|j                  | j                  v rH|}|d   j
                  rHt        j                  d      }|d   j
                  |_        d|d   _        |d   j                  d|       | j                  |d         }|K|j                  r?t        j                  |d   d      }|j                  j                         |_        d|_	        t        j                  |d      }| j                  j                  j                  d       |j                  d      }	| j                  j                  ||	g       | j                  j                  j!                          ne|j                  dv r|}nTt        j                  || j"                        }| j$                  s(| j&                  dk7  r| j&                  |j(                  d	<   | j                  j                  j                  d
       |D ]t  }
|
j+                  d| j,                  z        r!| j                  j                  |d   |
g       Bt        j                  |d      }| j                  j                  ||
g       v | j                  j                  j!                          y )Nr   r=   r|   rD   rj   	looselist)rl   rk   r   r   r   rC   )	get_itemsr}   r@   rx   r   rJ   r   r   r   tailr   lstripr1   rv   r   r   r   r   r   r   attribrF   r9   )r:   r?   r[   itemsr   lstr|   lchrj   	firstitemitems              r2   r\   zOListProcessor.rung  s'   vzz!}-..(7;;$2C2C#CC 2w|| MM#&R!BBq!$ ..R)C388$$SWc2* !!#t,BKK!!+.		!IKK##B4KK##%ZZ<' C ""64884C<<DOOs$:&*oo

7#f%  	4Ds4??23''B$8 %%c40''TF3	4 	!r4   c                h   g }|j                  d      D ]  }| j                  j                  |      }|rz|sW| j                  dk(  rHt	        j
                  d      }|j                  |j                  d            j                         | _        |j                  |j                  d             | j                  j                  |      rM|d   j                  d| j                  z        rdj                  |d   |      |d<   |j                  |       dj                  |d   |      |d<    |S )	z  Break a block into list items. rB   rl   z(\d+)r      r=   rC   {}
{})rE   r   r   r   ro   rp   r   r   rG   rq   rF   r9   r   )r:   rX   r   rN   r   
INTEGER_REs         r2   r   zOListProcessor.get_items  s    KK% 	=D##D)A T!1!#H!5J&0&6&6qwwqz&B&H&H&JDOQWWQZ(%%d+9''DOO(;< (b	4 @E"ILL& %OOE"It<b	)	=* r4   r]   rb   r   )rX   r`   r_   rd   )re   rf   rg   rh   r   __annotations__r   r   r   r;   rY   r\   r   r   r   s   @r2   r*   r*   G  sY    (CO6J GT*#TlL)*
	P*9"vr4   r*   c                  6     e Zd ZU dZdZded<   	 d fdZ xZS )r+   z  Process unordered list blocks. rk   r`   r   c                x    t         |   |       t        j                  d| j                  dz
  z        | _        y )Nz^[ ]{0,%d}[*+-][ ]+(.*)r   )rn   r;   ro   rp   r9   r   r   s     r2   r;   zUListProcessor.__init__  s/     **74??Q;NOPr4   r]   )re   rf   rg   rh   r   r   r;   r   r   s   @r2   r+   r+     s    *CO6Q Qr4   r+   c                  D    e Zd ZdZ ej
                  d      ZddZddZy)r'   z Process Hash Headers. z>(?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\])*?)#*(?:\n|$)c                J    t        | j                  j                  |            S r8   )rc   r   r   rW   s      r2   rY   zHashHeaderProcessor.test  s    DGGNN5)**r4   c           	     \   |j                  d      }| j                  j                  |      }|r|d |j                          }||j	                         d  }|r| j
                  j                  ||g       t        j                  |dt        |j                  d            z        }|j                  d      j                         |_        |rI| j
                  j                  j                  d      r| j                  |      }|j!                  d|       y y t"        j%                  d|z         y )Nr   h%drR   headerr   zWe've got a problem header: %r)r}   r   r   r   endr1   r   r   r   r>   r   rH   rJ   rv   rw   rT   r   loggerwarn)r:   r?   r[   rX   r   r   afterhs           r2   r\   zHashHeaderProcessor.run  s    

1GGNN5!:AGGI&F!%%'(OE ''9  QWWW5E1F)FGAWWX&,,.AF;;$$,,[9 !OOE2Ea'  KK85@Ar4   Nrb   r   )	re   rf   rg   rh   ro   rp   r   rY   r\   rV   r4   r2   r'   r'     s#    ! 
U	VB+Br4   r'   c                  Z    e Zd ZdZ ej
                  dej                        ZddZddZ	y)r(   z Process Setext-style Headers. z^.*?\n[=-]+[ ]*(\n|$)c                J    t        | j                  j                  |            S r8   r   rW   s      r2   rY   zSetextHeaderProcessor.test  r   r4   c                >   |j                  d      j                  d      }|d   j                  d      rd}nd}t        j                  |d|z        }|d   j                         |_        t        |      dkD  r%|j                  ddj                  |dd               y y )Nr   rB   r   =r   r   )
r}   rE   rF   r   r   rH   rJ   r>   r   rI   )r:   r?   r[   rM   rR   r   s         r2   r\   zSetextHeaderProcessor.run  s    

1##D)8s#EEVUU]3q!u:>MM!TYYuQRy12 r4   Nrb   r   
re   rf   rg   rh   ro   rp   	MULTILINEr   rY   r\   rV   r4   r2   r(   r(     s(    ) 
,bll	;B*3r4   r(   c                  ^    e Zd ZdZdZ ej                  eej                        ZddZ	ddZ
y)r)   z Process Horizontal Rules. zf^[ ]{0,3}(?=(?P<atomicgroup>(-+[ ]{0,2}){3,}|(_+[ ]{0,2}){3,}|(\*+[ ]{0,2}){3,}))(?P=atomicgroup)[ ]*$c                N    | j                   j                  |      }|r|| _        yy)NTF)	SEARCH_REr   r   )r:   r?   rX   r   s       r2   rY   zHRProcessor.test  s&    NN!!%(DJr4   c                Z   |j                  d      }| j                  }|d |j                          j                  d      }|r| j                  j                  ||g       t        j                  |d       ||j                         d  j                  d      }|r|j                  d|       y y )Nr   rB   r   )r}   r   r   r   r1   r   r   r   r   r   r   )r:   r?   r[   rX   r   prelines	postliness          r2   r\   zHRProcessor.run  s    

1

%++-(//5KK##FXJ7&%))+,'..t4	MM!Y' r4   Nrb   r   )re   rf   rg   rh   r   ro   rp   r   r   rY   r\   rV   r4   r2   r)   r)     s-    % 
sB

2r||,I(r4   r)   c                       e Zd ZdZddZddZy)r$   z< Process blocks that are empty or start with an empty line. c                .    | xs |j                  d      S )NrB   )rF   rW   s      r2   rY   zEmptyBlockProcessor.test)  s    y2E,,T22r4   c                ^   |j                  d      }d}|rd}|dd  }|r|j                  d|       | j                  |      }|j|j                  dk(  rZt	        |      rN|d   j                  dk(  r;t        j                  dj                  |d   j                  |            |d   _        y y y y y )Nr   z

rB   r   r   r   z{}{})	r}   r   r@   rx   r>   r   r   r   rJ   )r:   r?   r[   rX   fillerr   r   s          r2   r\   zEmptyBlockProcessor.run,  s    

1 FABiGa)..(GKK5$8w<GAJNNf4"//gajoov6GAJO 5< %9r4   Nrb   r   r   rV   r4   r2   r$   r$   &  s    F3r4   r$   c                  Z    e Zd ZdZ ej
                  dej                        ZddZddZ	y)r-   z Process link references. z\^[ ]{0,3}\[([^\[\]]*)\]:[ ]*\n?[ ]*([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$c                     yNTrV   rW   s      r2   rY   zReferenceProcessor.testG      r4   c                   |j                  d      }| j                  j                  |      }|rL|j                  d      j	                         j                         }|j                  d      j                  d      j                  d      }|j                  d      xs |j                  d      }||f| j                  j                  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
)Nr   r   r   <r         rB   TF)r}   r   r   r   rH   lowerr   r   r1   r/   
referencesr   r   r   )r:   r?   r[   rX   r   idlinktitles           r2   r\   zReferenceProcessor.runJ  s,   

1GGNN5!!!#))+B771:$$S)005DGGAJ,!''!*E-15MDKKNN%%b)QUUWX$$&aquuwx!7!7!=>Zaggi &&(az	!2!9!9$!?@ar4   Nrb   )r?   r^   r[   rd   r_   rc   r   rV   r4   r2   r-   r-   A  s+    $	gikiuiu
Br4   r-   c                       e Zd ZdZddZddZy)r.   z Process Paragraph blocks. c                     yr   rV   rW   s      r2   rY   zParagraphProcessor.testa  r   r4   c                   |j                  d      }|j                         r| j                  j                  j	                  d      r| j                  |      }|9|j                  r"dj                  |j                  |      |_        y d|z  |_        y |j                  r"dj                  |j                  |      |_        y |j                         |_        y t        j                  |d      }|j                         |_        y y )Nr   r   r   z
%sr|   )r}   rH   r1   rv   rw   r@   r   r   rJ   r   r   r   )r:   r?   r[   rX   r   r|   s         r2   r\   zParagraphProcessor.rund  s    

1;;={{  ((0 ..0&||'/w||U'K'-~ {{&.oofkk5&I&+lln $$VS17 r4   Nrb   r   r   rV   r4   r2   r.   r.   ^  s    %(r4   r.   )r/   r	   r0   r   r_   r   ) rh   
__future__r   loggingro   xml.etree.ElementTreer   ElementTreetypingr   r   rD   r   blockparserr   markdownr	   	getLoggerr   r3   r6   r%   r&   r,   r*   r+   r'   r(   r)   r$   r-   r.   rV   r4   r2   <module>r      s   ( #  	 % % %  $!			:	&"[ [|_. _D& &B(. (Vs^ sl	Q^ 	Q!B. !BH3N 3.(. (D. 6 :#( #(r4   