B
    &d,                 @   st   d Z ddlmZ G dd deZd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 Zdd ZdS )a  
    TOC directive
    ~~~~~~~~~~~~~

    The TOC directive syntax looks like::

        .. toc:: Title
           :depth: 3

    "Title" and "depth" option can be empty. "depth" is an integer less
    than 6, which defines the max heading level writers want to include
    in TOC.
   )	Directivec               @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )DirectiveToc   c             C   s
   || _ d S )N)depth)selfr    r   c/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/mistune/directives/toc.py__init__   s    zDirectiveToc.__init__c          	   C   sj   | d}d }| |}|rZt|d}|rZyt|}W n ttfk
rX   dddS X dd ||fdS )Nvaluer   Zblock_errorzTOC depth MUST be integer)typerawtoc)r   r   params)groupZparse_optionsdictgetint
ValueError	TypeError)r   blockmstatetitler   optionsr   r   r   parse   s    


zDirectiveToc.parsec             C   s   | j |d< g |d< ||fS )N	toc_depthtoc_headings)r   )r   mdsr   r   r   r   reset_toc_state'   s    
zDirectiveToc.reset_toc_statec             C   s\   t |j_|j| j |jt |jj	dkr>|j
dt n|jj	dkrX|j
dt d S )Nhtmltheadingast)record_toc_headingr   Ztokenize_headingZbefore_parse_hooksappendr   Zbefore_render_hooksmd_toc_hookrendererNAMEregisterrender_html_theadingrender_ast_theading)r   r   r   r   r   register_plugin,   s    zDirectiveToc.register_pluginc             C   sP   |  |d | | |jjdkr2|jdt n|jjdkrL|jdt d S )Nr   r    r"   )Zregister_directiver+   r&   r'   r(   render_html_tocrender_ast_toc)r   r   r   r   r   __call__6   s    
zDirectiveToc.__call__N)r   )__name__
__module____qualname__r	   r   r   r+   r.   r   r   r   r   r      s
   

r   c             C   s<   dt t|d d  }|d || |f d| ||fdS )NZtoc_r   r   r!   )r   textr   )strlenr$   )r2   levelr   tidr   r   r   r#   @   s    r#   c                s|   | d}|s|S | dd}tt| j||}xF|D ]>}|d dkr6|d }|d pX|  fdd	|D }||d
< q6W |S )Nr   r   r   r   r   r   r   c                s   g | ]}|d   kr|qS )   r   ).0d)r   r   r   
<listcomp>S   s    zmd_toc_hook.<locals>.<listcomp>r   )r   list_cleanup_headings_textinline)r   tokensr   headingsZdefault_depthtokr   itemsr   )r   r   r%   G   s    

r%   c             C   s   ddd | D ||dS )Nr   c             S   s   g | ]}t |qS r   )r;   )r8   r9   r   r   r   r:   [   s    z"render_ast_toc.<locals>.<listcomp>)r   rA   r   r   r   )rA   r   r   r   r   r   r-   X   s    r-   c             C   s   d| ||dS )Nheading)r   childrenr5   idr   )rC   r5   r6   r   r   r   r*   a   s    r*   c             C   s(   d}|r|d| d 7 }|t |  d S )Nz<section class="toc">
z<h1>z</h1>
z</section>
)render_toc_ul)rA   r   r   r    r   r   r   r,   h   s    r,   c             C   s0   dt | }d| d | d |  d | d S )Nh<z id="z">z</z>
)r3   )r2   r5   r6   tagr   r   r   r)   p   s    r)   c             C   sB   |  |i \}}| j|| |d}|s0g S tt| j||S )a&  Extract TOC headings into list structure of::

        [
          ('toc_1', 'Introduction', 1),
          ('toc_2', 'Install', 2),
          ('toc_3', 'Upgrade', 2),
          ('toc_4', 'License', 1),
        ]

    :param md: Markdown Instance with TOC plugin.
    :param s: text string.
    r   )Zbefore_parser   r   r   r;   r<   r=   )r   r   r   r?   r   r   r   extract_toc_itemsu   s    
rI   c             C   s>  | sdS d}g }x| D ]\}}}d ||}|sH|d| 7 }|| q||d krb|d| 7 }q||d kr|d| 7 }|| q| }x||r| }||kr|d| 7 }|| P q||kr|d| 7 }|| || P q|d	7 }qW || |d| 7 }qW x$t|d
kr4|d	7 }|  qW |d	 S )al  Render a <ul> table of content HTML. The param "toc" should
    be formatted into this structure::

        [
          (toc_id, text, level),
        ]

    For example::

        [
          ('toc-intro', 'Introduction', 1),
          ('toc-install', 'Install', 2),
          ('toc-upgrade', 'Upgrade', 2),
          ('toc-license', 'License', 1),
        ]
     z<ul>
z<a href="#{}">{}</a>z<li>z
</li>
<li>z

<ul>
<li>z</li>
</ul>
</li>
<li>z</li>
</ul>
r   )formatr$   popr4   )r   r   Zlevelskr2   r5   itemZ
last_levelr   r   r   rE      sB    



rE   c             c   sR   xL|D ]D}|d }|  ||| j}ddd |D }|d ||d fV  qW d S )Nr   rJ   c             s   s   | ]}t |V  qd S )N)_inline_token_text)r8   r@   r   r   r   	<genexpr>   s    z)_cleanup_headings_text.<locals>.<genexpr>    r7   )Z_scanrulesjoin)r=   rA   r   rO   r2   r>   r   r   r   r<      s
    
r<   c             C   s<   | d }|dkrdS t | dkr(| d S |dkr8| d S dS )NrR   Zinline_htmlrJ   r7   r   >   imagelink)r4   )tokentok_typer   r   r   rP      s    rP   N)__doc__baser   r   r#   r%   r-   r*   r,   r)   rI   rE   r<   rP   r   r   r   r   <module>   s   .	: