
    :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 dd
lmZmZ ddlmc mZ ddlmZmZmZ erddlm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d Zy)z
An implementation of [PHP Markdown Extra](http://michelf.com/projects/php-markdown/extra/)'s
parsing of Markdown syntax in raw HTML.

See the [documentation](https://Python-Markdown.github.io/extensions/raw_html)
for details.
    )annotations   )	Extension   )BlockProcessor)Preprocessor)RawHtmlPostprocessor)util)HTMLExtractorblank_line_reN)TYPE_CHECKINGLiteralMapping)Markdownc                       e Zd ZdZd fdZ fdZ fdZddZddZ fdZ	 fdZ
d	 Z fd
Z fdZd fdZd fdZ xZS )HTMLExtractorExtraz
    Override `HTMLExtractor` and create `etree` `Elements` for any elements which should have content parsed as
    Markdown.
    c                   t        |j                  j                               | _        t        g d      | _        t        g d      | _        t        |   |g|i | t        | j                        | j                  | j
                  z  | j                  z  z
  | _	        | j                  | j                  z  | _
        y )N)addressdddth1h2h3h4h5h6legendlipsummarytdth)canvasmathoptionprescriptstyletextarea)setblock_level_elementscopyblock_level_tags	span_tagsraw_tagssuper__init__
empty_tags
block_tagsspan_and_blocks_tags)selfmdargskwargs	__class__s       [/var/www/html/answerous/venv/lib/python3.12/site-packages/markdown/extensions/md_in_html.pyr1   zHTMLExtractorExtra.__init__+   s     #B$;$;$@$@$B Cs
 ^_-d-f- d3348VY]YhYh8hi$(OOdnn$D!    c                p    g | _         t        j                         | _        g | _        t
        |           y)z1Reset this instance.  Loses all unprocessed data.N)mdstacketreeTreeBuildertreebuildermdstater0   resetr5   r9   s    r:   rB   zHTMLExtractorExtra.reset;   s+    "$ ,,.DFr;   c                x    t         |           | j                  r| j                  | j                  d          yy)zHandle any buffered data.r   N)r0   closer=   handle_endtagrC   s    r:   rE   zHTMLExtractorExtra.closeB   s.    <<t||A/ r;   c                l    | j                   j                         }t        j                         | _         |S )zJ Return element from `treebuilder` and reset `treebuilder` for later use. )r@   rE   r>   r?   )r5   elements     r:   get_elementzHTMLExtractorExtra.get_elementJ   s,    ""((* ,,.r;   c                L   |j                  dd      }|dk(  rd}| j                  r| j                  d   nd}|dk(  s
|dk(  r|dk7  r|}|dk(  r|| j                  v s|dk(  r|| j                  v ry|dk(  r|| j                  v s|dk(  r|| j                  v ry|| j
                  v ryy)	zS Return state from tag and `markdown` attribute. One of 'block', 'span', or 'off'. markdown01Noffspanblock)getrA   r3   r4   r.   r-   )r5   tagattrsmd_attrparent_states        r:   	get_statezHTMLExtractorExtra.get_stateP   s    ))J,j G+/<<t||B'T5 \V%;3"G^t 6G#t/H/H(H#~#"7& SD,E,E%ED)))r;   c                   || j                   v r| j                         s| j                  r|D ci c]  \  }}|||n| }}}d|v r@|j                  d       t	        j
                  ||      }t	        j                  |dd      }n| j                         }| j                  |d       y || j                  v r| j                         s| j                  r|D ci c]  \  }}|||n| }}}| j                  ||      }| j                  s|dv r/| j                  s#|j                  dd        t        	| 9  ||       y d| j                  v r|| j                  v r| j                  d       | j                   j#                  |       | j                  j#                  |       ||d<   | j$                  j'                  ||       y | j                  rt        	| 9  ||       y | j                         }| j                   rG| j                   d   d	k(  r5| j)                  | j*                  j,                  j/                  |             n| j)                  |       || j0                  v r| j3                          y y c c}}w c c}}w )
NrK   unicodehtmlencodingmethodT)NrO   r   rN   rO   )r2   at_line_startintailpopr>   Elementtostringget_starttag_texthandle_empty_tagr-   rW   inrawr=   r0   handle_starttagrF   rA   appendr@   starthandle_datar6   	htmlStashstoreCDATA_CONTENT_ELEMENTSclear_cdata_mode)
r5   rS   rT   keyvaluerH   datastatetextr9   s
            r:   rf   z"HTMLExtractorExtra.handle_starttage   s&   $//!t'9'9';t{{PUV*#uS5#4%#=VEVU"		*%--U3~~g	&Q--/!!$-$'''T-?-?-AT[[ QVV*#uS5#4%#=VEVNN3.Ezze}4T\\		*d+'U3$,,&3$2G2G+G&&s+##E*##C($)j!  &&sE2 zz'U3--/<<DLL$4$=$$TWW%6%6%<%<T%BC$$T*$555))+ 6K W Ws   I7I=c           	        || j                   v rD| j                  rt        |   |       y || j                  v r| j                  rb| j                  j                         }| j                  j                          | j                  j                  |       ||k(  rn| j                  rb| j                  s| j                         }| j                  r| j                  d   nd}|j                  d      s,|j                  d      r| j                  j                  d       | j                  j                  | j                  j                  j                  |             | j                  j                  d       g | _        t#        j$                  | j&                  | j(                  | j*                  z   t-        | j/                  |            z   d        s~d| _        y | j/                  |      }| j                  rG| j                  d   dk(  r5| j3                  | j                  j                  j                  |             y | j3                  |       y y y | j                  rt        |   |       y | j/                  |      }| j                  rG| j                  d   dk(  r5| j3                  | j                  j                  j                  |             y | j3                  |       y )NrN    


TrO   )r-   re   r0   rF   r=   r`   rA   r@   endrI   cleandocendswithrg   r6   rj   rk   rq   r   matchrawdataline_offsetoffsetlenget_endtag_textr_   ri   )r5   rS   itemrH   rr   r9   s        r:   rF   z HTMLExtractorExtra.handle_endtag   s8   $'''zz%c*$ll<<++-DLL$$&$$((.s{ ll ||"..0G 154==,2D==0T]]45H,,T2MM(():):)@)@)IJMM((0!#DJ(.. LL)9)9DKK)G#dNbNbcfNgJh)h)ijl '+ ++C0<<DLL$4$=$$TWW%6%6%<%<T%BC$$T*l $4 zz%c*++C0<<DLL$4$=$$TWW%6%6%<%<T%BC$$T*r;   c                   || j                   v rn|D ci c]  \  }}|||n| }}}d|v r@|j                  d       t        j                  ||      }t        j                  |dd      }n!| j                         }n| j                         }| j                  || j                  j                  |             y c c}}w )NrK   rY   rZ   r[   )is_block)	r2   r`   r>   ra   rb   rc   rd   r6   is_block_level)r5   rS   rT   rn   ro   rH   rp   s          r:   handle_startendtagz%HTMLExtractorExtra.handle_startendtag   s    $//!PUV*#uS5#4%#=VEVU"		*%--U3~~g	&Q--/))+DdTWW-C-CC-HI Ws   B:c                    | j                   rd|v rd| _         | j                  s| j                  st        |   |       y | j
                  j                  |       y )Nrv   F)r_   re   r=   r0   ri   r@   rp   )r5   rp   r9   s     r:   ri   zHTMLExtractorExtra.handle_data   sE    ;;44<DK::T\\G%!!$'r;   c                Z   | j                   s| j                  st        |   ||       y | j	                         r=|r;| j                  d| j                  j                  j                  |      z   dz          y | j                  | j                  j                  j                  |             y )Nrv   ru   )	re   r=   r0   rd   r^   ri   r6   rj   rk   )r5   rp   r   r9   s      r:   rd   z#HTMLExtractorExtra.handle_empty_tag   s}    ::T\\G$T84!!#  (9(9(?(?(E!E!NO  !2!2!8!8!>?r;   c                    | j                         s| j                  s| j                  rt        t        |   |      S | j                  d       |dz   S )Nz<?r   )r^   r_   r=   r0   r   parse_piri   r5   ir9   s     r:   r   zHTMLExtractorExtra.parse_pi   sH    4;;$,, 6q99 	1ur;   c                    | j                         s| j                  s| j                  rt        t        |   |      S | j                  d       |dz   S )Nz<!r   )r^   r_   r=   r0   r   parse_html_declarationri   r   s     r:   r   z)HTMLExtractorExtra.parse_html_declaration   sH    4;;$,, DQGG 	1ur;   )r6   r   )returnetree.Element)rT   zMapping[str, str]r   z%Literal['block', 'span', 'off', None])r   intr   r   )__name__
__module____qualname____doc__r1   rB   rE   rI   rW   rf   rF   r   ri   rd   r   r   __classcell__)r9   s   @r:   r   r   %   sN    
E 0**,X-+^J(@ r;   r   c                      e Zd ZdZddZy)HtmlBlockPreprocessorzDRemove html blocks from the text and store them for later retrieval.c                    dj                  |      }t        | j                        }|j                  |       |j	                          dj                  |j
                        j                  d      S )Nrv   rt   )joinr   r6   feedrE   rx   split)r5   linessourceparsers       r:   runzHtmlBlockPreprocessor.run   sQ    5!#DGG,Fwwv'--d33r;   N)r   	list[str]r   r   )r   r   r   r   r    r;   r:   r   r      s
    N4r;   r   c                  (    e Zd ZdZddZddZddZy)	MarkdownInHtmlProcessorzNProcess Markdown Inside HTML Blocks which have been stored in the `HtmlStash`.c                     y)NTr   )r5   parentrQ   s      r:   testzMarkdownInHtmlProcessor.test  s    r;   c                   |j                   j                  dd      }|dk(  rt        |      D ]  }| j                  |        g }t	        |      D ]  \  }}|j
                  s|j
                  j                  d      }d|_        t        j                  d      }| j                  j                  ||j                  d             t        |      }|j                          |j                  |dz   |f        |j                          |D ]  \  }}	|	D ]  }
|j                  ||
          |j                  r|j                  j                  d      }d|_        t        j                  d      }| j                  j                  ||j                  d             t        |      }|j                          |D ]  }|j                  d	|        yy|d
k(  r"t        |      D ]  }| j                  |        y|j                  d|_        t!        j"                  |j                        |_        t        |      D ]D  }| j                  |       |j
                  s!t!        j"                  |j
                        |_        F y)a  
        Recursively parse the text content of an `etree` Element as Markdown.

        Any block level elements generated from the Markdown will be inserted as children of the element in place
        of the text content. All `markdown` attributes are removed. For any elements in which Markdown parsing has
        been disabled, the text content of it and its children are wrapped in an `AtomicString`.
        rK   rO   rQ   rv   rt   divru   r   r   rP   N)attribr`   listparse_element_content	enumeratetailrstripr>   ra   r   parseBlocksr   reverserg   insertrr   r
   AtomicString)r5   rH   rU   childtailsposrQ   dummychildrenr   r   s              r:   r   z-MarkdownInHtmlProcessor.parse_element_content  s0    ..$$Z7g g 2**512
 E'0 	6
U::!JJ--d3E!#EJ!MM%0EKK++E5;;v3FG#E{H$$&LL#'8!45	6 MMO" .	T  .DNN3-..
 ||++D1!e,''u{{6/BC;  "% -ENN1e,-   g 2**512
 ||#!,,W\\:GLg ?**51::!%!2!25::!>EJ?r;   c                ^   t         j                  j                  |d         }|rt        |j	                  d            }| j
                  j                  j                  j                  |   }t        |t        j                        r|j                  d       | j                  |       |j                  |       | j
                  j                  j                  j                  j                  |       | j
                  j                  j                  j                  j                  |d       yy)Nr   r   rt   TF)r
   HTML_PLACEHOLDER_RErz   r   groupr   r6   rj   rawHtmlBlocks
isinstancer>   ra   r`   r   rg   r   )r5   r   blocksmindexrH   s         r:   r   zMarkdownInHtmlProcessor.runK  s    $$**6!95
OEkknn..<<UCG'5==1

1**73g&((66::5A((66==eRHr;   N)r   r   rQ   strr   bool)rH   r   r   None)r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r;   r:   r   r      s    XD?Lr;   r   c                      e Zd ZddZy)MarkdownInHTMLPostprocessorc                    t        |t        j                        r| j                  j	                  |      S t        |      S )zE Override default to handle any `etree` elements still in the stash. )r   r>   ra   r6   
serializerr   )r5   rr   s     r:   stash_to_stringz+MarkdownInHTMLPostprocessor.stash_to_string_  s/    dEMM*77%%d++t9r;   N)rr   zstr | etree.Elementr   r   )r   r   r   r   r   r;   r:   r   r   ^  s    r;   r   c                      e Zd ZdZd Zy)MarkdownInHtmlExtensionz/Add Markdown parsing in HTML to Markdown class.c                   |j                   j                  t        |      dd       |j                  j                  j                  t        |j                        dd       |j                  j                  t        |      dd       y)z Register extension instances. 
html_block   markdown_blocki   raw_html   N)preprocessorsregisterr   r   blockprocessorsr   postprocessorsr   )r5   r6   s     r:   extendMarkdownz&MarkdownInHtmlExtension.extendMarkdownj  sl     	!!"7";\2N
		!!**#BII.0@#	
 	""#>r#BJPRSr;   N)r   r   r   r   r   r   r;   r:   r   r   g  s    9
Tr;   r   c                     t        di | S )Nr   )r   )r8   s    r:   makeExtensionr   w  s    ",V,,r;   )r   
__future__r   rt   r   r   r   r   r   r   r	   r
   
htmlparserr   r   xml.etree.ElementTreer>   ElementTreetypingr   r   r   rK   r   r   r   r   r   r   r   r   r;   r:   <module>r      s}    #  , ( 1  5 % % 2 2!K K\4L 4]n ]@"6 Ti T -r;   