U
    ôDk\w<  ã                   @   s*  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 ddlmZ ddlmZ ddlZddlZejd ej Zejd ej Ze d¡Ze d¡Ze 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 dd „ Z!dS )!a:  
Footnotes Extension for Python-Markdown
=======================================

Adds footnote handling to Python-Markdown.

See <https://Python-Markdown.github.io/extensions/footnotes>
for documentation.

Copyright The Python Markdown Project

License: [BSD](http://www.opensource.org/licenses/bsd-license.php)

é    )Úabsolute_import)Úunicode_literalsé   )Ú	Extensioné   )ÚPreprocessor)ÚInlineProcessor)ÚTreeprocessor)ÚPostprocessor)Úutil)ÚOrderedDictNZzz1337820767766393qqZqq3936677670287331zzz[ ]{0,3}\[\^([^\]]*)\]:\s*(.*)z((\t)|(    ))(.*)z(fnref)(\d+)c                       sl   e Zd ZdZ‡ f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d„Zdd„ Z‡  ZS )ÚFootnoteExtensionz Footnote Extension. c                    sX   ddgddgddgddgd	d
gdœ| _ tt| ƒjf |Ž d| _i | _tƒ | _|  ¡  dS )z Setup configs. z///Footnotes Go Here///z1The text string that marks where the footnotes goFz7Avoid name collisions across multiple calls to reset().z&#8617;zCThe text string that links from the footnote to the reader's place.z$Jump back to footnote %d in the textznThe text string used for the title HTML attribute of the backlink. %d will be replaced by the footnote number.ú:zFootnote separator.)ÚPLACE_MARKERÚ
UNIQUE_IDSÚBACKLINK_TEXTÚBACKLINK_TITLEÚ	SEPARATORr   N)	ZconfigÚsuperr   Ú__init__Úunique_prefixÚ
found_refsÚsetÚ	used_refsÚreset)ÚselfÚkwargs©Ú	__class__© ú?/usr/lib/python3/dist-packages/markdown/extensions/footnotes.pyr   &   s*    ÿÿÿÿÿîzFootnoteExtension.__init__c                 C   s†   |  | ¡ |j| _|| _|j t| ƒdd¡ d}|j t|| ƒdd¡ |j t	| ƒdd¡ |j t
| ƒdd¡ |j t| ƒdd¡ dS )	z Add pieces to Markdown. Úfootnoteé   z\[\^([^\]]*)\]é¯   é2   zfootnote-duplicateé   N)ZregisterExtensionÚparserÚmdÚpreprocessorsÚregisterÚFootnotePreprocessorZinlinePatternsÚFootnoteInlineProcessorÚtreeprocessorsÚFootnoteTreeprocessorÚFootnotePostTreeprocessorÚpostprocessorsÚFootnotePostprocessor)r   r'   ZFOOTNOTE_REr   r   r    ÚextendMarkdownG   s    
z FootnoteExtension.extendMarkdownc                 C   s(   t ƒ | _|  jd7  _i | _tƒ | _dS )z> Clear footnotes on reset, and prepare for distinct document. r   N)r   Ú	footnotesr   r   r   r   ©r   r   r   r    r   `   s    zFootnoteExtension.resetFc                 C   s°   |s|S |}|| j krx| |  ¡ d¡\}}t |¡}|rbd| d¡t| d¡ƒd |  ¡ |f }qd|d|  ¡ |f }q| j  |¡ || jkr¢| j|  d7  < n
d| j|< |S )z1 Get a unique reference if there are duplicates. r   ú%s%d%s%sr   )	r   ÚsplitÚget_separatorÚ	RE_REF_IDÚmatchÚgroupÚintÚaddr   )r   Z	referenceÚfoundZoriginal_refÚrefÚrestÚmr   r   r    Ú
unique_refg   s    

*

zFootnoteExtension.unique_refc                    s   ‡ ‡fdd„‰ ˆ |ƒ}|S )z@ Return ElementTree Element that contains Footnote placeholder. c                    sz   | D ]p}|j r2|j  ˆ d¡¡dkr2|| df  S |jr\|j ˆ d¡¡dkr\|| df  S ˆ |ƒ}|d k	r|  S qd S )Nr   éÿÿÿÿTF)ÚtextÚfindÚ	getConfigÚtail)ÚelementÚchildZ	child_res©Úfinderr   r   r    rI   ~   s    
z:FootnoteExtension.findFootnotesPlaceholder.<locals>.finderr   )r   ÚrootÚresr   rH   r    ÚfindFootnotesPlaceholder|   s    z*FootnoteExtension.findFootnotesPlaceholderc                 C   s   || j |< dS )z' Store a footnote for later retrieval. N©r2   )r   ÚidrB   r   r   r    ÚsetFootnoteŽ   s    zFootnoteExtension.setFootnotec                 C   s
   |   d¡S )z Get the footnote separator. r   )rD   r3   r   r   r    r6   ’   s    zFootnoteExtension.get_separatorc                 C   s2   |   d¡rd|  ¡ | j|f S d|  ¡ |f S dS )z Return footnote link id. r   z	fn%s%d-%szfn%s%sN)rD   r6   r   )r   rN   r   r   r    ÚmakeFootnoteId–   s    
z FootnoteExtension.makeFootnoteIdc                 C   sB   |   d¡r&|  d|  ¡ | j|f |¡S |  d|  ¡ |f |¡S dS )z Return footnote back-link id. r   zfnref%s%d-%sz	fnref%s%sN)rD   r@   r6   r   )r   rN   r<   r   r   r    ÚmakeFootnoteRefId   s    
z#FootnoteExtension.makeFootnoteRefIdc                 C   s^  t | j ¡ ƒsdS tj d¡}| dd¡ tj |d¡ tj |d¡}tj d¡}t| j ¡ ddD ]ô\}}tj |d	¡}| d
|  	|¡¡ | j
 || j| ¡ t |ƒD ]}| |¡ | |¡ q¨tj d¡}	|	 dd|  |¡ ¡ |	 dd¡ |	 d|  d¡| ¡ t|	_t|ƒrd|d }
|
jdkr@|
jt |
_|
 |	¡ qdtj |d¡}| |	¡ qd|S )z( Return div of footnotes as et Element. NÚdivÚclassr!   ZhrÚolr   )ÚstartÚlirN   ÚaÚhrefú#úfootnote-backrefÚtitler   rA   Úp)Úlistr2   Úkeysr   ÚetreeÚElementr   Ú
SubElementÚ	enumeraterP   r&   Z
parseChunkÚappendÚremoverQ   rD   ÚFN_BACKLINK_TEXTrB   ÚlenÚtagÚNBSP_PLACEHOLDER)r   rJ   rR   rT   Zsurrogate_parentÚindexrN   rV   ÚelZbacklinkZnoder\   r   r   r    ÚmakeFootnotesDiv¤   s<    
þz"FootnoteExtension.makeFootnotesDiv)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r1   r   r@   rL   rO   r6   rP   rQ   rk   Ú__classcell__r   r   r   r    r   #   s   !

r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r*   z7 Find all footnote references and store for later use. c                 C   s
   || _ d S ©NrM   ©r   r2   r   r   r    r   Ñ   s    zFootnotePreprocessor.__init__c           	      C   sÈ   g }d}t  || ¡}|r˜|  ||d d… ¡\}}| d| d¡¡ ||d 7 }d |¡}| j | d¡| ¡ ¡ t	| 
d¡ƒd }| dg| ¡ n| || ¡ t	|ƒ|d krÄ|d7 }qqÄq|S )zÞ
        Loop through lines and find, set, and remove footnote definitions.

        Keywords:

        * lines: A list of lines of text

        Return: A list of lines of text with footnote definitions removed.

        r   r   Nr   Ú
z

Ú )ÚDEF_REr8   ÚdetectTabbedÚinsertr9   Újoinr2   rO   Úrstriprf   r5   Úextendrc   )	r   ÚlinesÚnewlinesÚir?   ÚfnZ_ir!   Z
num_blocksr   r   r    ÚrunÔ   s     

zFootnotePreprocessor.runc           
      C   sø   g }d}d}dd„ }|D ]Î}|  ¡ r|||ƒ}|rF| |¡ |d7 }qqæ|sjt |¡sj| |¡ |d7 }qqæ||d f  S qd}|d7 }t|t|ƒƒD ]0}||   ¡ r´|| }	 qÌq–| d¡ |d7 }q– qð||	ƒrâ| d¡ qq qðq|d7 }||fS )zÙ Find indented text and remove indent before further proccesing.

        Keyword arguments:

        * lines: an array of strings

        Returns: a list of post processed items and the index of last line.

        Fr   c                 S   s   t  | ¡}|r| d¡S d S )Né   )Ú	TABBED_REr8   r9   )Úliner8   r   r   r    Údetab  s    
z0FootnotePreprocessor.detectTabbed.<locals>.detabr   Trt   )Ústriprc   ru   r8   Úrangerf   )
r   r{   ÚitemsZ
blank_liner}   rƒ   r‚   Zdetabbed_lineÚjZ	next_liner   r   r    rv   õ   s>    





z!FootnotePreprocessor.detectTabbedN)rl   rm   rn   ro   r   r   rv   r   r   r   r    r*   Î   s   !r*   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r+   z? InlinePattern for footnote markers in a document's body text. c                    s   t t| ƒ |¡ || _d S rq   )r   r+   r   r2   )r   Úpatternr2   r   r   r    r   7  s    z FootnoteInlineProcessor.__init__c                 C   s°   |  d¡}|| jj ¡ kr¨tj d¡}tj |d¡}| d| jj|dd¡ | dd| j 	|¡ ¡ | d	d
¡ t 
t| jj ¡ ƒ |¡d ¡|_|| d¡| d¡fS dS d S )Nr   ÚsuprW   rN   T)r<   rX   rY   rS   zfootnote-refr   )NNN)r9   r2   r^   r   r_   r`   ra   r   rQ   rP   Z	text_typer]   ri   rB   rU   Úend)r   r?   ÚdatarN   r‰   rW   r   r   r    ÚhandleMatch;  s    
"z#FootnoteInlineProcessor.handleMatch)rl   rm   rn   ro   r   rŒ   rp   r   r   r   r    r+   4  s   r+   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )r.   z% Amend footnote div with duplicates. c                 C   s
   || _ d S rq   rM   rr   r   r   r    r   L  s    z"FootnotePostTreeprocessor.__init__c                 C   s¼   |  d¡D ]¬}|j dd¡dkr
|jd  | j ¡ d¡\}}g }td|d ƒD ]B}t |¡}d||| j ¡ |f |jd< | 	|¡ |  j
d7  _
qNt|ƒd	 }	|D ]}
|	 	|
¡ q¢ q¸q
d
S )z@ Adjust current li and add the duplicates: fnref2, fnref3, etc. rW   rS   rt   rZ   rX   r   r   r4   rA   N)ÚiterÚattribÚgetr5   r2   r6   r…   ÚcopyÚdeepcopyrc   Úoffsetr]   )r   rV   Ú
duplicatesÚlinkr=   r>   Zlinksri   Zsib_linkrj   Úlr   r   r    Úadd_duplicatesO  s    

z(FootnotePostTreeprocessor.add_duplicatesc                 C   sD   |j  dd¡ | j ¡ d¡\}}d|| j ¡ |f }| jj |d¡S )z3 Get the number of duplicate refs of the footnote. rN   rt   r   z	%sref%s%sr   )rŽ   r   r5   r2   r6   r   )r   rV   r~   r>   Zlink_idr   r   r    Úget_num_duplicatesc  s     z,FootnotePostTreeprocessor.get_num_duplicatesc                 C   s0   t |ƒD ]"}|  |¡}|dkr|  ||¡ qdS )z= Find duplicate footnotes and format and add the duplicates. r   N)r]   r—   r–   )r   ÚparentrV   Úcountr   r   r    Úhandle_duplicatesi  s    
z+FootnotePostTreeprocessor.handle_duplicatesc                 C   sJ   d| _ | d¡D ]4}|j dd¡dkr| d¡D ]}|  |¡  qq0qdS )z= Crawl the footnote div and add missing duplicate footnotes. r   rR   rS   rt   r!   rT   N)r’   r   rŽ   r   rš   )r   rJ   rR   rT   r   r   r    r   r  s    
zFootnotePostTreeprocessor.runN)	rl   rm   rn   ro   r   r–   r—   rš   r   r   r   r   r    r.   I  s   	r.   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r-   z3 Build and append footnote div to end of document. c                 C   s
   || _ d S rq   rM   rr   r   r   r    r     s    zFootnoteTreeprocessor.__init__c                 C   s~   | j  |¡}|d k	rz| j  |¡}|rp|\}}}t|ƒ |¡}|rX| |¡ | ||¡ qz| |d |¡ d |_n
| |¡ d S )Nr   )	r2   rk   rL   r]   ri   rd   rw   rE   rc   )r   rJ   ZfootnotesDivÚresultrG   r˜   ZisTextZindr   r   r    r   „  s    

zFootnoteTreeprocessor.runN©rl   rm   rn   ro   r   r   r   r   r   r    r-   ~  s   r-   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r0   z* Replace placeholders with html entities. c                 C   s
   || _ d S rq   rM   rr   r   r   r    r   —  s    zFootnotePostprocessor.__init__c                 C   s    |  t| j d¡¡}|  td¡S )Nr   z&#160;)Úreplacere   r2   rD   rh   )r   rB   r   r   r    r   š  s
     
ÿzFootnotePostprocessor.runNrœ   r   r   r   r    r0   •  s   r0   c                  K   s
   t f | ŽS )z- Return an instance of the FootnoteExtension )r   )r   r   r   r    ÚmakeExtension¡  s    rž   )"ro   Z
__future__r   r   rt   r   r(   r   Zinlinepatternsr   r,   r	   r/   r
   r   Úcollectionsr   Úrer   ZSTXZETXre   rh   Úcompileru   r   r7   r   r*   r+   r.   r-   r0   rž   r   r   r   r    Ú<module>   s0   


 ,f5