a
    /q_                     @   s2  d Z ddlZddlZdZdZg dZedZedZedZ	ed	Z
ejd
ejdZedejZg dZg dZdZdZddeef ZdZdd Zdd Zd/ddZdd Zd0dd ZG d!d" d"eZG d#d$ d$eZG d%d& d&eZG d'd( d(eZG d)d* d*eZG d+d, d,eZ d1d-d.Z!dS )2z
    mistune
    ~~~~~~~

    The fastest markdown parser in pure Python with renderer feature.

    :copyright: (c) 2014 - 2018 by Hsiaoming Yang.
    Nz0.8.4zHsiaoming Yang <me@lepture.com>)BlockGrammar
BlockLexerInlineGrammarInlineLexerRendererMarkdownmarkdownescapez\s+z\Wz&(?!#?\w+;)z\r\n|\rz^ *> ?flagsz^ {4})!aZemZstrongZsmallsZciteqZdfnZabbrdatatimecodevarZsampZkbdsubZsupibuZmarkZrubyZrtZrpZbdiZbdospanbrZwbrZinsdelZimgZfont)ZpreZscriptZstylez(?!:/|[^\w\s@]*@)\bz7\s*[a-zA-Z\-](?:\s*\=\s*(?:"[^"]*"|'[^']*'|[^\s'">]+))?z(?!(?:%s)\b)\w+%s|)zjavascript:z	vbscript:c                 C   s    | j }|dr|dd  }|S )N^   )pattern
startswith)Zregexr    r   &lib/python3.9/site-packages/mistune.py_pure_pattern+   s    
r!   c                 C   s   t |  dd} td| S )NTquote )r	   lower_key_patternr   )keyr   r   r    _keyify2   s    r(   FTc                 C   sV   |rt d| } n| dd} | dd} | dd} |rR| dd} | d	d
} | S )a3  Replace special characters "&", "<" and ">" to HTML-safe sequences.

    The original cgi.escape will always escape "&", but you can control
    this one for a smart escape amp.

    :param quote: if set to True, " and ' will be escaped.
    :param smart_amp: if set to False, & will always be escaped.
    z&amp;&<z&lt;>z&gt;"z&quot;'z&#39;)_escape_patternr   replace)textr#   	smart_ampr   r   r    r	   7   s    	r	   c                 C   s@   |   d}tD ]}tdd||r dS qt| dddS )zDRemove dangerous URL schemes like javascript: and escape afterwards.z  
	z[^A-Za-z0-9\/:]+ TFr#   r1   )r%   strip_scheme_blacklistrer   r   r	   )urlZ	lower_urlZschemer   r   r    escape_linkL   s
    r8      c                 C   s<   t d| } | |} | dd} tdtj}|d| S )N
u   ␤z^ +$r2   )_newline_patternr   
expandtabsr/   r6   compileM)r0   Ztabr   r   r   r    preprocessingV   s
    
r?   c                   @   s4  e Zd ZdZedZedZedZedZ	edZ
edZedZed	Zed
Zedeeeef ZejdejdZedZedee
ddeeddeeeeeeeeeeeede f	 Zedddeef deef f ZedZedZedZdS )r   z Grammars for block level tokens.zB^ *\[([^^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)z<^\[\^([^\]]+)\]: *([^\n]*(?:\n+|$)(?: {1,}[^\n]*(?:\n+|$))*)z^\n+z^( {4}[^\n]+\n*)+z:^ *(`{3,}|~{3,}) *([^`\s]+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)z&^ {0,3}[-*_](?: *[-*_]){2,} *(?:\n+|$)z%^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)z^([^\n]+)\n *(=|-)+ *(?:\n+|$)z^( *>[^\n]+(\n[^\n]+)*\n*)+z^( *)(?=[*+-]|\d+\.)(([*+-])?(?:\d+\.)?) [\s\S]+?(?:\n+(?=\1?(?:[-*_] *){3,}(?:\n+|$))|\n+(?=%s)|\n+(?=%s)|\n+(?=\1(?(3)\d+\.|[*+-]) )|\n{2,}(?! )(?!\1(?:[*+-]|\d+\.) )\n*|\s*$)z@^(( *)(?:[*+-]|\d+\.) [^\n]*(?:\n(?!\2(?:[*+-]|\d+\.) )[^\n]*)*)r
   z^ *(?:[*+-]|\d+\.) +z2^((?:[^\n]+\n?(?!%s|%s|%s|%s|%s|%s|%s|%s|%s))+)\n*z\1z\2z\3r*   z ^ *(?:%s|%s|%s) *(?:\n{2,}|\s*$)<!--[\s\S]*?-->z <(%s)((?:%s)*?)>([\s\S]*?)<\/\1>z<%s(?:%s)*?\s*\/?>z9^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*z=^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*z^[^\n]+N)__name__
__module____qualname____doc__r6   r=   	def_linksdef_footnotesnewline
block_codefenceshruleheadinglheadingblock_quoter!   
list_blockr>   	list_itemlist_bulletr/   
_block_tag	paragraph_valid_attr
block_htmltablenptabler0   r   r   r   r    r   ^   sp   








r   c                   @   s   e Zd ZdZeZg dZdZdZd0ddZ	d1dd	Z
d2d
dZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ ZdS )3r   z%Block level lexer for block grammars.)rG   rJ   rH   rI   rK   rV   rL   rM   rN   rT   rE   rF   rU   rR   r0   )	rG   rH   rI   rL   rJ   rM   rN   rT   r0   )rG   rH   rI   rK   rV   rL   rJ   rM   rN   rT   rU   rR   r0   Nc                 K   sB   g | _ i | _i | _|s|  }|| _|dd| _d| _d| _d S )NZmax_recursive_depth   r   )	tokensrE   rF   grammar_classrulesget_max_recursive_depth_list_depth_blockquote_depth)selfrZ   kwargsr   r   r    __init__   s    zBlockLexer.__init__c                 C   s   |  ||S Nparser_   r0   rZ   r   r   r    __call__   s    zBlockLexer.__call__c                    sf   | d} sj  fdd}|r`||}|durN|t|dd  }q"|r"td| q"jS )Nr:   c                    sB    D ]8}t j|}|| }|s$qt d| | |  S dS )Nzparse_%sF)getattrrZ   match)r0   r'   ZrulemrZ   r_   r   r    
manipulate   s    
z$BlockLexer.parse.<locals>.manipulateFr   Infinite loop at: %s)rstripdefault_ruleslengroupRuntimeErrorrX   )r_   r0   rZ   rk   ri   r   rj   r    rd      s    

zBlockLexer.parsec                 C   s*   t |d}|dkr&| jddi d S )Nr   r   typerG   )ro   rp   rX   append)r_   ri   lengthr   r   r    parse_newline   s    zBlockLexer.parse_newlinec                 C   s*   t d|d}| jdd |d d S )Nr2   r   r   rr   langr0   )_block_code_leading_patternr   rp   rX   rs   )r_   ri   r   r   r   r    parse_block_code   s    zBlockLexer.parse_block_codec                 C   s$   | j d|d|dd d S )Nr         rv   rX   rs   rp   r_   ri   r   r   r    parse_fences   s
    zBlockLexer.parse_fencesc                 C   s(   | j dt|d|dd d S )NrK   r   rz   rr   levelr0   )rX   rs   ro   rp   r}   r   r   r    parse_heading  s
    zBlockLexer.parse_headingc                 C   s0   | j d|ddkrdnd|dd dS )zParse setext heading.rK   rz   =r   r   Nr|   r}   r   r   r    parse_lheading  s
    zBlockLexer.parse_lheadingc                 C   s   | j ddi d S )Nrr   rJ   )rX   rs   r}   r   r   r    parse_hrule  s    zBlockLexer.parse_hrulec                 C   s   | d}| jdd|v d |  jd7  _| j| jkrf| jddi | | | jddi n| d	}| || | jdd
i |  jd8  _d S )Nrz   Z
list_start.)rr   orderedr   rr   list_item_startlist_item_endr   list_end)rp   rX   rs   r]   r\   
parse_text_process_list_item)r_   ri   bullcapr   r   r    parse_list_block  s    


zBlockLexer.parse_list_blockc                 C   s  | j j|}d}t|}t|D ]}|| d }t|}| j jd|}d|v r~|t| }tjd| tj	d}|d|}|}	|	st
d|rd}	t|}
||d	 kr|
r||
d	  d
k}|	s|}	|	rd}nd}| jd|i | || j | jddi q"d S )NFr   r2   z
 z^ {1,%d}r
   z\n\n(?!\s*$)Tr   r:   Zloose_item_startr   rr   r   )rZ   rO   findallro   rangerP   r   r6   r=   r>   searchrX   rs   rd   
list_rules)r_   r   r   Z_nextrt   r   itemspacer   Zlooseresttr   r   r    r   $  s2    zBlockLexer._process_list_itemc                 C   st   | j ddi |  jd7  _| j| jkr6| | ntd|d}| | | j ddi |  jd8  _d S )Nrr   Zblock_quote_startr   r2   r   block_quote_end)	rX   rs   r^   r\   r   _block_quote_leading_patternr   rp   rd   )r_   ri   r   r   r   r    parse_block_quoteL  s    
zBlockLexer.parse_block_quotec                 C   s.   t |d}|d|dd| j|< d S )Nr   rz   r{   )linktitle)r(   rp   rE   r_   ri   r'   r   r   r    parse_def_linksX  s    zBlockLexer.parse_def_linksc           	      C   s   t |d}|| jv rd S d| j|< | jd|d |d}d|v r|d}d }|dd  D ],}t|t|  }|rd|r||k rd|}qd|d g}|dd  D ]}|||d   qd|}| 	|| j
 | jd|d d S )Nr   r   Zfootnote_start)rr   r'   rz   r:   footnote_end)r(   rp   rF   rX   rs   splitro   lstripjoinrd   footnote_rules)	r_   ri   r'   r0   linesZ
whitespaceliner   newlinesr   r   r    parse_def_footnotes_  s2    





zBlockLexer.parse_def_footnotesc                 C   sv   |  |}tdd|d}|d}t|D ]&\}}tdd|}td|||< q0| ||d< | j| d S )Nz(?: *\| *)?\n$r2   r{   r:   z^ *\| *| *\| *$ *(?<!\\)\| *cells	_process_tabler6   r   rp   r   	enumerate_process_cellsrX   rs   r_   ri   r   r   r   vr   r   r    parse_table  s    

zBlockLexer.parse_tablec                 C   sh   |  |}tdd|d}|d}t|D ]\}}td|||< q0| ||d< | j| d S )Nz\n$r2   r{   r:   r   r   r   r   r   r   r    parse_nptable  s    

zBlockLexer.parse_nptablec                 C   s   t dd|d}t d|}t dd|d}t d|}t|D ]R\}}t d|rfd||< qHt d	|r|d
||< qHt d|rd||< qHd ||< qHd||d}|S )Nz^ *| *\| *$r2   r   z *\| *z *|\| *$rz   z	^ *-+: *$rightz
^ *:-+: *$centerz	^ *:-+ *$leftrU   )rr   headeralign)r6   r   rp   r   r   r   )r_   ri   r   r   r   r   r   r   r   r    r     s"    



zBlockLexer._process_tablec                 C   s>   t |D ]0\}}t |D ]\}}tdd||| |< qq|S )Nz\\\|r   )r   r6   r   )r_   r   r   r   ccellr   r   r    r     s    zBlockLexer._process_cellsc                 C   sZ   | d}|s,| d}| jd|d n*| d}| d}| jd|||d d S )	Nr   r   Z
close_htmlrr   r0   rz   r{   Z	open_html)rr   tagextrar0   rp   rX   rs   )r_   ri   r   r0   attrr   r   r    parse_block_html  s    




zBlockLexer.parse_block_htmlc                 C   s&   | dd}| jd|d d S )Nr   r:   rR   r   )rp   rm   rX   rs   r_   ri   r0   r   r   r    parse_paragraph  s    zBlockLexer.parse_paragraphc                 C   s    | d}| jd|d d S )Nr   r0   r   r   r   r   r   r    r     s    
zBlockLexer.parse_text)N)N)N)rA   rB   rC   rD   r   rY   rn   r   r   ra   rf   rd   ru   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r      s4   


	("r   c                   @   s   e Zd ZdZedZedddeef deef f Z	edZ
edZed	Zed
ZedZedZedZedZedZedZedZedZdd ZdS )r   z!Grammars for inline level tokens.z^\\([\\`*{}\[\]()#+\-.!_>~|])z^(?:%s|%s|%s)r@   z&<(\w+%s)((?:%s)*?)\s*>([\s\S]*?)<\/\1>z<\w+%s(?:%s)*?\s*\/?>z^<([^ >]+(@|:)[^ >]+)>zi^!?\[((?:\[[^^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\(\s*(<)?([\s\S]*?)(?(2)>)(?:\s+['"]([\s\S]*?)['"])?\s*\)z@^!?\[((?:\[[^^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\s*\[([^^\]]*)\]z ^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]z$^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])z4^_{2}([\s\S]+?)_{2}(?!_)|^\*{2}([\s\S]+?)\*{2}(?!\*)z4^\b_((?:__|[^_])+?)_\b|^\*((?:\*\*|[^\*])+?)\*(?!\*)z ^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)z^ {2,}\n(?!\s*$)z^~~(?=\S)([\s\S]*?\S)~~z^\[\^([^\]]+)\]z-^[\s\S]+?(?=[\\<!\[_*`~]|https?://| {2,}\n|$)c                 C   s   t d| _t d| _dS )zhGrammar for hard wrap linebreak. You don't need to add two
        spaces at the end of a line.
        z^ *\n(?!\s*$)z*^[\s\S]+?(?=[\\<!\[_*`~]|https?://| *\n|$)N)r6   r=   	linebreakr0   r_   r   r   r    	hard_wrap  s    zInlineGrammar.hard_wrapN)rA   rB   rC   rD   r6   r=   r	   
_valid_endrS   inline_htmlautolinkr   reflinknolinkr7   double_emphasisemphasisr   r   strikethroughfootnoter0   r   r   r   r   r    r     s@   









r   c                   @   s   e Zd ZdZeZg dZg dZd+ddZd,ddZ	d	d
 Z
d-ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd.ddZdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* ZdS )/r   z'Inline level lexer for inline grammars.)r	   r   r   r7   r   r   r   r   r   r   r   r   r   r0   )r	   r   r   r7   r   r   r   r   r   r   r   r   r0   Nc                 K   sf   || _ i | _i | _d| _|s$|  }|| j j |drD|  || _	d| _
d| _|d| _d S )Nr   r   FZparse_inline_html)rendererlinks	footnotesfootnote_indexrY   updateoptionsr[   r   rZ   _in_link_in_footnote_parse_inline_html)r_   r   rZ   r`   r   r   r    ra     s    
zInlineLexer.__init__c                 C   s   |  ||S rb   )outputre   r   r   r    rf   *  s    zInlineLexer.__call__c                 C   s   d| _ |pi | _|pi | _d S Nr   )r   r   r   )r_   r   r   r   r   r    setup-  s    
zInlineLexer.setupc                    s   | d} stj jr0d v r0 d j } fdd}|r||}|dur|\}}||7 }|t|dd  }qH|rHt	d| qH|S )Nr:   r   c                    sV    D ]L}t j|}|| }|s$q|_t d| |}|d ur||f  S qdS )N	output_%sF)rg   rZ   rh   Z
line_match)r0   r'   r   ri   outrj   r   r    rk   <  s    
z&InlineLexer.output.<locals>.manipulateFr   rl   )
rm   listrn   r   remover   placeholderro   rp   rq   )r_   r0   rZ   r   rk   retri   r   r   rj   r    r   2  s"    



zInlineLexer.outputc                 C   s   | d}| j|S Nr   )rp   r   r	   r   r   r   r    output_escapeT  s    
zInlineLexer.output_escapec                 C   s0   | d}| ddkrd}nd}| j||S )Nr   rz   @TF)rp   r   r   )r_   ri   r   is_emailr   r   r    output_autolinkX  s
    
zInlineLexer.output_autolinkc                 C   s*   | d}| jr| j|S | j|dS )Nr   F)rp   r   r   r0   r   )r_   ri   r   r   r   r    
output_url`  s    
zInlineLexer.output_urlc                 C   s   | d}| jrx|tv rx| d}|dkrHd| _| j|| jd}d| _n| j|| jd}| dpdd}d	||||f }n
| d
}| j|S )Nr   r{   r   TrZ   Frz   r2   <%s%s>%s</%s>r   )rp   r   _inline_tagsr   r   inline_html_rulesr   r   )r_   ri   r   r0   r   htmlr   r   r    output_inline_htmlf  s    


zInlineLexer.output_inline_htmlc                 C   sT   t |d}|| jvrd S | j| r*d S |  jd7  _| j| j|< | j|| jS r   )r(   rp   r   r   r   footnote_refr   r   r   r    output_footnotev  s    

zInlineLexer.output_footnotec                 C   s   |  ||d|dS )Nr{   r9   )_process_linkrp   r}   r   r   r    output_link  s    zInlineLexer.output_linkc                 C   sF   t |dp|d}|| jvr&d S | j| }| ||d |d S )Nrz   r   r   r   r(   rp   r   r   r_   ri   r'   r   r   r   r    output_reflink  s
    

zInlineLexer.output_reflinkc                 C   s<   t |d}|| jvrd S | j| }| ||d |d S )Nr   r   r   r   r   r   r   r    output_nolink  s
    

zInlineLexer.output_nolinkc                 C   sV   | d}| d}|d dkr0| j|||S d| _| |}d| _| j|||S )Nr   r   !TF)rp   r   imager   r   r   )r_   ri   r   r   r   r0   r   r   r    r     s    


zInlineLexer._process_linkc                 C   s*   | dp| d}| |}| j|S Nrz   r   )rp   r   r   r   r   r   r   r    output_double_emphasis  s    
z"InlineLexer.output_double_emphasisc                 C   s*   | dp| d}| |}| j|S r   )rp   r   r   r   r   r   r   r    output_emphasis  s    
zInlineLexer.output_emphasisc                 C   s   | d}| j|S )Nrz   )rp   r   codespanr   r   r   r    output_code  s    
zInlineLexer.output_codec                 C   s
   | j  S rb   )r   r   r}   r   r   r    output_linebreak  s    zInlineLexer.output_linebreakc                 C   s   |  |d}| j|S r   )r   rp   r   r   r   r   r   r    output_strikethrough  s    z InlineLexer.output_strikethroughc                 C   s   | d}| j|S r   )rp   r   r0   r   r   r   r    output_text  s    
zInlineLexer.output_text)N)N)N)N)rA   rB   rC   rD   r   rY   rn   r   ra   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r     s.   


"

r   c                   @   s   e Zd ZdZdd Zdd Zd=ddZd	d
 Zdd Zd>ddZ	dd Z
d?ddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd@d-d.Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< ZdS )Ar   z6The default HTML renderer for rendering Markdown.
    c                 K   s
   || _ d S rb   )r   )r_   r`   r   r   r    ra     s    zRenderer.__init__c                 C   s   dS )a  Returns the default, empty output value for the renderer.

        All renderer methods use the '+=' operator to append to this value.
        Default is a string so rendering HTML can build up a result string with
        the rendered Markdown.

        Can be overridden by Renderer subclasses to be types like an empty
        list, allowing the renderer to create a tree-like structure to
        represent the document (which can then be reprocessed later into a
        separate format like docx or pdf).
        r2   r   r   r   r   r    r     s    zRenderer.placeholderNc                 C   s<   | d}|s"t|dd}d| S t|ddd}d||f S )zRendering block level code. ``pre > code``.

        :param code: text content of the code block.
        :param lang: language of the given code.
        r:   Fr1   z<pre><code>%s
</code></pre>
Tr3   z,<pre><code class="lang-%s">%s
</code></pre>
)rm   r	   )r_   r   rw   r   r   r    rH     s    
zRenderer.block_codec                 C   s   d| d S )zjRendering <blockquote> with the given text.

        :param text: text content of the blockquote.
        z<blockquote>%s
</blockquote>
r:   )rm   r_   r0   r   r   r    rM     s    zRenderer.block_quotec                 C   s6   | j dr| drdS | j dr2t|S |S )ziRendering block level pure html content.

        :param html: text content of the html snippet.
        Z
skip_stylez<styler2   r	   )r   r[   r%   r   r	   r_   r   r   r   r    rT     s    zRenderer.block_htmlc                 C   s   d|||f S )zRendering header/heading tags like ``<h1>`` ``<h2>``.

        :param text: rendered text content for the header.
        :param level: a number for the header level, for example: 1.
        :param raw: raw text content of the header.
        z<h%d>%s</h%d>
r   )r_   r0   r   rawr   r   r    r     s    zRenderer.headerc                 C   s   | j drdS dS )z"Rendering method for ``<hr>`` tag.	use_xhtmlz<hr />
z<hr>
r   r[   r   r   r   r    rJ     s    zRenderer.hruleTc                 C   s   d}|rd}d|||f S )zRendering list tags like ``<ul>`` and ``<ol>``.

        :param body: body contents of the list.
        :param ordered: whether this list is ordered or not.
        ZulZolz<%s>
%s</%s>
r   )r_   bodyr   r   r   r   r    r     s    zRenderer.listc                 C   s   d| S )z+Rendering list item snippet. Like ``<li>``.z<li>%s</li>
r   r   r   r   r    rO     s    zRenderer.list_itemc                 C   s   d| d S )z'Rendering paragraph tags. Like ``<p>``.z
<p>%s</p>
r$   )r4   r   r   r   r    rR   	  s    zRenderer.paragraphc                 C   s   d||f S )zRendering table element. Wrap header and body in it.

        :param header: header part of the table.
        :param body: body part of the table.
        z6<table>
<thead>%s</thead>
<tbody>
%s</tbody>
</table>
r   )r_   r   r  r   r   r    rU     s    zRenderer.tablec                 C   s   d| S )zeRendering a table row. Like ``<tr>``.

        :param content: content of current table row.
        z<tr>
%s</tr>
r   )r_   contentr   r   r    	table_row  s    zRenderer.table_rowc                 K   s<   |d rd}nd}|d }|s,d|||f S d||||f S )zRendering a table cell. Like ``<th>`` ``<td>``.

        :param content: content of current table cell.
        :param header: whether this is header or not.
        :param align: align of current table cell.
        r   ZthZtdr   z<%s>%s</%s>
z"<%s style="text-align:%s">%s</%s>
r   )r_   r  r   r   r   r   r   r    
table_cell  s    zRenderer.table_cellc                 C   s   d| S )zTRendering **strong** text.

        :param text: text content for emphasis.
        z<strong>%s</strong>r   r   r   r   r    r   1  s    zRenderer.double_emphasisc                 C   s   d| S )zTRendering *emphasis* text.

        :param text: text content for emphasis.
        z<em>%s</em>r   r   r   r   r    r   8  s    zRenderer.emphasisc                 C   s   t | dd}d| S )zZRendering inline `code` text.

        :param text: text content for inline code.
        Fr   z<code>%s</code>)r	   rm   r   r   r   r    r   ?  s    zRenderer.codespanc                 C   s   | j drdS dS )z#Rendering line break like ``<br>``.r   z<br />
z<br>
r  r   r   r   r    r   G  s    zRenderer.linebreakc                 C   s   d| S )z`Rendering ~~strikethrough~~ text.

        :param text: text content for strikethrough.
        z<del>%s</del>r   r   r   r   r    r   M  s    zRenderer.strikethroughc                 C   s   | j dr|S t|S )zHRendering unformatted text.

        :param text: text content.
        r   r   r[   r	   r   r   r   r    r0   T  s    zRenderer.textc                 C   s   t |S )zGRendering escape sequence.

        :param text: text content.
        r	   r   r   r   r    r	   ]  s    zRenderer.escapeFc                 C   s$   t | }}|rd| }d||f S )zRendering a given link or email address.

        :param link: link content or email address.
        :param is_email: whether this is an email or not.
        z	mailto:%s<a href="%s">%s</a>)r8   )r_   r   r   r0   r   r   r    r   d  s    zRenderer.autolinkc                 C   s2   t |}|sd||f S t|dd}d|||f S )zRendering a given link with content and title.

        :param link: href link for ``<a>`` tag.
        :param title: title content for `title` attribute.
        :param text: text content for description.
        r  Tr"   z<a href="%s" title="%s">%s</a>)r8   r	   )r_   r   r   r0   r   r   r    r   o  s
    zRenderer.linkc                 C   s\   t |}t|dd}|r4t|dd}d|||f }nd||f }| jdrTd| S d| S )zRendering a image with title and text.

        :param src: source link of the image.
        :param title: title text of the image.
        :param text: alt text of the image.
        Tr"   z!<img src="%s" alt="%s" title="%s"z<img src="%s" alt="%s"r   z%s />z%s>)r8   r	   r   r[   )r_   srcr   r0   r   r   r   r    r   |  s    zRenderer.imagec                 C   s   | j drt|S |S )zhRendering span level pure html content.

        :param html: text content of the html snippet.
        r	   r  r   r   r   r    r     s    zRenderer.inline_htmlc                 C   s   dS )zRendering newline element.r2   r   r   r   r   r    rG     s    zRenderer.newlinec                 C   s   dt |t ||f }|S )zRendering the ref anchor of a footnote.

        :param key: identity key for the footnote.
        :param index: the index count of current footnote.
        zE<sup class="footnote-ref" id="fnref-%s"><a href="#fn-%s">%d</a></sup>r  )r_   r'   indexr   r   r   r    r     s    zRenderer.footnote_refc                 C   sR   dt | }| }|dr2tdd| |}nd||f }dt ||f }|S )zRendering a footnote item.

        :param key: identity key for the footnote.
        :param text: text content of the footnote.
        z0<a href="#fnref-%s" class="footnote">&#8617;</a>z</p>z<\/p>$z%s</p>z%s<p>%s</p>z<li id="fn-%s">%s</li>
)r	   rm   endswithr6   r   )r_   r'   r0   Zbackr   r   r   r    footnote_item  s    
zRenderer.footnote_itemc                 C   s   d}||   |f S )zTWrapper for all footnotes.

        :param text: contents of all footnotes.
        z-<div class="footnotes">
%s<ol>%s</ol>
</div>
)rJ   )r_   r0   r   r   r   r    r     s    zRenderer.footnotes)N)N)T)F) rA   rB   rC   rD   ra   r   rH   rM   rT   r   rJ   r   rO   rR   rU   r  r  r   r   r   r   r   r0   r	   r   r   r   r   rG   r   r  r   r   r   r   r    r     s:   

	
	
	r   c                   @   s   e Zd ZdZd1ddZdd Zdd Zd	d
 Zdd Zdd Z	d2ddZ
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )3r   zThe Markdown parser.

    :param renderer: An instance of ``Renderer``.
    :param inline: An inline lexer class or instance.
    :param block: A block lexer class or instance.
    Nc                 K   s   |st f i |}n||j || _|rDt|rD||fi |}|r`t|r`|f i |}|rl|| _nt|fi || _|ptt	 | _
g | _g | _|d| _d S )Nr   )r   r   r   r   inspectZisclassinliner   r   r   blockr   rX   r[   _parse_block_html)r_   r   r  r  r`   r   r   r    ra     s    zMarkdown.__init__c                 C   s
   |  |S rb   rc   r   r   r   r    rf     s    zMarkdown.__call__c                 C   s
   |  |S )zYRender the Markdown text.

        :param text: markdown formatted text content.
        rc   r   r   r   r    render  s    zMarkdown.renderc                    s   |  t|}| jj i | j_i | j_i | j_i | j_| js@|S t fdd| j}t	| fdddd| _| j
 }| jr| j }|| j
|d |d 7 }qv|| j
|7 }|S )Nc                    s     | d S Nr'   r[   okeysr   r    <lambda>      z Markdown.parse.<locals>.<lambda>c                    s     | d S r  r  r  r  r   r    r    r  T)r'   reverser'   r0   )r   r?   r  rF   rE   r  r   r   filtersortedr   r   popr  )r_   r0   r   r   r  Znoter   r  r    rd     s(    

zMarkdown.parsec                 C   s   | j s
d S | j  | _| jS rb   )rX   r  tokenr   r   r   r    r  
  s    zMarkdown.popc                 C   s   | j r| j d S d S )N)rX   r   r   r   r    peek  s    
zMarkdown.peekc                 C   sR   |  ||| _| j  | j| j j| j j | j }| 	 rN|| 
 7 }q8|S rb   )r  rX   r  r  r   rE   rF   r   r   r  tok)r_   r0   rZ   r   r   r   r    r     s    

zMarkdown.outputc                 C   s0   | j d }|dr |d d }t| d|  S )Nrr   Z_startir   )r  r  rg   )r_   r   r   r   r    r!     s    

zMarkdown.tokc                 C   s:   | j d }|  d dkr0|d|  d  7 }q
| |S )Nr0   rr   r:   )r  r   r  r  r   r   r   r    tok_text)  s    
zMarkdown.tok_textc                 C   s
   | j  S rb   )r   rG   r   r   r   r    output_newline/  s    zMarkdown.output_newlinec                 C   s
   | j  S rb   )r   rJ   r   r   r   r    output_hrule2  s    zMarkdown.output_hrulec                 C   s(   | j | | jd | jd | jd S )Nr0   r   )r   r   r  r  r   r   r   r    output_heading5  s
    zMarkdown.output_headingc                 C   s   | j | jd | jd S )Nr0   rw   )r   rH   r  r   r   r   r    r   <  s    zMarkdown.output_codec                 C   s"  | j d }t|}| j }| j }t| j d D ]D\}}||k rL|| nd }d|d}|| jj| |fi |7 }q4|| j|7 }| j }	t| j d D ]p\}}
| j }t|
D ]D\}}||k r|| nd }d|d}|| jj| |fi |7 }q|	| j|7 }	q| j||	S )Nr   r   T)r   r   r   F)	r  ro   r   r   r   r  r  r  rU   )r_   ZalignsZaligns_lengthr   r   r   valuer   r   r  rowjr   r   r    output_tableA  s$    



 


 zMarkdown.output_tablec                 C   s4   | j  }|  d dkr(||  7 }q
| j |S )Nrr   r   )r   r   r  r!  rM   r_   r  r   r   r    output_block_quote[  s    
zMarkdown.output_block_quotec                 C   s@   | j d }| j }|  d dkr2||  7 }q| j||S )Nr   rr   r   )r  r   r   r  r!  r   )r_   r   r  r   r   r    output_lista  s
    

zMarkdown.output_listc                 C   sP   | j  }|  d dkrD| jd dkr6||  7 }q
||  7 }q
| j |S )Nrr   r   r0   )r   r   r  r  r"  r!  rO   r*  r   r   r    output_list_itemh  s    
zMarkdown.output_list_itemc                 C   s4   | j  }|  d dkr(||  7 }q
| j |S )Nrr   r   )r   r   r  r!  rO   r*  r   r   r    output_loose_itemr  s    
zMarkdown.output_loose_itemc                 C   s^   d| j _| j }| jd }|  d dkr:||  7 }q| j||d d| j _| j S )NTr'   rr   r   )r'   r0   F)	r  r   r   r   r  r  r!  r   rs   )r_   r  r'   r   r   r    r   x  s    

zMarkdown.output_footnotec                 C   s   | j d }| j|S Nr0   )r  r   rT   r   r   r   r    output_close_html  s    
zMarkdown.output_close_htmlc                 C   s`   | j d }| j d }| jr4|tvr4| j|| jjd}| j dpBd}d||||f }| j|S )Nr0   r   r   r   r2   r   )r  r  	_pre_tagsr  r   r[   r   rT   )r_   r0   r   r   r   r   r   r    output_open_html  s    

zMarkdown.output_open_htmlc                 C   s   | j | | jd S r/  )r   rR   r  r  r   r   r   r    output_paragraph  s    zMarkdown.output_paragraphc                 C   s   | j |  S rb   )r   rR   r"  r   r   r   r    r     s    zMarkdown.output_text)NNN)N)rA   rB   rC   rD   ra   rf   r  rd   r  r   r   r!  r"  r#  r$  r%  r   r)  r+  r,  r-  r.  r   r0  r2  r3  r   r   r   r   r    r     s0   

	

	r   c                 K   s   t f d|i|| S )a  Render markdown formatted text to html.

    :param text: markdown formatted text content.
    :param escape: if set to False, all html tags will not be escaped.
    :param use_xhtml: output with xhtml tags.
    :param hard_wrap: if set to True, it will use the GFM line breaks feature.
    :param parse_block_html: parse text only in block level html.
    :param parse_inline_html: parse text only in inline level html.
    r	   )r   )r0   r	   r`   r   r   r    r     s    
r   )FT)r9   )T)"rD   r6   r  __version__
__author____all__r=   r&   Z_nonalpha_patternr.   r;   r>   r   rx   r   r1  r   rS   r   rQ   r5   r!   r(   r	   r8   r?   objectr   r   r   r   r   r   r   r   r   r   r    <module>   sF   	






O  '5 0   V