o
    ¹¼tft>  ã                   @   s,  d dl Z d dlmZmZmZmZ ddlmZmZm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ e jde jd	Ze  d
¡Ze jde jd	Ze jde jd	Ze  d¡Z e  d¡Z!d "e¡d d "e¡ Z#e  ed ¡Z$e  d¡Z%e  d¡Z&G dd„ deƒZ'dd„ Z(dd„ Z)dS )é    N)ÚOptionalÚListÚTupleÚMatché   )ÚunikeyÚ
escape_urlÚ
expand_tabÚexpand_leading_tab)ÚParserÚ
BlockState)Ú
LINK_LABELÚHTML_TAGNAMEÚHTML_ATTRIBUTESÚ
BLOCK_TAGSÚPRE_TAGSÚunescape_charÚparse_link_hrefÚparse_link_title)Ú
parse_listÚLIST_PATTERNz^ {1,4})Úflagsz(\s+|^)#+\s*$z^ ?z^ *>z\n[ \t]*\n$z[ \t]*\nú|z[ \t]*>[ \t]*(?:\n|$)z( {0,3}>[^\n]*(?:\n|$))+c                       sÔ  e Zd Ze dej¡Zde d Zde	 d Z
ddddd	d
de d dee
edœZdZ			d5deee  deee  def‡ fdd„Zdededefdd„Zdededefdd„Zdededefdd„Zdededee fd d!„Zdededefd"d#„Zdededee fd$d%„Zdededee fd&d'„Zdededeeef fd(d)„Zdededefd*d+„Zdededefd,d-„Z dededee fd.d/„Z!dededee fd0d1„Z"d6ded2eee  ddfd3d4„Z#‡  Z$S )7ÚBlockParserz(^[ \t\v\f]*\n)+z^ {0,3}(</?z|<!--|<\?|<![A-Z]|<!\[CDATA\[)z^ {0,3}(?:(?:</?z.(?:[ \t]+|\n|$))|<!--|<\?|<![A-Z]|<!\[CDATA\[)z:^ {0,3}(?P<axt_1>#{1,6})(?!#+)(?P<axt_2>[ \t]*|[ \t]+.*?)$z#^ {0,3}(?P<setext_1>=|-){1,}[ \t]*$zF^(?P<fenced_1> {0,3})(?P<fenced_2>`{3,}|~{3,})[ \t]*(?P<fenced_3>.*?)$zC^(?: {4}| *\t)[^\n]+(?:\n+|$)((?:(?: {4}| *\t)[^\n]+(?:\n+|$))|\s)*z:^ {0,3}((?:-[ \t]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})$z^ {0,3}\[(?P<reflink_1>z)\]:z^ {0,3}>(?P<quote_1>.*?)$)Ú
blank_lineÚaxt_headingÚsetex_headingÚfenced_codeÚindent_codeÚthematic_breakÚref_linkÚblock_quoteÚlistÚ
block_htmlÚraw_html)
r   r   r   r   r   r!   r"   r    r$   r   Né   Úblock_quote_rulesÚ
list_rulesÚmax_nested_levelc                    s^   t tˆ ƒ ¡  |d u rtˆ jƒ}|d u rtˆ jƒ}|ˆ _|ˆ _|ˆ _‡ fdd„ˆ jD ƒˆ _	d S )Nc                    s   i | ]
}|t ˆ d | ƒ“qS )Zparse_)Úgetattr)Ú.0Úname©Úself© ú]/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/mistune/block_parser.pyÚ
<dictcomp>l   s    ÿz(BlockParser.__init__.<locals>.<dictcomp>)
Úsuperr   Ú__init__r"   ÚDEFAULT_RULESr&   r'   r(   ÚSPECIFICATIONZ_methods)r-   r&   r'   r(   ©Ú	__class__r,   r/   r2   Z   s   


ÿzBlockParser.__init__ÚmÚstateÚreturnc                 C   s   |  ddi¡ | ¡ S )zParse token for blank lines.Útyper   ©Úappend_tokenÚend©r-   r7   r8   r.   r.   r/   Úparse_blank_linep   s   zBlockParser.parse_blank_linec                 C   s   |  ddi¡ | ¡ d S )z:Parse token for thematic break, e.g. ``<hr>`` tag in HTML.r:   r   r   r;   r>   r.   r.   r/   Úparse_thematic_breaku   s   z BlockParser.parse_thematic_breakc                 C   sR   |  ¡ }|r|S | d¡}t|ƒ}t d|¡}| d¡}| d|ddœ¡ | ¡ S )z9Parse token for code block which is indented by 4 spaces.r   Ú Ú
Ú
block_codeÚindent)r:   ÚrawÚstyle)Úappend_paragraphÚgroupr
   Ú_INDENT_CODE_TRIMÚsubÚstripr<   r=   )r-   r7   r8   Úend_posÚcoder.   r.   r/   Úparse_indent_code{   s   

zBlockParser.parse_indent_codec                 C   s"  |  d¡}|  d¡}|  d¡}|d }|r"|dkr"| |¡dkr"dS t d| d	 tt|ƒƒ d
 tj¡}| ¡ d }| |j	|¡}	|	rR|j	||	 
¡ … }
|	 ¡ }n
|j	|d… }
|j}|ru|
rut dtt|ƒƒ d tj¡}| d|
¡}
d|
d|dœ}|rŠt|ƒ}d| ¡ i|d< | |¡ |S )a9  Parse token for fenced code block. A fenced code block is started with
        3 or more backtick(`) or tilde(~).

        An example of a fenced code block:

        .. code-block:: markdown

            ```python
            def markdown(text):
                return mistune.html(text)
            ```
        Zfenced_1Zfenced_2Zfenced_3r   ú`éÿÿÿÿNz^ {0,3}Ú{z,}[ \t]*(?:\n|$)r   z^ {0,Ú}rA   rC   Zfenced)r:   rE   rF   ÚmarkerÚinfoÚattrs)rH   ÚfindÚreÚcompileÚstrÚlenÚMr=   ÚsearchÚsrcÚstartÚ
cursor_maxrJ   r   rK   r<   )r-   r7   r8   ÚspacesrS   rT   ÚcÚ_endZcursor_startÚm2rM   rL   Z_trim_patternÚtokenr.   r.   r/   Úparse_fenced_code‰   s4   


ÿ

zBlockParser.parse_fenced_codec                 C   sT   t | d¡ƒ}| d¡ ¡ }|rt d|¡}d|d|iddœ}| |¡ | ¡ d S )	z[Parse token for AXT heading. An AXT heading is started with 1 to 6
        symbol of ``#``.Zaxt_1Zaxt_2rA   ÚheadingÚlevelZaxt)r:   ÚtextrU   rF   r   )rZ   rH   rK   Ú_AXT_HEADING_TRIMrJ   r<   r=   )r-   r7   r8   rg   rh   rd   r.   r.   r/   Úparse_axt_heading¹   s   
zBlockParser.parse_axt_headingc                 C   sˆ   |  ¡ }|r+|d dkr+| d¡dkrdnd}d|d< d|d	< d
|i|d< | ¡ d S |  ddg¡}| |j|j¡}|rB|  ||¡S dS )zParse token for setex style heading. A setex heading syntax looks like:

        .. code-block:: markdown

            H1 title
            ========
        r:   Z	paragraphZsetext_1ú=r   é   rf   ZsetextrF   rg   rU   r   r"   N)Ú
last_tokenrH   r=   Ú
compile_scÚmatchr]   ÚcursorÚparse_method)r-   r7   r8   rm   rg   Úscr.   r.   r/   Úparse_setex_headingÆ   s   ÿzBlockParser.parse_setex_headingc                 C   s*  |  ¡ }|r|S | d¡}t|ƒ}|sdS t|j| ¡ dd\}}|du r'dS | j |j|¡}|r6| ¡ }	n|j	}	t
|j||	ƒ\}
}|rVt |j|¡}|rR| ¡ }nd}d}
|du rlt |j|¡}|rh| ¡ }nd}d}|po|}|stdS ||jd vr“t|ƒ}t|ƒ|dœ}|
rŒ|
|d< ||jd |< |S )aã  Parse link references and save the link information into ``state.env``.

        Here is an example of a link reference:

        .. code-block:: markdown

            a [link][example]

            [example]: https://example.com "Optional title"

        This method will save the link reference into ``state.env`` as::

            state.env['ref_links']['example'] = {
                'url': 'https://example.com',
                'title': "Optional title",
            }
        Z	reflink_1NT)ÚblockZ	ref_links)ÚurlÚlabelÚtitle)rG   rH   r   r   r]   r=   Ú
BLANK_LINEr\   r^   r_   r   Ú_BLANK_TO_LINEro   Úenvr   r   )r-   r7   r8   rL   rv   ÚkeyÚhrefZhref_posZ_blankZmax_posrw   Z	title_posÚdatar.   r.   r/   Úparse_ref_linkÛ   sJ   



zBlockParser.parse_ref_linkc                 C   s¦  |  d¡d }t|dƒ}t d|¡}|  g d¢¡}t| |¡ƒ}| ¡ d |_d}|rUt	 |j
|j¡}|rT|  d¡}t d|¡}t|dƒ}t d|¡}||7 }| ¡ |_nxd	}|  g d
¢¡}	|j|jk rÍt	 |j
|j¡}|rœ|  d¡}t d|¡}t|dƒ}t d|¡}||7 }| ¡ |_| ¡ s”d}ntt |¡ƒ}q^|rŸn.|	 |j
|j¡}|r²|  ||¡}|r²n| ¡ }
| |
¡}t|dƒ}||7 }|
|_|j|jk sdt|ƒ|fS )z6Extract text and cursor end position of a block quote.Zquote_1rB   é   rA   )r   r   r   r   Nr   F)r   r   r   r"   r#   T)rH   r
   Ú_BLOCK_QUOTE_TRIMrJ   rn   Úboolro   r=   rp   Ú_STRICT_BLOCK_QUOTEr]   Ú_BLOCK_QUOTE_LEADINGr_   rK   Ú_LINE_BLANK_ENDr\   rq   Úfind_line_endÚget_textr	   )r-   r7   r8   rh   rr   Zrequire_markerrL   ÚquoteZprev_blank_lineZbreak_scÚposÚliner.   r.   r/   Úextract_block_quote  s\   



€




à$zBlockParser.extract_block_quotec                 C   s‚   |   ||¡\}}| |¡}| ¡ | jd kr!t| jƒ}| d¡ n| j}|  ||¡ d|jdœ}|r9| 	|¡ |S | 
|¡ |jS )z­Parse token for block quote. Here is an example of the syntax:

        .. code-block:: markdown

            > a block quote starts
            > with right arrows
        r   r!   )r:   Úchildren)rŠ   Zchild_stateÚdepthr(   r"   r&   ÚremoveÚparseÚtokensZprepend_tokenr<   rp   )r-   r7   r8   rh   rL   ÚchildÚrulesrd   r.   r.   r/   Úparse_block_quote`  s   



zBlockParser.parse_block_quotec                 C   s   t | ||ƒS )z,Parse tokens for ordered and unordered list.)r   r>   r.   r.   r/   r   y  s   zBlockParser.parse_listc                 C   s   |   ||¡S ©N)Úparse_raw_htmlr>   r.   r.   r/   Úparse_block_html}  s   zBlockParser.parse_block_htmlc           	      C   sL  |  d¡ ¡ }|dkrt|d| ¡ ƒS |dkrt|d| ¡ ƒS |dkr+t|d| ¡ ƒS | d¡r8t|d	| ¡ ƒS d }d }| d
¡rT|dd …  ¡ }|tv rSt|| jƒS n$|dd …  ¡ }|t	v rnd
| d	 }t||| ¡ ƒS |tv rxt|| jƒS | 
¡ }|r€|S | ¡ }| ¡ }|r’t |j||¡sœ|r¢t |j||¡r¤t|| jƒS d S d S )Nr   z<!--z-->z<?z?>z	<![CDATA[z]]>z<!ú>z</rl   r   )rH   rK   Ú_parse_html_to_endr=   Ú
startswithÚlowerr   Ú_parse_html_to_newlinerx   r   rG   r…   Ú_OPEN_TAG_ENDro   r]   Ú_CLOSE_TAG_END)	r-   r7   r8   rS   Z	close_tagÚopen_tagZend_tagrL   Ú	start_posr.   r.   r/   r”   €  sD   

ÿÿÿþzBlockParser.parse_raw_htmlr‘   c                 C   sÔ   |   |¡}|j|jk rO| |j|j¡}|sn9| ¡ }||jkr,| |¡}| |¡ ||_|  ||¡}|r8||_n| 	¡ }| |¡}| |¡ ||_|j|jk s|j|jk rh|j|jd … }| |¡ |j|_d S d S r“   )
rn   rp   r_   r\   r]   r^   r†   Zadd_paragraphrq   r…   )r-   r8   r‘   rr   r7   rL   rh   r.   r.   r/   rŽ   °  s.   





î
ýzBlockParser.parse)NNr%   r“   )%Ú__name__Ú
__module__Ú__qualname__rW   rX   r[   rx   r   ZRAW_HTMLÚ_BLOCK_TAGS_PATTERNZ
BLOCK_HTMLr   r   r4   r3   r   r   rY   Úintr2   r   r   r?   r@   rN   re   rj   rs   r~   r   rŠ   r’   r   r•   r”   rŽ   Ú__classcell__r.   r.   r5   r/   r   $   sd    ÿÿÿ
ÿÿÿ

ïü
þ
ýü0BC(0r   c                 C   sh   | j  ||¡}|dkr| j | jd … }| j}n|  |¡}|| _|  ¡ }||  |¡7 }|  d|dœ¡ |S )NrP   r#   ©r:   rE   )r]   rV   rp   r_   r†   r…   r<   )r8   Z
end_markerrž   Z
marker_posrh   rL   r.   r.   r/   r—   Í  s   
r—   c                 C   sR   |  | j| j¡}|r| ¡ }|  |¡}n| j| jd … }| j}|  d|dœ¡ |S )Nr#   r¥   )r\   r]   rp   r^   r†   r_   r<   )r8   Únewliner7   rL   rh   r.   r.   r/   rš   Ü  s   rš   )*rW   Útypingr   r   r   r   Úutilr   r   r	   r
   Úcorer   r   Úhelpersr   r   r   r   r   r   r   r   Zlist_parserr   r   rX   r[   rI   ri   r€   rƒ   r„   ry   Újoinr¢   r›   rœ   r‚   r   r—   rš   r.   r.   r.   r/   Ú<module>   s,    (





   ,