B
    nb\"                 @   s$  d Z ddlZddlZddlmZ ddl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mZ ddlZddlmZ ddlmZmZmZ dd	lmZ ed
ZeeddZeedZededdedddZededdeddZdd Z dd Z!dd Z"d$ddZ#d%d d!Z$d"d# Z%dS )&a  
Process docstrings with Sphinx

AUTHORS:
- Tim Joseph Dumol (2009-09-29): initial version
- The Spyder Project Contributors: Several changes to make it work with Spyder

Copyright (C) 2009 Tim Dumol <tim@timdumol.com>
Copyright (C) Spyder Project Contributors
Distributed under the terms of the BSD License

Taken from the Sage project (www.sagemath.org).
See here for the original version:
www.sagemath.org/doc/reference/sagenb/misc/sphinxify.html
    N)mkdtemp)escape)SystemMessage)EnvironmentFileSystemLoader)Sphinx)_get_module_data_pathget_module_source_path)encodingzspyder.utils.helpstaticZcssZjsZspyderZutilshelpZmathjaxZMATHJAXPATH)relpathZ	attr_nameZ
JQUERYPATHc             C   s   d| kpd| kS )z;Returns whether a string contains Sphinx-style ReST markup.`z:: )	docstringr   r   :lib/python3.7/site-packages/spyder/utils/help/sphinxify.pyis_sphinx_markupB   s    r   c             C   s0   t  }tttd|_|d}|jt| dS )z-Print a warning message on the rich text view	templateszwarning.html)css_pathtext)	r   r   ospjoinCONFDIR_PATHloaderget_templaterenderCSS_PATH)messageenvwarningr   r   r   r    H   s    
r    c             C   s6   t  }tttd|_|d}|jt| |||dS )z+Print a usage message on the rich text viewr   z
usage.html)r   titleZintro_messagetutorial_messagetutorial)	r   r   r   r   r   r   r   r   r   )r!   r   r"   r#   r   usager   r   r   r$   P   s
    

r$    Fc             C   sT   |rt jdkr|dd}|r"dnd| ||||tttttjdk rDdndt	j
d}|S )aH  
    Generate the html_context dictionary for our Sphinx conf file.
    
    This is a set of variables to be passed to the Jinja template engine and
    that are used to control how the webpage is rendered in connection with
    Sphinx

    Parameters
    ----------
    name : str
        Object's name.
    note : str
        A note describing what type has the function or method being
        introspected
    argspec : str
        Argspec of the the function or method being introspected
    math : bool
        Turn on/off Latex rendering on the OI. If False, Latex will be shown in
        plain text.
    collapse : bool
        Collapse sections
    img_path : str
        Path for images relative to the file containing the docstring

    Returns
    -------
    A dict of strings to be used by Jinja to generate the webpage
    nt\/truer%   z1.1)math_onnameargspecnotecollapseimg_pathr   Zjs_pathZjquery_pathZmathjax_pathright_sphinx_versionplatform)osr+   replacer   JS_PATHJQUERY_PATHMATHJAX_PATHsphinx__version__sysr1   )r+   r,   r-   Zmathr.   r/   contextr   r   r   generate_contextY   s    

r;   htmlc             C   s  t  }t|}t|d}t|d}|dkr6d}nd}t|d| }|d rf|d rf| d	d
} t|d }x dD ]}	||	d|	 d }qxW ||d< tj|ddd}
|
	|  |

  d}|rt  }t|}t| nttd}d|i}t|d}t||||||dddddd}y|d|g W n" tk
rT   td}t|S X t|rtj|ddd }|dd}ntd}t|S |rtj|dd tj|dd |S )a  
    Runs Sphinx on a docstring and outputs the processed documentation.

    Parameters
    ----------
    docstring : str
        a ReST-formatted docstring

    context : dict
        Variables to be passed to the layout template to control how its
        rendered (through the Sphinx variable *html_context*).

    buildername:  str
        It can be either `html` or `text`.

    Returns
    -------
    An Sphinx-processed string, in either HTML or plain text format, depending
    on the value of `buildername`
    Z_buildzdocstring.rstr<   z.htmlz.txtr   r0   r*   z\\z\\\\r,   )=,()*z**z <span class="argspec-highlight">z</span>wzutf-8)r   Fzspyder.utils.helpZhtml_contextZdoctreesNT)Zstatusr    ZfreshenvZwarningiserrorZtagsz`It was not possible to generate rich text help for this object.</br>Please see it in plain text.rz<pre>z<pre class="literal-block">)ignore_errors)r   r   Zto_unicode_from_fsr   r   r3   r   codecsopenwriteclosegenerate_configurationr
   r   Zbuildr   r   r    existsreadshutilZrmtree)r   r:   ZbuildernameZsrcdirZdestdirZrst_namesuffixZoutput_namer,   charZdoc_fileZtemp_confdirZconfdirZconfoverridesZ
doctreedirZ
sphinx_appoutputr   r   r   	sphinxify   sV    





rP   c             C   s   t tdd}t t tdd}tt | d tt | d t||  t|t | d tt | dd	d tt | dd	d	d d
S )z
    Generates a Sphinx configuration in `directory`.

    Parameters
    ----------
    directory : str
        Base directory to use
    zspyder.utils.helpzconf.pyr   zlayout.htmlr   z__init__.pyrB   r%   emptyN)
r   r   r
   r   r2   makedirsrL   copyrF   rG   )Z	directoryZconfZlayoutr   r   r   rI      s    
rI   )r%   r%   r%   FFr%   )r<   )&__doc__rE   r2   os.pathpathr   rL   r9   Ztempfiler   Zxml.sax.saxutilsr   Zdocutils.utilsr   Zjinja2r   r   r7   Zsphinx.applicationr   Zspyder.config.baser   r	   r
   Zspyder.utilsr   r   r   r   r4   r6   r5   r   r    r$   r;   rP   rI   r   r   r   r   <module>   s>   		 
7
]