o
    Ì^½^Ù  ã                   @   sÆ   d dl Z d dlZd dlmZmZmZ d dlmZ d dl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mZmZmZ e e¡Ze e¡ G d	d
„ d
ƒZG dd„ deƒZG dd„ de
ƒZdS )é    N)ÚurlparseÚ
urlunparseÚurljoin)Úunquote)Ú	Extension)ÚTreeprocessor)ÚAMP_SUBSTITUTE)Úget_toc)ÚmetaÚget_build_dateÚget_markdown_titleÚwarning_filterc                   @   s´   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd%d
d„Zdd„ Zdd„ Z	e
ee	ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$S )&ÚPagec                 C   s”   | |_ || _|| _d | _d | _d | _d | _d| _d| _d| _	d| _
tƒ | _|  | dd ¡¡ |  | dd ¡| dd ¡¡ d | _d | _g | _i | _d S )NFTÚsite_urlÚrepo_urlÚedit_uri)ÚpageÚfileÚtitleÚparentÚchildrenÚprevious_pageÚ	next_pageÚactiveÚ
is_sectionÚis_pageÚis_linkr   Úupdate_dateÚ_set_canonical_urlÚgetÚ_set_edit_urlÚmarkdownÚcontentÚtocr
   )Úselfr   r   Úconfig© r&   ú8/usr/lib/python3/dist-packages/mkdocs/structure/pages.pyÚ__init__   s$   
zPage.__init__c                 C   s(   dd„ }t || jƒo|| jƒ||jƒkS )Nc                 S   s   dd„ |   ¡ D ƒS )Nc                 S   s   i | ]\}}|d v r||“qS ))r   r   r&   )Ú.0ÚkeyÚvaluer&   r&   r'   Ú
<dictcomp>1   s    z1Page.__eq__.<locals>.sub_dict.<locals>.<dictcomp>)Úitems)Údr&   r&   r'   Úsub_dict0   s   zPage.__eq__.<locals>.sub_dict)Ú
isinstanceÚ	__class__Ú__dict__)r$   Úotherr/   r&   r&   r'   Ú__eq__.   s    zPage.__eq__c                 C   s   |   |¡ S ©N)r4   )r$   r3   r&   r&   r'   Ú__ne__5   s   zPage.__ne__c                 C   s0   | j d urd | j ¡nd}d || jp| jj¡S )Nz'{}'z[blank]zPage(title={}, url='{}'))r   ÚformatÚabs_urlr   Úurl©r$   r   r&   r&   r'   Ú__repr__8   s   zPage.__repr__r   c                 C   s   d  d| t| ƒ¡S )Nz{}{}z    )r7   Úrepr)r$   Údepthr&   r&   r'   Ú_indent_print<   s   zPage._indent_printc                 C   s   | j S )z Return active status of page. )Ú_Page__active©r$   r&   r&   r'   Ú_get_active?   s   zPage._get_activec                 C   s(   t |ƒ| _| jdurt |ƒ| j_dS dS )z* Set active status of page and ancestors. N)Úboolr?   r   r   )r$   r+   r&   r&   r'   Ú_set_activeC   s   

ÿzPage._set_activec                 C   s   | j jdkS )NÚindex)r   Únamer@   r&   r&   r'   Úis_indexK   s   zPage.is_indexc                 C   s
   | j d u S r5   )r   r@   r&   r&   r'   Úis_top_levelO   s   
zPage.is_top_levelc                 C   s   | j o| jo| jjdkS )NÚ.)rG   rF   r   r9   r@   r&   r&   r'   Úis_homepageS   ó   zPage.is_homepagec                 C   s   | j jdkrdS | j jS )NrH   Ú )r   r9   r@   r&   r&   r'   r9   W   rJ   zPage.urlc                 C   s   | j d u rg S | j g| j j S r5   )r   Ú	ancestorsr@   r&   r&   r'   rL   [   s   
zPage.ancestorsc                 C   sF   |r|  d¡s|d7 }t|| jƒ| _t| jƒj| _d S d | _d | _d S )Nú/)Úendswithr   r9   Úcanonical_urlr   Úpathr8   )r$   Úbaser&   r&   r'   r   a   s   

zPage._set_canonical_urlc                 C   s6   |r|r| j j dd¡}t||| ƒ| _d S d | _d S )Nú\rM   )r   Úsrc_pathÚreplacer   Úedit_url)r$   r   r   rS   r&   r&   r'   r    k   s   
zPage._set_edit_urlc                 C   sÂ   |d j d| |d�}|d u rRz t| jjdddd��}| ¡ }W d   ƒ n1 s(w   Y  W n# ty@   t d | jj	¡¡ ‚  t
yQ   t d	 | jj	¡¡ ‚ w t |¡\| _| _|  ¡  d S )
NÚpluginsÚpage_read_source)r   r%   Úrz	utf-8-sigÚstrict)ÚencodingÚerrorszFile not found: {}zEncoding error reading file: {})Ú	run_eventÚopenr   Úabs_src_pathÚreadÚOSErrorÚlogÚerrorr7   rS   Ú
ValueErrorr
   Úget_datar!   Ú
_set_title)r$   r%   ÚsourceÚfr&   r&   r'   Úread_sourcer   s$   ÿ
ÿ€þzPage.read_sourcec                 C   s|   | j durdS d| jv r| jd | _ dS t| jƒ}|du r9| jr#d}n| jj dd¡ dd¡}| ¡ |kr9| 	¡ }|| _ dS )a;  
        Set the title for a Markdown document.

        Check these in order and use the first that returns a valid title:
        - value provided on init (passed in from config)
        - value of metadata 'title'
        - content of the first H1 in Markdown content
        - convert filename to title
        Nr   ÚHomeú-ú Ú_)
r   r
   r   r!   rI   r   rE   rT   ÚlowerÚ
capitalizer:   r&   r&   r'   re   „   s   




zPage._set_titlec                 C   sP   t | j|ƒg|d  }tj||d pi d�}| | j¡| _tt|dg ƒƒ| _dS )zM
        Convert the Markdown source file to HTML as per the config.
        Úmarkdown_extensionsÚmdx_configs)Ú
extensionsÚextension_configsÚ
toc_tokensN)	Ú_RelativePathExtensionr   r!   ÚMarkdownÚconvertr"   r	   Úgetattrr#   )r$   r%   Úfilesrq   Úmdr&   r&   r'   Úrender¢   s   
ÿþ
þzPage.renderN)r   )Ú__name__Ú
__module__Ú__qualname__r(   r4   r6   r;   r>   rA   rC   Úpropertyr   rF   rG   rI   r9   rL   r   r    rh   re   rz   r&   r&   r&   r'   r      s0    







r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Ú_RelativePathTreeprocessorc                 C   ó   || _ || _d S r5   ©r   rx   ©r$   r   rx   r&   r&   r'   r(   ´   ó   
z#_RelativePathTreeprocessor.__init__c                 C   sT   |  ¡ D ]#}|jdkrd}n	|jdkrd}nq| |¡}|  |¡}| ||¡ q|S )zÖ
        Update urls on anchors and images to make them relative

        Iterates through the full document tree looking for specific
        tags and then makes them relative based on the site navigation
        ÚaÚhrefÚimgÚsrc)ÚiterÚtagr   Úpath_to_urlÚset)r$   ÚrootÚelementr*   r9   Únew_urlr&   r&   r'   Úrun¸   s   



z_RelativePathTreeprocessor.runc                 C   sÚ   t |ƒ\}}}}}}|s(|s(|r(| d¡s(| d¡s(t|v s(dtj |¡d vr*|S tj tj | jj	¡t
|ƒ¡}tj |¡ tj¡}|| jvrUt d | jj	|¡¡ |S | j |¡}	|	 | j¡}||||||f}
t|
ƒS )NrM   rR   rH   éÿÿÿÿz^Documentation file '{}' contains a link to '{}' which is not found in the documentation files.)r   Ú
startswithr   ÚosrP   ÚsplitÚjoinÚdirnamer   rS   Ú
urlunquoteÚnormpathÚlstripÚseprx   ra   Úwarningr7   Úget_file_from_pathÚurl_relative_tor   )r$   r9   ÚschemeÚnetlocrP   ÚparamsÚqueryÚfragmentÚtarget_pathÚtarget_fileÚ
componentsr&   r&   r'   rŠ   Í   s     
þz&_RelativePathTreeprocessor.path_to_urlN)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S )rt   zc
    The Extension class is what we pass to markdown, it then
    registers the Treeprocessor.
    c                 C   r€   r5   r�   r‚   r&   r&   r'   r(   î   rƒ   z_RelativePathExtension.__init__c                 C   s"   t | j| jƒ}|j |dd¡ d S )NÚrelpathr   )r   r   rx   ÚtreeprocessorsÚregister)r$   ry   r¥   r&   r&   r'   ÚextendMarkdownò   s   z%_RelativePathExtension.extendMarkdownN)r{   r|   r}   Ú__doc__r(   r¨   r&   r&   r&   r'   rt   è   s    rt   )r’   ÚloggingÚurllib.parser   r   r   r   r–   r!   Úmarkdown.extensionsr   Úmarkdown.treeprocessorsr   Úmarkdown.utilr   Úmkdocs.structure.tocr	   Úmkdocs.utilsr
   r   r   r   Ú	getLoggerr{   ra   Ú	addFilterr   r   rt   r&   r&   r&   r'   Ú<module>   s     

 "5