o
    Ì^½^à(  ã                   @   sà   d dl Z d dlZd dlZd dl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ZG dd„ dƒZe  e¡Ze eƒ ¡ e e	j¡ dd
d„Zdd„ Zdd„ Zdd„ Zddd„Zddd„Zddd„Zdd„ ZdS )é    N)Úurlparse)ÚTemplateNotFound)Úutils)Ú	get_files)Úget_navigationc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚDuplicateFilterz# Avoid logging duplicate messages. c                 C   s   t ƒ | _d S ©N)ÚsetÚmsgs)Úself© r   ú7/usr/lib/python3/dist-packages/mkdocs/commands/build.pyÚ__init__   s   zDuplicateFilter.__init__c                 C   s   |j | jv}| j |j ¡ |S r   )Úmsgr
   Úadd)r   ÚrecordÚrvr   r   r   Úfilter   s   zDuplicateFilter.filterN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s    r   Ú c              
   C   s\   |durt  d|j¡}t  |d ||¡}t  |d ||¡}| | ¡ |||tjt  ¡ ||dœ	S )zC
    Return the template context for a given page or template.
    NÚ.Úextra_javascriptÚ	extra_css)	ÚnavÚpagesÚbase_urlr   r   Úmkdocs_versionÚbuild_date_utcÚconfigÚpage)r   Úget_relative_urlÚurlÚcreate_media_urlsÚdocumentation_pagesÚmkdocsÚ__version__Úget_build_datetime)r   Úfilesr!   r"   r   r   r   r   r   r   Úget_context   s   ór+   c                 C   sŠ   |d j d|| |d�}t | ¡rt|d pdƒj}nt d| ¡}t||||d�}|d j d|| |d�}| |¡}|d j d	|| |d�}|S )
z@
    Return rendered output for given template as a string.
    ÚpluginsÚpre_template)Útemplate_namer!   Úsite_urlú/r   )r   Útemplate_contextÚpost_template)Ú	run_eventr   Úis_error_templater   Úpathr#   r+   Úrender)ÚnameÚtemplater*   r!   r   r   ÚcontextÚoutputr   r   r   Ú_build_template<   s   ÿ
ÿ
ÿr;   c              	   C   s@  t  d | ¡¡ z| | ¡}W n ty!   t  d | ¡¡ Y dS w t| ||||ƒ}| ¡ r–tj	 
|d | ¡}t | d¡|¡ | dkr”t  d| ¡ d |¡}t|d	ƒ�6}	t ¡ }
tj|	|d	|
d
��}| | d¡¡ W d  ƒ n1 suw   Y  W d  ƒ dS W d  ƒ dS 1 s�w   Y  dS dS t  d | ¡¡ dS )z/ Build a template using the theme environment. zBuilding theme template: {}z6Template skipped: '{}' not found in theme directories.NÚsite_dirúutf-8zsitemap.xmlzGzipping template: %sz{}.gzÚwb)ÚfileobjÚfilenameÚmodeÚmtimeú.Template skipped: '{}' generated empty output.)ÚlogÚdebugÚformatÚget_templater   Úwarningr;   ÚstripÚosr5   Újoinr   Ú
write_fileÚencodeÚopenÚget_build_timestampÚgzipÚGzipFileÚwriteÚinfo)r.   Úenvr*   r!   r   r8   r:   Úoutput_pathÚgz_filenameÚfÚ	timestampÚgz_bufr   r   r   Ú_build_theme_templatea   s0   þ
ÿþ"þýrZ   c           	   
   C   sø   t  d | ¡¡ | | ¡}|du rt  d | ¡¡ dS z"t|jdddd��}t | 	¡ ¡}W d  ƒ n1 s7w   Y  W n t
yY } zt  d | |¡¡ W Y d}~dS d}~ww t| ||||ƒ}| ¡ rrt | d¡|j¡ dS t  d	 | ¡¡ dS )
z7 Build user templates which are not part of the theme. zBuilding extra template: {}Nz-Template skipped: '{}' not found in docs_dir.Úrr=   Ústrict)ÚencodingÚerrorszError reading template '{}': {}rC   )rD   rE   rF   Úget_file_from_pathrH   rN   Úabs_src_pathÚjinja2ÚTemplateÚreadÚ	Exceptionr;   rI   r   rL   rM   Úabs_dest_pathrS   )	r.   r*   r!   r   ÚfilerW   r8   Úer:   r   r   r   Ú_build_extra_template}   s&   
ÿ€€þrh   Fc              
   C   s°   z?|r| j  ¡ sW dS |d jd| ||d�} |  |¡ |d jd| j| ||d�| _|  ||¡ |d jd| j| ||d�| _W dS  tyW } zt 	d 
| j j|¡¡ ‚ d}~ww )	z6 Read page content from docs_dir and render Markdown. Nr,   Úpre_page©r!   r*   Úpage_markdown)r"   r!   r*   Úpage_contentzError reading page '{}': {})rf   Úis_modifiedr3   Úread_sourceÚmarkdownr6   Úcontentrd   rD   ÚerrorrF   Úsrc_path)r"   r!   r*   Údirtyrg   r   r   r   Ú_populate_page–   s&   ÿ
ÿÿ€þrt   c           
   
   C   s  zo|r| j  ¡ sW dS t d | j j¡¡ d| _t|||| ƒ}d| jv r-| 	| jd ¡}n| 	d¡}|d j
d|| ||d�}| |¡}|d j
d	|| |d
�}| ¡ r`t |jddd�| j j¡ n
t d | j j¡¡ d| _W dS  ty‡ }	 zt d | j j|	¡¡ ‚ d}	~	ww )z= Pass a Page to theme template and write output to site_dir. NzBuilding page {}Tr8   z	main.htmlr,   Úpage_context)r"   r!   r   Ú	post_page)r"   r!   r=   Úxmlcharrefreplace)r^   z+Page skipped: '{}'. Generated empty output.FzError building page '{}': {})rf   rm   rD   rE   rF   rr   Úactiver+   ÚmetarG   r3   r6   rI   r   rL   rM   re   rS   rd   rq   )
r"   r!   r*   r   rT   rs   r9   r8   r:   rg   r   r   r   Ú_build_page¶   s2   


ÿ
ÿ€þrz   c           
      C   sô  ddl m } |ƒ }| d  d| ¡} | d jd| d� |s)t d¡ t | d ¡ nt d	¡ |sEt d
| d ¡ |rEt| d ƒrEt d¡ t| ƒ}| d  	¡ }| 
|| ¡ | d jd|| d�}t|| ƒ}| d jd|| |d�}t d¡ | ¡ D ]}t d|j ¡ t|j| ||ƒ qx| d jd|| |d�}t d¡ |j|d� | d jD ]
}	t|	||| |ƒ q¦| d D ]	}	t|	|| |ƒ qµt d¡ | ¡ D ]}t|j| ||||ƒ qÈ| d jd| d� | d rïtjjrïtd tjj¡ƒ‚t d|ƒ | ¡ dS )z Perform a full site build. r   )Útimer,   r!   Ú	pre_build)r!   zCleaning site directoryr<   z°A 'dirty' build is being performed, this will likely lead to inaccurate navigation and other links within your site. This option is designed for site development purposes only.z'Building documentation to directory: %sz?The directory contains stale files. Use --clean to remove them.Úthemer*   r   rj   zReading markdown pages.z	Reading: rT   zCopying static assets.)rs   Úextra_templateszBuilding markdown pages.Ú
post_buildr\   z(
Exited with {} warnings in strict mode.z#Documentation built in %.2f secondsN)r{   r3   rD   rS   r   Úclean_directoryrH   Ú#site_directory_contains_stale_filesr   Úget_envÚadd_files_from_themer   rE   r&   rr   rt   r"   Úcopy_static_filesÚstatic_templatesrZ   rh   rz   Úwarning_filterÚcountÚ
SystemExitrF   )
r!   Úlive_serverrs   r{   Ústartr*   rT   r   rf   r8   r   r   r   Úbuildæ   sL   




ÿ

r‹   c                 C   s   t j | ¡rt  | ¡rdS dS )zI Check if the site directory contains stale files from a previous build. TF)rJ   r5   ÚexistsÚlistdir)Úsite_directoryr   r   r   r�   /  s   r�   )Nr   )F)FF)ÚloggingrJ   rP   Úurllib.parser   Újinja2.exceptionsr   ra   r'   r   Úmkdocs.structure.filesr   Úmkdocs.structure.navr   r   Ú	getLoggerr   rD   Ú	addFilterr†   r+   r;   rZ   rh   rt   rz   r‹   r�   r   r   r   r   Ú<module>   s,    

%

 
0I