o
    Ì^½^s(  ã                   @   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m	Z	 e 
e¡Ze e	j¡ G dd„ dƒZG dd„ dƒZd	d
„ Zdd„ Zdd„ ZdS )é    N)Ú
cmp_to_key)Úquote)Úutilsc                   @   sz   e Zd ZdZdd„ Zdd„ Zdd„ Z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„ Zdd„ ZdS )ÚFilesz A collection of File objects. c                 C   s   || _ dd„ |D ƒ| _d S )Nc                 S   s   i | ]}|j |“qS © )Úsrc_path©Ú.0Úfiler   r   ú8/usr/lib/python3/dist-packages/mkdocs/structure/files.pyÚ
<dictcomp>   s    z"Files.__init__.<locals>.<dictcomp>)Ú_filesÚ	src_paths)ÚselfÚfilesr   r   r   Ú__init__   s   zFiles.__init__c                 C   ó
   t | jƒS ©N)Úiterr   ©r   r   r   r   Ú__iter__   ó   
zFiles.__iter__c                 C   r   r   )Úlenr   r   r   r   r   Ú__len__   r   zFiles.__len__c                 C   s
   || j v S r   )r   ©r   Úpathr   r   r   Ú__contains__   r   zFiles.__contains__c                 C   s   | j  tj |¡¡S )z: Return a File instance with File.src_path equal to path. )r   ÚgetÚosr   Únormpathr   r   r   r   Úget_file_from_path   s   zFiles.get_file_from_pathc                 C   s   | j  |¡ || j|j< dS )z" Append file to Files collection. N)r   Úappendr   r   )r   r
   r   r   r   r!   !   s   zFiles.appendFc                 C   s    | D ]}|  ¡ s| |¡ qdS )z/ Copy static files from source to destination. N)Úis_documentation_pageÚ	copy_file)r   Údirtyr
   r   r   r   Úcopy_static_files&   s
   
€þzFiles.copy_static_filesc                 C   ó   dd„ | D ƒS )z4 Return iterable of all Markdown page file objects. c                 S   ó   g | ]}|  ¡ r|‘qS r   )r"   r   r   r   r   Ú
<listcomp>.   ó    z-Files.documentation_pages.<locals>.<listcomp>r   r   r   r   r   Údocumentation_pages,   ó   zFiles.documentation_pagesc                 C   r&   )z2 Return iterable of all static page file objects. c                 S   r'   r   )Úis_static_pager   r   r   r   r(   2   r)   z&Files.static_pages.<locals>.<listcomp>r   r   r   r   r   Ústatic_pages0   r+   zFiles.static_pagesc                 C   r&   )zR Return iterable of all file objects which are not documentation or static pages. c                 S   r'   r   )Úis_media_filer   r   r   r   r(   6   r)   z%Files.media_files.<locals>.<listcomp>r   r   r   r   r   Úmedia_files4   r+   zFiles.media_filesc                 C   r&   )z1 Return iterable of all javascript file objects. c                 S   r'   r   )Úis_javascriptr   r   r   r   r(   :   r)   z*Files.javascript_files.<locals>.<listcomp>r   r   r   r   r   Újavascript_files8   r+   zFiles.javascript_filesc                 C   r&   )z* Return iterable of all CSS file objects. c                 S   r'   r   )Úis_cssr   r   r   r   r(   >   r)   z#Files.css_files.<locals>.<listcomp>r   r   r   r   r   Ú	css_files<   r+   zFiles.css_filesc              
      s|   ‡ fdd„}|j |d�D ]/}tj |¡}|| vr;ˆ d jD ]}tj tj ||¡¡r:|  t||ˆ d ˆ d ƒ¡  nqqdS )zE Retrieve static files from Jinja environment and add to collection. c                    sR   g d¢}|  dd„ tjD ƒ¡ |  ˆ d j¡ |D ]}t |  ¡ |¡r& dS qdS )N)ú.*z*/.*z*.pyz*.pycz*.htmlz*readme*zmkdocs_theme.ymlc                 s   s   � | ]}d   |¡V  qdS )z*{}N)Úformat)r	   Úxr   r   r   Ú	<genexpr>E   s   € z=Files.add_files_from_theme.<locals>.filter.<locals>.<genexpr>ÚthemeFT)Úextendr   Úmarkdown_extensionsÚstatic_templatesÚfnmatchÚlower)ÚnameÚpatternsÚpattern©Úconfigr   r   ÚfilterB   s   ÿz*Files.add_files_from_theme.<locals>.filter)Úfilter_funcr8   Úsite_dirÚuse_directory_urlsN)	Úlist_templatesr   r   r   ÚdirsÚisfileÚjoinr!   ÚFile)r   ÚenvrB   rC   r   Údirr   rA   r   Úadd_files_from_theme@   s   	þ€úzFiles.add_files_from_themeN©F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r    r!   r%   r*   r-   r/   r1   r3   rN   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d	„ Z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„ Zdd„ ZdS )!rK   a»  
    A MkDocs File object.

    Points to the source and destination locations of a file.

    The `path` argument must be a path that exists relative to `src_dir`.

    The `src_dir` and `dest_dir` must be absolute paths on the local file system.

    The `use_directory_urls` argument controls how destination paths are generated. If `False`, a Markdown file is
    mapped to an HTML file of the same name (the file extension is changed to `.html`). If True, a Markdown file is
    mapped to an HTML index file (`index.html`) nested in a directory using the "name" of the file in `path`. The
    `use_directory_urls` argument has no effect on non-Markdown files.

    File objects have the following properties, which are Unicode strings:

    File.src_path
        The pure path of the source file relative to the source directory.

    File.abs_src_path
        The absolute concrete path of the source file.

    File.dest_path
        The pure path of the destination file relative to the destination directory.

    File.abs_dest_path
        The absolute concrete path of the destination file.

    File.url
        The url of the destination file relative to the destination directory as a string.
    c                 C   sn   d | _ tj |¡| _tj tj || j¡¡| _|  ¡ | _|  	|¡| _
tj tj || j
¡¡| _|  |¡| _d S r   )Úpager   r   r   r   rJ   Úabs_src_pathÚ	_get_stemr>   Ú_get_dest_pathÚ	dest_pathÚabs_dest_pathÚ_get_urlÚurl)r   r   Úsrc_dirÚdest_dirrF   r   r   r   r   v   s   
zFile.__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   rU   r[   r   )r	   ÚkeyÚvaluer   r   r   r   ‚   s    z1File.__eq__.<locals>.sub_dict.<locals>.<dictcomp>)Úitems)Údr   r   r   Úsub_dict�   s   zFile.__eq__.<locals>.sub_dict)Ú
isinstanceÚ	__class__Ú__dict__)r   Úotherrb   r   r   r   Ú__eq__   s    zFile.__eq__c                 C   s   |   |¡ S r   )rg   ©r   rf   r   r   r   Ú__ne__†   s   zFile.__ne__c                 C   s.   t j | j¡}t j |¡\}}|dv rdS |S )z5 Return the name of the file without it's extension. ©ÚindexÚREADMErk   )r   r   Úbasenamer   Úsplitext)r   ÚfilenameÚstemÚextr   r   r   rV   ‰   s   zFile._get_stemc                 C   sT   |   ¡ r'tj | j¡\}}|r| jdkrtj || jd ¡S tj || jd¡S | jS )z/ Return destination path based on source path. rk   ú.htmlú
index.html)r"   r   r   Úsplitr   r>   rJ   )r   rF   Úparentro   r   r   r   rW   �   s   zFile._get_dest_pathc                 C   sR   | j  tjjd¡}tj |¡\}}|r%|dkr%|dkr!d}t|ƒS |d }t|ƒS )z' Return url based in destination path. ú/rs   Ú Ú.)rX   Úreplacer   r   Úseprt   Úurlquote)r   rF   r[   Údirnamero   r   r   r   rZ   œ   s   ÿzFile._get_urlc                 C   s    t  | jt|tƒr|j¡S |¡S )z- Return url for file relative to other file. )r   Úget_relative_urlr[   rc   rK   rh   r   r   r   Úurl_relative_to§   s    zFile.url_relative_toFc                 C   sH   |r|   ¡ st d | j¡¡ dS t d | j¡¡ t | j| j¡ dS )zE Copy source file to destination, ensuring parent directories exist. z"Skip copying unmodified file: '{}'zCopying media file: '{}'N)	Úis_modifiedÚlogÚdebugr5   r   r   r#   rU   rY   )r   r$   r   r   r   r#   «   s   zFile.copy_filec                 C   s.   t j | j¡rt j | j¡t j | j¡k S dS )NT)r   r   rI   rY   ÚgetmtimerU   r   r   r   r   r   ³   s   zFile.is_modifiedc                 C   s   t j | j¡d tjv S )z) Return True if file is a Markdown page. é   )r   r   rn   r   r   r:   r   r   r   r   r"   ¸   s   zFile.is_documentation_pagec                 C   ó   t j | j¡d dv S )z9 Return True if file is a static page (html, xml, json). rƒ   )rr   z.htmz.xmlz.json©r   r   rn   r   r   r   r   r   r,   ¼   ó   zFile.is_static_pagec                 C   s   |   ¡ p|  ¡  S )z< Return True if file is not a documentation or static page. )r"   r,   r   r   r   r   r.   Å   s   zFile.is_media_filec                 C   r„   )z+ Return True if file is a JavaScript file. rƒ   )z.jsz.javascriptr…   r   r   r   r   r0   É   r†   zFile.is_javascriptc                 C   r„   )z$ Return True if file is a CSS file. rƒ   )z.cssr…   r   r   r   r   r2   Ð   r†   zFile.is_cssNrO   )rP   rQ   rR   rS   r   rg   ri   rV   rW   rZ   r~   r#   r   r"   r,   r.   r0   r2   r   r   r   r   rK   V   s     	
	rK   c           
   
   C   s  g }ddg}t j| d dd�D ]o\}}}t j || d ¡}t|ƒD ]}t j t j ||¡¡}t||d|d�r;| |¡ q!| 	¡  t
|ƒD ]9}	t j t j ||	¡¡}t|	|d|d�rZqD|	 ¡ dkrmd	|v rmt d
 |¡¡ qD| t|| d | d | d ƒ¡ qDqt|ƒS )z4 Walk the `docs_dir` and return a Files collection. r4   z
/templatesÚdocs_dirT)Úfollowlinks)rm   r   Úis_dirÚexcludeFz	readme.mdzindex.mdz=Both index.md and readme.md found. Skipping readme.md from {}rE   rF   )r   Úwalkr   ÚrelpathÚlistr   rJ   Ú_filter_pathsÚremoveÚsortÚ_sort_filesr=   r€   Úwarningr5   r!   rK   r   )
rB   r   rŠ   Ú
source_dirÚdirnamesÚ	filenamesÚrelative_dirr|   r   ro   r   r   r   Ú	get_files×   s(   
€"÷r—   c                 C   s   dd„ }t | t|ƒd�S )z< Always sort `index` or `README` as first filename in list. c                 S   sH   | |krdS t j |¡d dv rdS t j | ¡d dv s | |k r"dS dS )Nr   rj   rƒ   éÿÿÿÿ)r   r   rn   )r6   Úyr   r   r   Úcompare÷   s   z_sort_files.<locals>.compare)r^   )Úsortedr   )r•   rš   r   r   r   r‘   ô   s   	r‘   c                 C   sH   |D ]}|  d¡r|sq| d¡r|n| }t || d¡¡r! dS qdS )z" .gitignore style file filtering. rv   TF)ÚendswithÚ
startswithr<   Ústrip)rm   r   r‰   rŠ   ÚitemÚmatchr   r   r   rŽ     s   ÿrŽ   )r<   r   ÚloggingÚ	functoolsr   Úurllib.parser   r{   Úmkdocsr   Ú	getLoggerrP   r€   Ú	addFilterÚwarning_filterr   rK   r—   r‘   rŽ   r   r   r   r   Ú<module>   s    
H 