3
(hN                 @   s   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ yddlZddlZddlZW n ek
r|   dZY nX d	Zd
d Zdd ZdddZddddddZdd ZdddZerejjjjde xej D ]\ZZeee qW dS )zBMisc. utility functions/classes for admin documentation generator.    N)HeaderParseError)HeaderParser)reverse)force_bytes)	mark_safeFTc                sh   |  s| j   rdS | j j }tdd |D  |d j g fdd|dd D  }d	j|j  S )
z
    Uniformly trim leading/trailing whitespace from docstrings.

    Based on https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
     c             s   s*   | ]"}|j  rt|t|j   V  qd S )N)lstriplen).0line r   B/tmp/pip-install-q3hcpn_q/Django/django/contrib/admindocs/utils.py	<genexpr>   s    z!trim_docstring.<locals>.<genexpr>r   c                s   g | ]}| d  j  qS )N)rstrip)r
   r   )indentr   r   
<listcomp>    s    z"trim_docstring.<locals>.<listcomp>   N
)strip
expandtabs
splitlinesminr   join)	docstringlinesZtrimmedr   )r   r   trim_docstring   s    (r   c             C   s   t | } tjd| }|d }t|dkr2d}i }nzt }y|j|d }W n* tk
rt   i }dj|dd }Y n8X t|j	 }|rdj|dd }ndj|dd }|||fS )	zN
    Parse out the parts of a docstring.  Return (title, body, metadata).
    z\n{2,}r   r   r   z

Nr   )
r   resplitr	   r   parsestrr   r   dictitems)r   partstitlebodymetadataparserr   r   r   parse_docstring$   s"    r'   c             C   sV   dd|t djdddd}|r,td| }d}tjj||  |d	d
|d}t|d S )z<
    Convert the string from reST to an XHTML fragment.
    T   zdjango-admindocs-docroot/F)Zdoctitle_xformZinitial_header_leveldefault_reference_context	link_baseZraw_enabledZfile_insertion_enabledz<%s>z7
.. default-role:: cmsreference

%s

.. default-role::
Nhtml)source_pathZdestination_pathZwriter_nameZsettings_overridesfragment)r   r   r   docutilscoreZpublish_partsr   )textr*   Zthing_being_parsedZ	overridessourcer"   r   r   r   	parse_rst>   s    	
r3   z%s/models/%s/z%s/views/%s/z%s/templates/%s/z%s/filters/#%sz%s/tags/#%s)modelviewtemplatefiltertagc                s$   d fdd	}t jjjj| | d S )Nc                sN   |d kri }|d krg }t jj||fd |jjj|j f i|}|gg fS )Nrefuri)r/   nodes	referencedocumentsettingsr+   lower)namerawtextr1   linenoinlineroptionscontentnode)urlbaser   r   _rolei   s    z$create_reference_role.<locals>._role)NN)r/   parsersrstrolesregister_canonical_role)ZrolenamerF   rG   r   )rF   r   create_reference_roleh   s    rL   c       	      C   s\   |d kri }|d krg }|j jj}tjj||fdt| |j jj|j f i|}|gg fS )Nr9   )	r<   r=   r*   r/   r:   r;   ROLESr+   r>   )	r?   r@   r1   rA   rB   rC   rD   contextrE   r   r   r   default_reference_role{   s    
rO   Zcmsreference)N)NN)__doc__r   Zemail.errorsr   email.parserr   Zdjango.urlsr   Zdjango.utils.encodingr   Zdjango.utils.safestringr   Zdocutils.corer/   Zdocutils.nodesZdocutils.parsers.rst.rolesImportErrorZdocutils_is_availabler   r'   r3   rM   rL   rO   rH   rI   rJ   rK   r!   r?   rF   r   r   r   r   <module>   s6   

"
