o
    Æiéa  ã                   @   s~   d dl Z d dlZd dlZd dlZd dlZd dlmZ e e¡Z	G dd„ dƒZ
G dd„ deƒZG dd„ dƒZG d	d
„ d
eƒZdS )é    N)Ú
HTMLParserc                   @   sH   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S )ÚSearchIndexzy
    Search index is a collection of pages and sections (heading
    tags and their following content are sections).
    c                 K   s   g | _ || _d S ©N)Ú_entriesÚconfig)Úselfr   © r   úD/usr/lib/python3/dist-packages/mkdocs/contrib/search/search_index.pyÚ__init__   s   
zSearchIndex.__init__c                 C   s>   |D ]}|j |kr|  S |  |j|¡}|dur|  S qdS )zx
        Given a table of contents and HTML ID, iterate through
        and return the matched item in the TOC.
        N)ÚidÚ_find_toc_by_idÚchildren)r   ÚtocÚid_Útoc_itemÚ
toc_item_rr   r   r	   r      s   
ÿüzSearchIndex._find_toc_by_idc                 C   sD   |  dd¡}t dd| ¡ ¡}| j |t| d¡dd�|dœ¡ dS )zc
        A simple wrapper to add an entry and ensure the contents
        is UTF8 encoded.
        õ   Â ú z[ \t\n\r\f\v]+úutf-8)Úencoding)ÚtitleÚtextÚlocationN)ÚreplaceÚreÚsubÚstripr   ÚappendÚstrÚencode)r   r   r   Úlocr   r   r	   Ú
_add_entry"   s   ýzSearchIndex._add_entryc                 C   s`   t ƒ }| |j¡ | ¡  |j}| j|j|  |j¡ d¡|d� |j	D ]
}|  
||j|¡ q#dS )z–
        Create a set of entries in the index for a page. One for
        the page itself and then one for each of its' heading
        tags.
        Ú
©r   r   r    N)ÚContentParserÚfeedÚcontentÚcloseÚurlr!   r   Ú
strip_tagsÚrstripÚdataÚcreate_entry_for_sectionr   )r   ÚpageÚparserr(   Úsectionr   r   r	   Úadd_entry_from_context0   s   
ý
ÿz"SearchIndex.add_entry_from_contextc                 C   s>   |   ||j¡}|dur| j|jd |j¡||j d� dS dS )z“
        Given a section on the page, the table of contents and
        the absolute url for the page create an entry in the
        index
        Nr   r#   )r   r   r!   r   Újoinr   r(   )r   r/   r   Úabs_urlr   r   r   r	   r,   L   s   

ýÿz$SearchIndex.create_entry_for_sectionc              
   C   s  | j | jdœ}tj|ddd�}| jd dv r‹zWtj tj tj t	¡¡d¡}t
jd|gt
jt
jt
jd	�}| | d
¡¡\}}|sct|dƒrJ| d
¡n|}t |¡|d< tj|ddd�}t d¡ W |S t d |¡¡ W |S  ttfyŠ } zt d |¡¡ W Y d}~|S d}~ww |S )zpython to json conversion)Údocsr   T)ú,ú:)Ú	sort_keysÚ
separatorsÚprebuild_index)TÚnodezprebuild-index.jsr9   )ÚstdinÚstdoutÚstderrr   ÚdecodeÚindexz,Pre-built search index created successfully.z+Failed to pre-build search index. Error: {}N)r   r   ÚjsonÚdumpsÚosÚpathr1   ÚdirnameÚabspathÚ__file__Ú
subprocessÚPopenÚPIPEÚcommunicater   Úhasattrr=   ÚloadsÚlogÚdebugÚwarningÚformatÚOSErrorÚ
ValueError)r   Ú
page_dictsr+   Úscript_pathÚpÚidxÚerrÚer   r   r	   Úgenerate_search_index\   s8   þüüý€ýz!SearchIndex.generate_search_indexc                 C   s   t ƒ }| |¡ | ¡ S )zstrip html tags from data)ÚHTMLStripperr%   Úget_data)r   ÚhtmlÚsr   r   r	   r)   z   s   
zSearchIndex.strip_tagsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r!   r0   r,   rX   r)   r   r   r   r	   r      s    r   c                       s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )rY   z•
    A simple HTML parser that stores all of the data within tags
    but ignores the tags themselves and thus strips them from the
    content.
    c                    s   t ƒ j|i |¤Ž g | _d S r   )Úsuperr
   r+   ©r   ÚargsÚkwargs©Ú	__class__r   r	   r
   ˆ   s   
zHTMLStripper.__init__c                 C   s   | j  |¡ dS )ú;
        Called for the text contents of each tag.
        N)r+   r   )r   Údr   r   r	   Úhandle_data�   s   zHTMLStripper.handle_datac                 C   s   d  | j¡S )Nr"   )r1   r+   )r   r   r   r	   rZ   “   s   zHTMLStripper.get_data)r]   r^   r_   r`   r
   ri   rZ   Ú__classcell__r   r   re   r	   rY   �   s
    rY   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚContentSectionzm
    Used by the ContentParser class to capture the information we
    need when it is parsing the HMTL.
    Nc                 C   s   |pg | _ || _|| _d S r   )r   r   r   )r   r   r   r   r   r   r	   r
   �   s   

zContentSection.__init__c                 C   s&   t | j|jk| j|jk| j|jkgƒS r   )Úallr   r   r   )r   Úotherr   r   r	   Ú__eq__¢   s
   


ýzContentSection.__eq__)NNN)r]   r^   r_   r`   r
   rn   r   r   r   r	   rk   —   s    
rk   c                       s8   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡  ZS )
r$   zš
    Given a block of HTML, group the content under the preceding
    heading tags which can then be used for creating an index
    for that section.
    c                    s(   t ƒ j|i |¤Ž g | _d | _d| _d S )NF)ra   r
   r+   r/   Úis_header_tagrb   re   r   r	   r
   ±   s   
zContentParser.__init__c                 C   s^   |dd„ t ddƒD ƒvrdS d| _tƒ | _| j | j¡ |D ]}|d dkr,|d | j_qdS )	z&Called at the start of every HTML tag.c                 S   ó   g | ]}d | ‘qS ©zh%dr   ©Ú.0Úxr   r   r	   Ú
<listcomp>½   ó    z1ContentParser.handle_starttag.<locals>.<listcomp>é   é   NTr   r   )Úrangero   rk   r/   r+   r   r   )r   ÚtagÚattrsÚattrr   r   r	   Úhandle_starttag¹   s   €þzContentParser.handle_starttagc                 C   s&   |dd„ t ddƒD ƒvrdS d| _dS )z$Called at the end of every HTML tag.c                 S   rp   rq   r   rr   r   r   r	   ru   Î   rv   z/ContentParser.handle_endtag.<locals>.<listcomp>rw   rx   NF)ry   ro   )r   rz   r   r   r	   Úhandle_endtagÊ   s   
zContentParser.handle_endtagc                 C   s8   | j du rdS | jr|| j _dS | j j | d¡¡ dS )rg   Nr"   )r/   ro   r   r   r   r*   )r   r+   r   r   r	   ri   Ó   s
   
zContentParser.handle_data)	r]   r^   r_   r`   r
   r}   r~   ri   rj   r   r   re   r	   r$   ª   s    	r$   )rA   r   r?   ÚloggingrF   Úhtml.parserr   Ú	getLoggerr]   rL   r   rY   rk   r$   r   r   r   r	   Ú<module>   s    
u