
 XN                 @   si  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 Wn e k
 r d Z Yn Xd	 Z d
 d   Z d d   Z d d d  Z i d d 6d d 6d d 6d d 6d d 6Z d d   Z d d d d  Z e ree j j j j d e  x* e j   D] \ Z Z e e e  qEWn  d S)zBMisc. utility functions/classes for admin documentation generator.    N)HeaderParseError)HeaderParser)reverse)force_bytes)	mark_safeFTc                s   |  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   s7   |  ]- } | j    r t |  t | j     Vq d  S)N)lstriplen).0line r   L/home/ubuntu/projects/ifolica/build/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    4r   c             C   s   t  |   }  t j d |   } | d } t |  d k rI d } i  } n t   } y | j | d  } Wn1 t k
 r i  } d j | d d   } YnN Xt | j	    } | r d 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   s   i d d 6d d 6| d 6t  d  j d  d 6d	 d
 6d	 d 6} | rX t d |  } n  d } t j j | |  d | d d d d d | } t | d  S)z<
    Convert the string from reST to an XHTML fragment.
    TZdoctitle_xform   Zinitial_header_leveldefault_reference_contextzdjango-admindocs-docroot/	link_baseFZraw_enabledZfile_insertion_enabledz<%s>z7
.. default-role:: cmsreference

%s

.. default-role::
source_pathZdestination_pathNZwriter_namehtmlZ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/modelz%s/views/%s/Zviewz%s/templates/%s/templatez%s/filters/#%sfilterz%s/tags/#%stagc                s5   d  d    f d d  } t  j j j j |  |  d  S)Nc                sn   | d  k r i  } n  | d  k r* g  } n  t  j j | | d   | j j j | j   f | } | 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)r/   parsersrstrolesregister_canonical_role)ZrolenamerE   rF   r   )rE   r   create_reference_roleh   s    rK   c       	      C   s   | d  k r i  } n  | d  k r* g  } n  | j  j j } t j j | | d t | | j  j j | j   f | } | g g  f S)Nr8   )	r;   r<   r)   r/   r9   r:   ROLESr+   r=   )	r>   r?   r1   r@   rA   rB   rC   contextrD   r   r   r   default_reference_role{   s    				rN   Zcmsreference)__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   rL   rK   rN   rG   rH   rI   rJ   r!   r>   rE   r   r   r   r   <module>   s8   !
