U
    ú±d=a  ã                   @   s  d Z ddlZddlZddlm  mZ ddlmZ ddlm	Z	 e 
d¡Zdd„ ZG d	d
„ 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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dS )!aÇ  
Python Markdown

A Python implementation of John Gruber's Markdown.

Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).

CORE MARKDOWN BLOCKPARSER
===========================================================================

This parser handles basic parsing of Markdown blocks.  It doesn't concern
itself with inline elements such as **bold** or *italics*, but rather just
catches blocks, lists, quotes, etc.

The BlockParser is made up of a bunch of BlockProcessors, each handling a
different type of block. Extensions may add/replace/remove BlockProcessors
as they need to alter how markdown blocks are parsed.
é    Né   )Úutil)ÚBlockParserZMARKDOWNc                 K   sè   t | ƒ}|j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒd	d
¡ |j t|ƒdd¡ |j t	|ƒdd¡ |j t
|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |j t|ƒdd¡ |S )z2 Build the default block parser used by Markdown. Úemptyéd   ÚindentéZ   ÚcodeéP   Z
hashheaderéF   Zsetextheaderé<   Úhré2   Zolisté(   Zulisté   Úquoteé   Ú	referenceé   Z	paragraphé
   )r   ZblockprocessorsÚregisterÚEmptyBlockProcessorÚListIndentProcessorÚCodeBlockProcessorÚHashHeaderProcessorÚSetextHeaderProcessorÚHRProcessorÚOListProcessorÚUListProcessorÚBlockQuoteProcessorÚReferenceProcessorÚParagraphProcessor)ÚmdÚkwargsÚparser© r%   ú</tmp/pip-unpacked-wheel-muih9_xp/markdown/blockprocessors.pyÚbuild_block_parser*   s    r'   c                   @   sD   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	dS )ÚBlockProcessora›   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.

    c                 C   s   || _ |jj| _d S ©N)r$   r"   Ú
tab_length©Úselfr$   r%   r%   r&   Ú__init__F   s    zBlockProcessor.__init__c                 C   s   t |ƒr|d S dS dS )z, Return the last child of an etree element. éÿÿÿÿN)Úlen)r,   Úparentr%   r%   r&   Ú	lastChildJ   s    zBlockProcessor.lastChildNc                 C   s€   |dkr| j }g }| d¡}|D ]>}| d| ¡rF| ||d… ¡ q | ¡ sZ| d¡ q  q`q d |¡d |t|ƒd… ¡fS )z= Remove a tab from the front of each line of the given text. NÚ
ú Ú )r*   ÚsplitÚ
startswithÚappendÚstripÚjoinr/   )r,   ÚtextÚlengthZnewtextÚlinesÚliner%   r%   r&   ÚdetabQ   s    
zBlockProcessor.detabr   c                 C   sX   |  d¡}tt|ƒƒD ]6}||  d| j | ¡r|| | j| d… ||< qd |¡S )z? Remove a tab from front of lines but allowing dedented lines. r2   r3   N)r5   Úranger/   r6   r*   r9   )r,   r:   Úlevelr<   Úir%   r%   r&   Ú
looseDetab`   s
    
zBlockProcessor.looseDetabc                 C   s   dS )ae   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.

        Keywords:

        * ``parent``: A 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.
        Nr%   ©r,   r0   Úblockr%   r%   r&   Útesth   s    zBlockProcessor.testc                 C   s   dS )a½   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.

        Keywords:

        * ``parent``: A etree element which is the parent of the current block.
        * ``blocks``: A list of all remaining blocks of the document.
        Nr%   )r,   r0   Úblocksr%   r%   r&   Úrun|   s    zBlockProcessor.run)N)r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r1   r>   rB   rE   rG   r%   r%   r%   r&   r(   ;   s   


r(   c                       sN   e Zd ZdZdgZddgZ‡ fdd„Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
‡  ZS )r   z‚ Process children of list items.

    Example:
        * a list item
            process this part

            or this part

    ÚliÚulÚolc                    s"   t ƒ j|Ž  t d| j ¡| _d S )Nz^(([ ]{%s})+))Úsuperr-   ÚreÚcompiler*   Ú	INDENT_RE)r,   Úargs©Ú	__class__r%   r&   r-   £   s    zListIndentProcessor.__init__c                 C   sP   |  d| j ¡oN| jj d¡ oN|j| jkpNt|ƒoN|d d k	oN|d j| jkS )Nr3   Údetabbedr.   )	r6   r*   r$   ÚstateÚisstateÚtagÚ
ITEM_TYPESr/   Ú
LIST_TYPESrC   r%   r%   r&   rE   §   s    ÿüzListIndentProcessor.testc                 C   s$  |  d¡}|  ||¡\}}|  ||¡}| jj d¡ |j| jkr€t|ƒrn|d j| j	krn| j 
|d |g¡ n| j 
||g¡ n”|j| jkrž| j 
||g¡ nvt|ƒr|d j| jkr|d jrôt d¡}|d j|_d|d _|d  d|¡ | j |d |¡ n|  ||¡ | jj ¡  d S )Nr   rV   r.   Úpr4   )ÚpopÚ	get_levelrB   r$   rW   ÚsetrY   rZ   r/   r[   ÚparseBlocksr:   ÚetreeÚElementÚinsertÚ
parseChunkÚcreate_itemÚreset)r,   r0   rF   rD   r@   Úsiblingr\   r%   r%   r&   rG   ®   s&    



zListIndentProcessor.runc                 C   s    t  |d¡}| j ||g¡ dS )z< Create a new li and parse the block with it as the parent. rL   N)ra   Ú
SubElementr$   r`   )r,   r0   rD   rL   r%   r%   r&   re   Ò   s    zListIndentProcessor.create_itemc                 C   sš   | j  |¡}|r&t| d¡ƒ| j }nd}| jj d¡r>d}nd}||kr’|  |¡}|dk	r’|j	| j
kst|j	| jkr’|j	| j
krˆ|d7 }|}qBq’qB||fS )z* Get level of indent based on list level. r   r   ÚlistN)rR   Úmatchr/   Úgroupr*   r$   rW   rX   r1   rY   r[   rZ   )r,   r0   rD   ÚmÚindent_levelr@   Úchildr%   r%   r&   r^   ×   s&    

ÿ
ÿzListIndentProcessor.get_level)rH   rI   rJ   rK   rZ   r[   r-   rE   rG   re   r^   Ú__classcell__r%   r%   rT   r&   r   •   s   
$r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z Process code blocks. c                 C   s   |  d| j ¡S )Nr3   )r6   r*   rC   r%   r%   r&   rE   ÷   s    zCodeBlockProcessor.testc              	   C   sÌ   |   |¡}| d¡}d}|d k	rx|jdkrxt|ƒrx|d jdkrx|d }|  |¡\}}t d |jt 	| 
¡ ¡¡¡|_n@t |d¡}t |d¡}|  |¡\}}t dt 	| 
¡ ¡ ¡|_|rÈ| d|¡ d S )Nr   r4   Úprer	   z{}
{}
z%s
)r1   r]   rY   r/   r>   r   ÚAtomicStringÚformatr:   Zcode_escapeÚrstripra   rh   rc   )r,   r0   rF   rg   rD   ÚtheRestr	   rp   r%   r%   r&   rG   ú   s&    

ÿÿÿzCodeBlockProcessor.runN©rH   rI   rJ   rK   rE   rG   r%   r%   r%   r&   r   ô   s   r   c                   @   s.   e Zd Ze d¡Zdd„ Zdd„ Zdd„ ZdS )	r   z(^|\n)[ ]{0,3}>[ ]?(.*)c                 C   s   t | j |¡ƒot ¡  S r)   )ÚboolÚREÚsearchr   Znearing_recursion_limitrC   r%   r%   r&   rE     s    zBlockQuoteProcessor.testc                    s¾   |  d¡}ˆ j |¡}|rd|d | ¡ … }ˆ j ||g¡ d ‡ fdd„|| ¡ d …  d¡D ƒ¡}ˆ  |¡}|d k	r†|j	dkr†|}nt
 |d¡}ˆ jj d¡ ˆ j ||¡ ˆ jj ¡  d S )Nr   r2   c                    s   g | ]}ˆ   |¡‘qS r%   )Úclean)Ú.0r=   ©r,   r%   r&   Ú
<listcomp>%  s     z+BlockQuoteProcessor.run.<locals>.<listcomp>Ú
blockquote)r]   rw   rx   Ústartr$   r`   r9   r5   r1   rY   ra   rh   rW   r_   rd   rf   )r,   r0   rF   rD   rl   Úbeforerg   r   r%   r{   r&   rG     s    
"ÿ
zBlockQuoteProcessor.runc                 C   s2   | j  |¡}| ¡ dkrdS |r*| d¡S |S dS )z( Remove ``>`` from beginning of a line. ú>r4   é   N)rw   rj   r8   rk   )r,   r=   rl   r%   r%   r&   ry   4  s    
zBlockQuoteProcessor.cleanN)	rH   rI   rJ   rP   rQ   rw   rE   rG   ry   r%   r%   r%   r&   r     s   
r   c                       sL   e Zd ZdZdZdZdZddgZ‡ fdd„Zdd	„ Z	d
d„ Z
dd„ Z‡  ZS )r   z Process ordered list blocks. rN   Ú1TrM   c                    s\   t ƒ  |¡ t d| jd  ¡| _t d| jd  ¡| _t d| j| jd d f ¡| _d S )Nz^[ ]{0,%d}\d+\.[ ]+(.*)r   z!^[ ]{0,%d}((\d+\.)|[*+-])[ ]+(.*)z ^[ ]{%d,%d}((\d+\.)|[*+-])[ ]+.*r   )rO   r-   rP   rQ   r*   rw   ÚCHILD_RErR   r+   rT   r%   r&   r-   M  s    ÿÿzOListProcessor.__init__c                 C   s   t | j |¡ƒS r)   ©rv   rw   rj   rC   r%   r%   r&   rE   X  s    zOListProcessor.testc                 C   s˜  |   | d¡¡}|  |¡}|d k	rì|j| jkrì|}|d jrlt d¡}|d j|_d|d _|d  d|¡ |  |d ¡}|d k	rª|j	rªt 
|d d¡}|j	 ¡ |_d|_	t 
|d¡}| jj d¡ | d¡}	| j ||	g¡ | jj ¡  n>|jdkrü|}n.t 
|| j¡}| js*| jdkr*| j|jd	< | jj d
¡ |D ]J}
|
 d| j ¡rh| j |d |
g¡ nt 
|d¡}| j ||
g¡ q<| jj ¡  d S )Nr   r.   r\   r4   rL   Z	looselist)rN   rM   r‚   r~   ri   r3   )Ú	get_itemsr]   r1   rY   ÚSIBLING_TAGSr:   ra   rb   rc   Útailrh   Úlstripr$   rW   r_   r`   rf   ÚTAGÚLAZY_OLÚ
STARTSWITHÚattribr6   r*   )r,   r0   rF   Úitemsrg   Úlstr\   ZlchrL   Z	firstitemÚitemr%   r%   r&   rG   [  s>    





zOListProcessor.runc                 C   s¾   g }|  d¡D ]ª}| j |¡}|rb|sP| jdkrPt d¡}| | d¡¡ ¡ | _| | d¡¡ q| j	 |¡r¤|d  
d| j ¡r˜d |d |¡|d< q¸| |¡ qd |d |¡|d< q|S )	z  Break a block into list items. r2   rN   z(\d+)r   é   r.   r3   ú{}
{})r5   rƒ   rj   r‰   rP   rQ   rk   r‹   r7   rR   r6   r*   rr   )r,   rD   r   r=   rl   Z
INTEGER_REr%   r%   r&   r…   –  s    
zOListProcessor.get_items)rH   rI   rJ   rK   r‰   r‹   rŠ   r†   r-   rE   rG   r…   ro   r%   r%   rT   r&   r   ?  s   ;r   c                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )r   z  Process unordered list blocks. rM   c                    s&   t ƒ  |¡ t d| jd  ¡| _d S )Nz^[ ]{0,%d}[*+-][ ]+(.*)r   )rO   r-   rP   rQ   r*   rw   r+   rT   r%   r&   r-   ¶  s    zUListProcessor.__init__)rH   rI   rJ   rK   r‰   r-   ro   r%   r%   rT   r&   r   ±  s   r   c                   @   s*   e Zd ZdZe d¡Zdd„ Zdd„ ZdS )r   z Process Hash Headers. z>(?:^|\n)(?P<level>#{1,6})(?P<header>(?:\\.|[^\\])*?)#*(?:\n|$)c                 C   s   t | j |¡ƒS r)   )rv   rw   rx   rC   r%   r%   r&   rE   Â  s    zHashHeaderProcessor.testc                 C   sœ   |  d¡}| j |¡}|rŠ|d | ¡ … }|| ¡ d … }|rN| j ||g¡ t |dt	| 
d¡ƒ ¡}| 
d¡ ¡ |_|r˜| d|¡ nt d| ¡ d S )Nr   úh%dr@   ÚheaderzWe've got a problem header: %r)r]   rw   rx   r~   Úendr$   r`   ra   rh   r/   rk   r8   r:   rc   ÚloggerÚwarn)r,   r0   rF   rD   rl   r   ÚafterÚhr%   r%   r&   rG   Å  s    
zHashHeaderProcessor.runN)	rH   rI   rJ   rK   rP   rQ   rw   rE   rG   r%   r%   r%   r&   r   ¼  s   
r   c                   @   s.   e Zd ZdZe dej¡Zdd„ Zdd„ Z	dS )r   z Process Setext-style Headers. z^.*?\n[=-]+[ ]*(\n|$)c                 C   s   t | j |¡ƒS r)   r„   rC   r%   r%   r&   rE   á  s    zSetextHeaderProcessor.testc                 C   sp   |  d¡ d¡}|d  d¡r$d}nd}t |d| ¡}|d  ¡ |_t|ƒdkrl| dd 	|dd … ¡¡ d S )Nr   r2   r   ú=r   r’   )
r]   r5   r6   ra   rh   r8   r:   r/   rc   r9   )r,   r0   rF   r<   r@   r˜   r%   r%   r&   rG   ä  s    zSetextHeaderProcessor.runN©
rH   rI   rJ   rK   rP   rQ   Ú	MULTILINErw   rE   rG   r%   r%   r%   r&   r   Û  s   r   c                   @   s2   e Zd ZdZdZe eej¡Zdd„ Z	dd„ Z
dS )r   z Process Horizontal Rules. zf^[ ]{0,3}(?=(?P<atomicgroup>(-+[ ]{0,2}){3,}|(_+[ ]{0,2}){3,}|(\*+[ ]{0,2}){3,}))(?P=atomicgroup)[ ]*$c                 C   s   | j  |¡}|r|| _dS dS )NTF)Ú	SEARCH_RErx   rj   )r,   r0   rD   rl   r%   r%   r&   rE   û  s
    zHRProcessor.testc                 C   sp   |  d¡}| j}|d | ¡ …  d¡}|r:| j ||g¡ t |d¡ || ¡ d …  	d¡}|rl| 
d|¡ d S )Nr   r2   r   )r]   rj   r~   rs   r$   r`   ra   rh   r”   rˆ   rc   )r,   r0   rF   rD   rj   ZprelinesZ	postlinesr%   r%   r&   rG     s    
zHRProcessor.runN)rH   rI   rJ   rK   rw   rP   rQ   r›   rœ   rE   rG   r%   r%   r%   r&   r   ò  s
   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z< Process blocks that are empty or start with an empty line. c                 C   s   | p|  d¡S )Nr2   )r6   rC   r%   r%   r&   rE     s    zEmptyBlockProcessor.testc                 C   s†   |  d¡}d}|r2d}|dd … }|r2| d|¡ |  |¡}|d k	r‚|jdkr‚t|ƒr‚|d jdkr‚t d |d j|¡¡|d _d S )Nr   z

r2   r   rp   r	   z{}{})	r]   rc   r1   rY   r/   r   rq   rr   r:   )r,   r0   rF   rD   Zfillerrt   rg   r%   r%   r&   rG     s     

ÿÿÿzEmptyBlockProcessor.runNru   r%   r%   r%   r&   r     s   r   c                   @   s.   e Zd ZdZe dej¡Zdd„ Zdd„ Z	dS )r    z Process link references. z\^[ ]{0,3}\[([^\[\]]*)\]:[ ]*\n?[ ]*([^\s]+)[ ]*(?:\n[ ]*)?((["\'])(.*)\4[ ]*|\((.*)\)[ ]*)?$c                 C   s   dS ©NTr%   rC   r%   r%   r&   rE   5  s    zReferenceProcessor.testc                 C   sà   |  d¡}| j |¡}|rÐ| d¡ ¡  ¡ }| d¡ d¡ d¡}| d¡pT| d¡}||f| jj	j
|< || ¡ d …  ¡ rš| d|| ¡ d …  d¡¡ |d | ¡ …  ¡ rÌ| d|d | ¡ …  d¡¡ d	S | d|¡ d
S )Nr   r   r   ú<r€   é   é   r2   TF)r]   rw   rx   rk   r8   Úlowerrˆ   rs   r$   r"   Z
referencesr”   rc   r~   )r,   r0   rF   rD   rl   ÚidÚlinkÚtitler%   r%   r&   rG   8  s    
zReferenceProcessor.runNrš   r%   r%   r%   r&   r    /  s    ÿr    c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r!   z Process Paragraph blocks. c                 C   s   dS r   r%   rC   r%   r%   r&   rE   O  s    zParagraphProcessor.testc                 C   s”   |  d¡}| ¡ r| jj d¡rz|  |¡}|d k	rV|jrJd |j|¡|_qxd| |_q|jrnd |j|¡|_q| 	¡ |_nt
 |d¡}| 	¡ |_d S )Nr   ri   r‘   z
%sr\   )r]   r8   r$   rW   rX   r1   r‡   rr   r:   rˆ   ra   rh   )r,   r0   rF   rD   rg   r\   r%   r%   r&   rG   R  s    
	
zParagraphProcessor.runNru   r%   r%   r%   r&   r!   L  s   r!   )rK   ÚloggingrP   Úxml.etree.ElementTreera   ÚElementTreer4   r   Zblockparserr   Ú	getLoggerr•   r'   r(   r   r   r   r   r   r   r   r   r   r    r!   r%   r%   r%   r&   Ú<module>   s&    
Z_!*r"