U
    ôDk\r  ã                   @   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
Z
d
d„ ZG dd„ deƒZG dd„ deƒZdd„ Zd	S )a‡  
WikiLinks Extension for Python-Markdown
======================================

Converts [[WikiLinks]] to relative links.

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

Original code Copyright [Waylan Limberg](http://achinghead.com/).

All changes Copyright The Python Markdown Project

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

é    )Úabsolute_import)Úunicode_literalsé   )Ú	Extensioné   )ÚInlineProcessor)ÚetreeNc                 C   s   t  dd| ¡}d|||f S )z1 Build a url from the label, a base, and an end. z([ ]+_)|(_[ ]+)|([ ]+)Ú_z%s%s%s)ÚreÚsub)ÚlabelÚbaseÚendZclean_label© r   ú?/usr/lib/python3/dist-packages/markdown/extensions/wikilinks.pyÚ	build_url   s    r   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚWikiLinkExtensionc                    s6   ddgddgddgt dgdœ| _tt| ƒjf |Ž d S )Nú/z%String to append to beginning or URL.zString to append to end of URL.ÚwikilinkzCSS hook. Leave blank for none.z Callable formats URL from label.)Úbase_urlÚend_urlÚ
html_classr   )r   ÚconfigÚsuperr   Ú__init__)ÚselfÚkwargs©Ú	__class__r   r   r   "   s    üzWikiLinkExtension.__init__c                 C   s2   || _ d}t||  ¡ ƒ}||_ |j |dd¡ d S )Nz\[\[([\w0-9_ -]+)\]\]r   éK   )ÚmdÚWikiLinksInlineProcessorZ
getConfigsZinlinePatternsÚregister)r   r    ZWIKILINK_REZwikilinkPatternr   r   r   ÚextendMarkdown,   s
    z WikiLinkExtension.extendMarkdown)Ú__name__Ú
__module__Ú__qualname__r   r#   Ú__classcell__r   r   r   r   r       s   
r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )r!   c                    s   t t| ƒ |¡ || _d S ©N)r   r!   r   r   )r   Úpatternr   r   r   r   r   7   s    z!WikiLinksInlineProcessor.__init__c           	      C   s„   |  d¡ ¡ rj|  ¡ \}}}|  d¡ ¡ }| jd |||ƒ}t d¡}||_| d|¡ |rn| d|¡ nd}|| d¡| 	d¡fS )Nr   r   ÚaZhrefÚclassÚ r   )
ÚgroupÚstripÚ_getMetar   r   ZElementÚtextÚsetÚstartr   )	r   ÚmÚdatar   r   r   r   Zurlr*   r   r   r   ÚhandleMatch;   s    
z$WikiLinksInlineProcessor.handleMatchc                 C   sˆ   | j d }| j d }| j d }t| jdƒr~d| jjkrF| jjd d }d| jjkrb| jjd d }d| jjkr~| jjd d }|||fS )	z" Return meta data or config data. r   r   r   ÚMetaZwiki_base_urlr   Zwiki_end_urlZwiki_html_class)r   Úhasattrr    r6   )r   r   r   r   r   r   r   r/   I   s    


z!WikiLinksInlineProcessor._getMeta)r$   r%   r&   r   r5   r/   r'   r   r   r   r   r!   6   s   r!   c                  K   s
   t f | ŽS r(   )r   )r   r   r   r   ÚmakeExtensionX   s    r8   )Ú__doc__Z
__future__r   r   r,   r   Zinlinepatternsr   Úutilr   r
   r   r   r!   r8   r   r   r   r   Ú<module>   s   "