U
    þÆ¼\B<  ã                   @   s~   d Z ddlmZ ddlmZ ddlmZ ddlZdd„ ZG d	d
„ d
ejƒ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).

PRE-PROCESSORS
=============================================================================

Preprocessors work on source text before we start doing anything too
complicated.
é    )Úabsolute_import)Úunicode_literalsé   )ÚutilNc                 K   sB   t  ¡ }| t| ƒdd¡ | t| ƒdd¡ | t| ƒdd¡ |S )z: Build the default set of preprocessors used by Markdown. Znormalize_whitespaceé   Z
html_blocké   Z	referenceé
   )r   ZRegistryÚregisterÚNormalizeWhitespaceÚHtmlBlockPreprocessorÚReferencePreprocessor)ÚmdÚkwargsZpreprocessors© r   ú8/usr/lib/python3/dist-packages/markdown/preprocessors.pyÚbuild_preprocessors#   s
    r   c                   @   s   e Zd ZdZdd„ ZdS )ÚPreprocessoraF  
    Preprocessors are run after the text is broken into lines.

    Each preprocessor implements a "run" method that takes a pointer to a
    list of lines of the document, modifies it as necessary and returns
    either the same pointer or a pointer to a new list.

    Preprocessors must extend markdown.Preprocessor.

    c                 C   s   dS )zÕ
        Each subclass of Preprocessor should override the `run` method, which
        takes the document as a list of strings split by newlines and returns
        the (possibly modified) list of lines.

        Nr   )ÚselfÚlinesr   r   r   Úrun7   s    zPreprocessor.runN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   ,   s   
r   c                   @   s   e Zd ZdZdd„ ZdS )r
   z. Normalize whitespace for consistent parsing. c                 C   s`   d  |¡}| tjd¡ tjd¡}| dd¡ dd¡d }| | jj¡}t 	dd|¡}| 
d¡S )NÚ
Ú z
úú

z(?<=\n) +\n)ÚjoinÚreplacer   ZSTXZETXÚ
expandtabsr   Z
tab_lengthÚreÚsubÚsplit)r   r   Úsourcer   r   r   r   D   s    
zNormalizeWhitespace.runNr   r   r   r   r   r
   A   s   r
   c                   @   s„   e Zd ZdZddgZdZde Ze eej	¡Z
e eej	¡Z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 )r   zDRemove html blocks from the text and store them for later retrieval.z</%s>z%s>av  
        \s+(?P<attr>[^>"'/= ]+)=(?P<q>['"])(?P<value>.*?)(?P=q) # attr="value"
        |                                                       # OR
        \s+(?P<attr1>[^>"'/= ]+)=(?P<value1>[^> ]+)             # attr=value
        |                                                       # OR
        \s+(?P<attr2>[^>"'/= ]+)                                # attr
        z+^\<(?P<tag>[^> ]+)(?P<attrs>(%s)*)\s*\/?\>?Fc                 C   s$  | j  |¡}|rò| d¡}| d¡}i }|rÞ| j |¡D ]¤}| d¡r~| d¡rj| d¡|| d¡ ¡ < qÜd|| d¡ ¡ < q8| d¡rÀ| d¡r¬| d¡|| d¡ ¡ < qÜd|| d¡ ¡ < q8| d¡r8d|| d¡ ¡ < q8|t| d	¡ƒ|fS |d
d …  dd
¡d	  ¡ }|t|ƒd i fS d S )NÚtagÚattrsÚattrÚvaluer   Zattr1Zvalue1Zattr2r   r   ú>é   )	Úleft_tag_reÚmatchÚgroupÚattrs_reÚfinditerÚstripÚlenr$   Úlower)r   ÚblockÚmr&   Z	raw_attrsr'   Zmar   r   r   Ú_get_left_tag^   s,    





ÿ
z#HtmlBlockPreprocessor._get_left_tagc                 C   sr   |  ||¡}|dkrdS |  ||¡}||ks4|dkr@|t|ƒ S |  d|¡}|  |||d |¡}|dkr dS q d S )Néÿÿÿÿr*   r   )Úfindr2   Ú_recursive_tagfind)r   ZltagZrtagZstart_indexr4   ÚiÚjr   r   r   r9   y   s    z(HtmlBlockPreprocessor._recursive_tagfindc                 C   sf   | j D ]@}|| }|  d| |||¡}|dkr| d¡ d¡|f  S q| ¡ | d…  ¡ t|ƒfS )Nz<%sr+   ú<r*   r7   )Úright_tag_patternsr9   ÚlstripÚrstripr3   r2   )r   Úleft_tagÚ
left_indexr4   Úpr&   r:   r   r   r   Ú_get_right_tag‹   s    
   ÿz$HtmlBlockPreprocessor._get_right_tagc                 C   s\   |d dkrdS d| |kr dS |dkr4|dkr4dS ||dd … krT|d dkrTdS dS d S )Nr   )ú?ú@ú%Tú/ú--r   Fr   )r   r@   Ú	right_tagr   r   r   Ú_equal_tags•   s    z!HtmlBlockPreprocessor._equal_tagsc                 C   s   |dkS )N)Zhrzhr/r   )r   r&   r   r   r   Ú_is_oneliner¡   s    z"HtmlBlockPreprocessor._is_onelinerc                 C   s<   |  d¡ d\}}||kr4|t|| ƒ7 }|d7 }q|d S )zÛ
        Same effect as concatenating the strings in items,
        finding the character to which stringindex refers in that string,
        and returning the index of the item in which that character resides.
        Zdummy)r   r   r   )Úappendr2   )r   ZstringindexÚitemsr:   Úcountr   r   r   Ú_stringindex_to_listindex¤   s    

z/HtmlBlockPreprocessor._stringindex_to_listindexc              
   C   sX  t |ƒD ]H\}}| j |¡r|  d ||d… ¡¡\}}}|  ||d ||d… ¡¡\}}|  |||d… ¡| }	d| ¡ krô|| |d… ||< | jj	 
|||d |	d ¡}
| ||
¡ t|ƒ|	 dkrÔ|	d8 }	||	 dt|ƒ d … ||	< qt|ƒ|	 dkr|	d8 }	|	|kr |d }	| jj	 d |||	… ¡¡}
|||	…= | ||
¡ q|S )z@Find and process html child elements of the given element block.r   NÚmarkdownr   r+   r   )Ú	enumerater,   r-   r6   r   rC   rO   Úkeysr   Ú	htmlStashÚ	store_tagÚinsertr2   Ústore)r   rM   r:   Úitemr@   rA   r'   rI   Ú
data_indexZright_listindexZplaceholderr   r   r   Ú_nested_markdown_in_html±   sF    ÿ  ÿÿ   ÿÿ

ÿ
z.HtmlBlockPreprocessor._nested_markdown_in_htmlc                 C   s   d  |¡}g }| d¡}g }d}d}d}|rà|d }| d¡rL|dd … }|dd … }| d¡rn|dd … }|sN| d¡rBt| ¡ ƒdkrB|dd… d	kr´d
di   }}	}
n|  |¡\}}	}
|  ||	|¡\}}|t|ƒk r| j |¡sú|d
kr| 	d||d … ¡ |d |… }| j |¡sB|d dksB| 
|¡ q(|  |¡r^| 
| ¡ ¡ q(| ¡  d¡rì|  ||¡rì| jrÐd|
 ¡ krÐ||	t|ƒ d … }| 
| jj ||
dd¡¡ | |g¡ q(| 
| jj | ¡ ¡¡ q(nT|  ||¡s&| j |¡s|d
kr&| 
| ¡ ¡ d}q(| 
| jj | ¡ ¡¡ q(n
| 
|¡ q(| 
|¡ |  ||	d  |¡¡\}}d}|d d… D ]}|t|ƒ7 }q€||8 }|  ||¡r(|t|ƒk rÚ|d |… |d< | 	d||d … ¡ d}| jrÀd|
 ¡ krÀ|d |	d … |d< |d d t|ƒ d … |d< |t|ƒd  rFt|ƒd }nt|ƒd }| 
| jj ||
d|¡¡ t| jjjƒ}| |  |¡¡ t| jjjƒ| }| jjjd|  d  |d 7  < n| 
| jj d  |¡¡¡ g }q(|rì| jrÈd|
 ¡ krÈ|d |	d … |d< |d d t|ƒ d … |d< |t|ƒd  rNt|ƒd }nt|ƒd }| 
| jj ||
d|¡¡ t| jjjƒ}| |  |¡¡ t| jjjƒ| }| jjjd|  d  |d 7  < n| 
| jj d  |¡¡¡ | 
d¡ d  |¡}| d¡S )Nr   r   r   Fr   r   r<   é   z!--rH   r+   )ú!rD   rE   rF   r*   rP   Tr7   é   Úright_index)r   ÚrsplitÚ
startswithr2   r1   r6   rC   r   Zis_block_levelrU   rL   rK   r?   ÚendswithrJ   Úmarkdown_in_rawrR   rS   rT   ÚextendrV   Ztag_datarY   r$   )r   r   ÚtextZ
new_blocksrM   r@   rI   Zin_tagr4   rA   r'   rX   Zprev_block_lengthrW   r]   ZplaceholderslenZnestsÚnew_textr   r   r   r   Ï   s    



þ$

ÿ   ÿÿ
ÿÿÿ
   ÿÿÿÿÿÿ   ÿÿÿÿÿ

zHtmlBlockPreprocessor.runN)r   r   r   r   r=   Zattrs_patternZleft_tag_patternr"   ÚcompileÚVERBOSEr/   r,   ra   r6   r9   rC   rJ   rK   rO   rY   r   r   r   r   r   r   M   s"   ÿ
r   c                   @   s<   e Zd ZdZdZe de ej¡Ze de ¡Z	dd„ Z
dS )r   zA Remove reference definitions from text and store for later use. z$[ ]*(\"(.*)\"|\'(.*)\'|\((.*)\))[ ]*z*^[ ]{0,3}\[([^\]]*)\]:\s*([^ ]*)[ ]*(%s)?$z^%s$c           	      C   sÔ   g }|rÐ|  d¡}| j |¡}|rÄ| d¡ ¡  ¡ }| d¡ d¡ d¡}| d¡pf| d¡pf| d¡}|s¨| j |d ¡}|r¨|  d¡ | d¡p¦| d	¡p¦| d
¡}||f| j	j
|< | d¡ q| |¡ q|S )Nr   r   r+   r<   r*   é   é   é   r\   rZ   r   )ÚpopÚREr-   r.   r1   r3   r>   r?   ÚTITLE_REr   Z
referencesrL   )	r   r   rd   Úliner5   ÚidÚlinkÚtZtmr   r   r   r   _  s"    

zReferencePreprocessor.runN)r   r   r   r   ZTITLEr"   re   ÚDOTALLrk   rl   r   r   r   r   r   r   V  s    ÿr   )r   Z
__future__r   r   r   r   r"   r   Z	Processorr   r
   r   r   r   r   r   r   Ú<module>   s   	  