ó
‹²,]c           @   s¦  d  Z  d d l Z d d l Z d d l j 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 j e d d ƒ Z e j e d ƒ Z e d d e j d d e d ƒ d d ƒZ e d d e j d d e ƒ d d ƒZ d „  Z  d „  Z! d „  Z" d d d e# e# d d „ Z$ d d „ Z% d „  Z& d S(   sÂ  
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
iÿÿÿÿN(   t   mkdtemp(   t   escape(   t   SystemMessage(   t   Environmentt   FileSystemLoader(   t   Sphinx(   t   _t   get_module_data_patht   get_module_source_path(   t   encodings   spyder.utils.helpt   statict   csst   jst   spydert   relpatht   utilst   helpt   mathjaxt	   attr_namet   MATHJAXPATHt
   JQUERYPATHc         C   s   d |  k p d |  k S(   s;   Returns whether a string contains Sphinx-style ReST markup.t   `s   ::(    (   t	   docstring(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyt   is_sphinx_markupB   s    c         C   sI   t  ƒ  } t t j t d ƒ ƒ | _ | j d ƒ } | j d t d |  ƒ S(   s-   Print a warning message on the rich text viewt	   templatess   warning.htmlt   css_patht   text(	   R   R   t   ospt   joint   CONFDIR_PATHt   loadert   get_templatet   rendert   CSS_PATH(   t   messaget   envt   warning(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyR$   H   s    	c         C   s[   t  ƒ  } t t j t d ƒ ƒ | _ | j d ƒ } | j d t d |  d | d | d | ƒ S(   s+   Print a usage message on the rich text viewR   s
   usage.htmlR   t   titlet   intro_messaget   tutorial_messaget   tutorial(	   R   R   R   R   R   R   R   R    R!   (   R%   R"   R'   R(   R#   t   usage(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyR)   P   s
    	t    c         C   s¬   | r* t  j d k r* | j d d ƒ } n  i | r9 d n d d 6|  d 6| d 6| d	 6| d
 6| d 6t d 6t d 6t d 6t d 6t j d k  r” d n d d 6t	 j
 d 6} | S(   sH  
    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
    t   nts   \t   /t   trueR*   t   math_ont   namet   argspect   notet   collapset   img_pathR   t   js_patht   jquery_patht   mathjax_paths   1.1t   right_sphinx_versiont   platform(   t   osR/   t   replaceR!   t   JS_PATHt   JQUERY_PATHt   MATHJAX_PATHt   sphinxt   __version__t   sysR8   (   R/   R0   R1   t   mathR2   R3   t   context(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyt   generate_contextY   s     t   htmlc         C   sƒ  t  ƒ  } t j | ƒ } t j | d ƒ } t j | d ƒ } | d k rQ d } n d } t j | d | ƒ } | d r– | d r– |  j d	 d
 ƒ }  n  t | d ƒ } x: d d d d d d g D]  }	 | j |	 d |	 d ƒ } q¿ W| | d <t j | d d d ƒ}
 |
 j	 |  ƒ |
 j
 ƒ  t } | rMt  ƒ  } t j | ƒ } t | ƒ n t j t d ƒ ƒ } i | d 6} t j | d ƒ } t | | | | | | d d$ d d$ d t d t d d$ ƒ} y | j d$ | g ƒ Wn$ t k
 r÷t d ƒ } t | ƒ SXt j | ƒ r:t j | d  d d ƒj ƒ  } | j d! d" ƒ } n t d ƒ } t | ƒ S| rlt j | d# t ƒn  t j | d# t ƒ| S(%   s  
    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`
    t   _builds   docstring.rstRD   s   .htmls   .txtR   R7   R.   s   \\s   \\\\R0   t   =t   ,t   (t   )t   *s   **s    <span class="argspec-highlight">s   </span>t   wR	   s   utf-8s   spyder.utils.helpt   html_contextt   doctreest   statusR$   t   freshenvt   warningiserrort   tagss`   It was not possible to generate rich text help for this object.</br>Please see it in plain text.t   rs   <pre>s   <pre class="literal-block">t   ignore_errorsN(   R    R	   t   to_unicode_from_fsR   R   R:   R   t   codecst   opent   writet   closet   Falset   generate_configurationR   R   t   Nonet   Truet   buildR   R   R$   t   existst   readt   shutilt   rmtree(   R   RB   t   buildernamet   srcdirt   destdirt   rst_namet   suffixt   output_nameR0   t   chart   doc_filet   temp_confdirt   confdirt   confoverridest
   doctreedirt
   sphinx_appt   output(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyt	   sphinxify‘   sV    			

	
c         C   sß   t  j t d ƒ d ƒ } t  j t  j t d ƒ d ƒ } t j t  j |  d ƒ ƒ t j t  j |  d ƒ ƒ t j | |  ƒ t j | t  j |  d ƒ ƒ t t  j |  d ƒ d ƒ j	 d ƒ t t  j |  d d	 ƒ d ƒ j	 d ƒ d
 S(   s‹   
    Generates a Sphinx configuration in `directory`.

    Parameters
    ----------
    directory : str
        Base directory to use
    s   spyder.utils.helps   conf.pyR   s   layout.htmlR
   s   __init__.pyRK   R*   t   emptyN(
   R   R   R   R   R9   t   makedirsR`   t   copyRV   RW   (   t	   directoryt   conft   layout(    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyRZ   î   s    	"('   t   __doc__RU   R9   t   os.patht   pathR   R`   R@   t   tempfileR    t   xml.sax.saxutilsR   t   docutils.utilsR   t   jinja2R   R   R>   t   sphinx.applicationR   t   spyder.config.baseR   R   R   t   spyder.utilsR	   R   R   R!   R;   R=   R<   R   R$   R)   RY   RC   Rp   RZ   (    (    (    s:   lib/python2.7/site-packages/spyder/utils/help/sphinxify.pyt   <module>   s>   										7]