o
    Ì^½^;  ã                   @   sd   d Z ddlZddlZddlmZ ddlmZ e d¡ZdZ	dd„ Z
G d	d
„ d
ƒZG dd„ deƒZdS )z(
Implements the plugin API for MkDocs.

é    N)ÚOrderedDict)ÚConfigúmkdocs.plugins)ÚconfigÚ	pre_buildÚfilesÚnavÚenvÚpre_templateÚtemplate_contextÚpost_templateÚpre_pageÚpage_read_sourceÚpage_markdownÚpage_contentÚpage_contextÚ	post_pageÚ
post_buildÚservec                  C   s   t jdd�} dd„ | D ƒS )z1 Return a dict of all installed Plugins by name. r   )Úgroupc                 S   s   i | ]}|j |“qS © )Úname)Ú.0Úpluginr   r   ú0/usr/lib/python3/dist-packages/mkdocs/plugins.pyÚ
<dictcomp>   s    zget_plugins.<locals>.<dictcomp>)Úpkg_resourcesÚiter_entry_points)Úpluginsr   r   r   Úget_plugins   s   r   c                   @   s"   e Zd ZdZdZi Zddd„ZdS )Ú
BasePluginzI
    Plugin base class.

    All plugins should subclass this class.
    r   Nc                 C   s&   t | j|d�| _| j |¡ | j ¡ S )zK Load config from a dict of options. Returns a tuple of (errors, warnings).)ÚschemaÚconfig_file_path)r   Úconfig_schemer   Ú	load_dictÚvalidate)ÚselfÚoptionsr"   r   r   r   Úload_config*   s   
zBasePlugin.load_config©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r(   r   r   r   r   r        s
    r    c                       s>   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Zdd	d
„Z‡  ZS )ÚPluginCollectionzì
    A collection of plugins.

    In addition to being a dict of Plugin instances, each event method is registered
    upon being added. All registered methods for a given event can then be run in order
    by calling `run_event`.
    c                    s&   t ƒ j|i |¤Ž dd„ tD ƒ| _d S )Nc                 S   s   i | ]}|g “qS r   r   ©r   Úxr   r   r   r   >   s    z-PluginCollection.__init__.<locals>.<dictcomp>)ÚsuperÚ__init__ÚEVENTSÚevents)r&   ÚargsÚkwargs©Ú	__class__r   r   r2   <   s   zPluginCollection.__init__c                 C   s   | j |  |¡ dS )z! Register a method for an event. N)r4   Úappend)r&   Ú
event_nameÚmethodr   r   r   Ú_register_event@   s   z PluginCollection._register_eventc                    s~   t |tƒstd | j| jtjtj¡ƒ‚tƒ j||fi |¤Ž dd„ t|ƒD ƒD ]}t	||ƒ}t
|ƒr<|  |dd … |¡ q'd S )NzE{0}.{1} only accepts values which are instances of {2}.{3} sublcassesc                 s   s   � | ]
}|  d ¡r|V  qdS )Úon_N)Ú
startswithr/   r   r   r   Ú	<genexpr>L   s   € z/PluginCollection.__setitem__.<locals>.<genexpr>é   )Ú
isinstancer    Ú	TypeErrorÚformatr+   r*   r1   Ú__setitem__ÚdirÚgetattrÚcallabler<   )r&   ÚkeyÚvaluer6   r:   r;   r7   r   r   rD   D   s   

ÿþ
€ýzPluginCollection.__setitem__Nc                 K   sL   |du}| j | D ]}|r||fi |¤Ž}n|di |¤Ž}|dur#|}q	|S )a[  
        Run all registered methods of an event.

        `item` is the object to be modified or replaced and returned by the event method.
        If it isn't given the event method creates a new object to be returned.
        All other keywords are variables for context, but would not generally
        be modified by the event method.
        Nr   )r4   )r&   r   Úitemr6   Ú	pass_itemr;   Úresultr   r   r   Ú	run_eventQ   s   
€zPluginCollection.run_eventr)   )	r*   r+   r,   r-   r2   r<   rD   rM   Ú__classcell__r   r   r7   r   r.   3   s    r.   )r-   r   ÚloggingÚcollectionsr   Úmkdocs.config.baser   Ú	getLoggerÚlogr3   r   r    r.   r   r   r   r   Ú<module>   s    
