ó
öÀ„\c           @   so  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z m Z d d l m Z d d l m Z d d l Z d d l m Z d d	 l m Z m Z d d
 l m Z d d l m Z m Z d d l m Z m Z m Z d d l  m! Z! m" Z" d d l# m$ Z$ e% rd d l& m' Z' m( Z( m) Z) m* Z* d d l+ m, Z, d d l- m. Z. d d l/ m0 Z0 d d l1 m2 Z2 n  e j3 e4 ƒ Z5 d e f d „  ƒ  YZ6 d e f d „  ƒ  YZ7 d* Z8 d Z9 d Z: d Z; e j< d ƒ Z= d „  Z> d „  Z? d  „  Z@ d! „  ZA d" „  ZB d# „  ZC d$ „  ZD d% „  ZE d& „  ZF d' „  ZG d( „  ZH d) „  ZI d S(+   sÑ   
    sphinx.ext.imgmath
    ~~~~~~~~~~~~~~~~~~

    Render math in HTML via dvipng or dvisvgm.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iÿÿÿÿN(   t   sha1(   t   path(   t   Popent   PIPE(   t   nodes(   t	   text_type(   t   SphinxError(   t   _t   __(   t   logging(   t   get_node_equation_numbert   wrap_displaymath(   t	   ensuredirt   ENOENTt   cd(   t   read_png_deptht   write_png_depth(   t   sys_encoding(   t   Anyt   Dictt   Listt   Tuple(   t   displaymath(   t   Sphinx(   t   Builder(   t   Configt   MathExtErrorc           B   s   e  Z d  Z d d d „ Z RS(   s   Math extension errorc         C   sZ   | r# | d | j  t d ƒ 7} n  | rF | d | j  t d ƒ 7} n  t j |  | ƒ d  S(   Ns
   
[stderr]
t   replaces
   
[stdout]
(   t   decodeR   R   t   __init__(   t   selft   msgt   stderrt   stdout(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyR   /   s
    N(   t   __name__t
   __module__t   categoryt   NoneR   (    (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyR   ,   s   t   InvokeErrorc           B   s   e  Z d  Z RS(   s   errors on invoking converters.(   R"   R#   t   __doc__(    (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyR&   8   s   t   pngt   svgsË   
\documentclass[12pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{anyfontsize}
\usepackage{bm}
\pagestyle{empty}
sA   
\begin{document}
\fontsize{%d}{%d}\selectfont %s
\end{document}
s|   
\usepackage[active]{preview}
\begin{document}
\begin{preview}
\fontsize{%s}{%s}\selectfont %s
\end{preview}
\end{document}
s   \[\d+ depth=(-?\d+)\]c         C   sj   | j  } t t | d ƒ ƒ } t | j } | j rO | t | | |  f 7} n | t | | |  f 7} | S(   s   Generate LaTeX macro.g333333ó?(   t   imgmath_font_sizet   intt   roundt   DOC_HEADt   imgmath_latex_preamblet   imgmath_use_previewt   DOC_BODY_PREVIEWt   DOC_BODY(   t   matht   configt   fontsizet   baselineskipt   latex(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   generate_latex_macro\   s    		c         C   s(   t  |  d ƒ s! t j ƒ  |  _ n  |  j S(   sî   Create temporary directory.

    use only one tempdir per build -- the use of a directory is cleaner
    than using temporary files, since we can clean up everything at once
    just removing the whole directory (see cleanup_tempdir)
    t   _imgmath_tempdir(   t   hasattrt   tempfilet   mkdtempR8   (   t   builder(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   ensure_tempdirk   s    c   
      C   s=  t  | ƒ } t j | d ƒ } t j | d d ƒ  } | j |  ƒ Wd QX| j j d g } | j | j j	 ƒ | j
 d ƒ t | ƒ n y t | d t d t ƒ} WnJ t k
 rð } | j t k rË ‚  n  t j t d ƒ | j j ƒ t ‚ n XWd QX| j ƒ  \ } }	 | j d	 k r-t d
 |	 | ƒ ‚ n  t j | d ƒ S(   s%   Compile LaTeX macros for math to DVI.s   math.text   ws   utf-8Ns   --interaction=nonstopmodeR!   R    sY   LaTeX command %r cannot be run (needed for math display), check the imgmath_latex settingi    s   latex exited with errors   math.dvi(   R=   R   t   joint   codecst   opent   writeR3   t   imgmath_latext   extendt   imgmath_latex_argst   appendR   R   R   t   OSErrort   errnoR   t   loggert   warningR   R&   t   communicatet
   returncodeR   (
   R6   R<   t   tempdirt   filenamet   ft   commandt   pt   errR!   R    (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   compile_mathy   s(    c         C   s±   y t  |  d t d t ƒ} WnN t k
 rl } | j t k rC ‚  n  t j t d ƒ | |  d | ƒ t ‚ n X| j	 ƒ  \ } } | j
 d k r§ t d | | | ƒ ‚ n  | | f S(   s*   Convert DVI file to specific image format.R!   R    sS   %s command %r cannot be run (needed for math display), check the imgmath_%s settingi    s   %s exited with error(   R   R   RG   RH   R   RI   RJ   R   R&   RK   RL   R   (   RP   t   nameRQ   RR   R!   R    (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   convert_dvi_to_image›   s    
c         C   s  t  | ƒ } t j | d ƒ } d } | j j d | d d d g } | j | j j ƒ | j j rq | j d ƒ n  | j |  ƒ t	 | | ƒ \ } } d	 } | j j rú xR | j ƒ  D]A }	 t j |	 ƒ }
 |
 r² t |
 j d ƒ ƒ } t | | ƒ Pq² q² Wn  | | f S(
   s   Convert DVI file to PNG image.s   math.pngt   dvipngs   -os   -Tt   tights   -z9s   --depthi   N(   R=   R   R?   R3   t   imgmath_dvipngRD   t   imgmath_dvipng_argsR/   RF   RU   R%   t
   splitlinest   depth_ret   matchR+   t   groupR   (   t   dvipathR<   RM   RN   RT   RP   R!   R    t   deptht   linet   matched(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   convert_dvi_to_png¯   s$    c         C   sp   t  | ƒ } t j | d ƒ } d } | j j d | g } | j | j j ƒ | j |  ƒ t | | ƒ | d f S(   s   Convert DVI file to SVG image.s   math.svgt   dvisvgms   -oN(
   R=   R   R?   R3   t   imgmath_dvisvgmRD   t   imgmath_dvisvgm_argsRF   RU   R%   (   R^   R<   RM   RN   RT   RP   (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   convert_dvi_to_svgÊ   s    c   
      C   sÍ  |  j  j j j ƒ  } | t k r0 t d ƒ ‚ n  t | |  j  j ƒ } d t | j d ƒ ƒ j	 ƒ  | f } t
 j |  j  j d | ƒ } t j |  j  j |  j  j d | ƒ } t j | ƒ rÎ t | ƒ } | | f St |  j  d ƒ sò t |  j  d ƒ rö d
 Sy t | |  j  ƒ } Wn t k
 r,t |  j  _ d SXyR | d k rWt | |  j  ƒ \ }	 } n' | d k r~t | |  j  ƒ \ }	 } n  Wn t k
 rŸt |  j  _ d SXt t j | ƒ ƒ t j |	 | ƒ | | f S(   s‹  Render the LaTeX math expression *math* using latex and dvipng or
    dvisvgm.

    Return the filename relative to the built document and the "depth",
    that is, the distance of image bottom and baseline in pixels, if the
    option to use preview_latex is switched on.

    Error handling may seem strange, but follows a pattern: if LaTeX or dvipng
    (dvisvgm) aren't available, only a warning is generated (since that enables
    people on machines without these programs to at least build the rest of the
    docs successfully).  If the programs are there, however, they may not fail
    since that indicates a problem in the math source.
    s2   imgmath_image_format must be either "png" or "svg"s   %s.%ss   utf-8R2   t   _imgmath_warned_latext    _imgmath_warned_image_translatorR(   R)   N(   NN(   NN(   NN(   R<   R3   t   imgmath_image_formatt   lowert   SUPPORT_FORMATR   R7   R    t   encodet	   hexdigestt	   posixpathR?   t   imgpathR   t   outdirt   imagedirt   isfileR   R9   R%   RS   R&   t   TrueRg   Rb   Rf   Rh   R   t   dirnamet   shutilt   move(
   R   R2   t   image_formatR6   RN   t   relfnt   outfnR_   R^   Ro   (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   render_mathÙ   s:    %$
c         C   sO   | r
 d  St  |  j d ƒ s  d  Sy t j |  j j ƒ Wn t k
 rJ n Xd  S(   NR8   (   R9   R<   Ru   t   rmtreet   _mathpng_tempdirt	   Exception(   t   appt   exc(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   cleanup_tempdir  s    c         C   s0   |  j  j j r, d |  j | j ƒ  ƒ j ƒ  Sd S(   Ns	    alt="%s"t    (   R<   R3   t   imgmath_add_tooltipsRl   t   astextt   strip(   R   t   node(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   get_tooltip  s    c      
   C   s9  y' t  |  d | j ƒ  d ƒ \ } } Wn t k
 rª } t | ƒ } t j | d d d d d g  d | j ƒ  ƒ} | j |  ƒ t j t	 d ƒ | j ƒ  | ƒ t j
 ‚ n X| d  k rã |  j j d	 |  j | j ƒ  ƒ j ƒ  ƒ nI d
 | t |  | ƒ } | d  k	 r| d | 7} n  |  j j | d ƒ t j
 ‚ d  S(   Nt   $t   typet   WARNINGt   leveli   t   backrefst   sources   display latex %r: %ss   <span class="math">%s</span>s   <img class="math" src="%s"s    style="vertical-align: %dpx"s   />(   Rz   Rƒ   R   R   R   t   system_messaget	   walkaboutRI   RJ   R   t   SkipNodeR%   t   bodyRF   Rl   R„   R†   (   R   R…   t   fnameR_   R   R   t   smt   c(    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   html_visit_math%  s"    ' c   	   
   C   sÀ  | d r | j  ƒ  } n t | j  ƒ  d  t ƒ } y t |  | ƒ \ } } Wn t k
 rÍ } t | ƒ } t j | d d d d d g  d | j  ƒ  ƒ} | j	 |  ƒ t
 j t d ƒ | j  ƒ  | ƒ t j ‚ n X|  j j |  j | d	 d
 d ƒƒ |  j j d ƒ | d rVt |  | ƒ } |  j j d | ƒ |  j | t d ƒ ƒ |  j j d ƒ n  | d  k rŽ|  j j d |  j | j  ƒ  ƒ j ƒ  ƒ n% |  j j d | t |  | ƒ d ƒ t j ‚ d  S(   Nt   nowrapRˆ   R‰   RŠ   i   R‹   RŒ   s   inline latex %r: %st   divt   CLASSR2   s   <p>t   numbers   <span class="eqno">(%s)s   Permalink to this equations   </span>s'   <span class="math">%s</span></p>
</div>s   <img src="%s"s   /></p>
</div>(   Rƒ   R   R%   t   FalseRz   R   R   R   R   RŽ   RI   RJ   R   R   R   RF   t   starttagR
   t   add_permalink_refR   Rl   R„   R†   (	   R   R…   R6   R‘   R_   R   R   R’   R˜   (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   html_visit_displaymath<  s2    
"
 c         C   s*  |  j  d t d  f t d  f ƒ |  j d d d ƒ |  j d d d ƒ |  j d d d ƒ |  j d	 d
 d ƒ |  j d t d ƒ |  j d d d d d d d g d ƒ |  j d d g d ƒ |  j d g  d ƒ |  j d d d ƒ |  j d t d ƒ |  j d d d ƒ |  j d t ƒ i t	 j
 d 6t d 6S(   Nt   imgmathRi   R(   t   htmlRX   RV   Rd   Rc   RC   R6   R/   RY   s   -gammas   1.5s   -Dt   110s   -bgt   TransparentRe   s
   --no-fontsRE   R.   R   R‚   R*   i   s   build-finishedt   versiont   parallel_read_safe(   t   add_html_math_rendererR”   R%   Rœ   t   add_config_valueR™   Rs   t   connectR€   t   sphinxt   __display_version__(   R~   (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   setup\  s$    			(   R(   R)   (J   R'   R@   Rn   t   reRu   R:   t   hashlibR    t   osR   t
   subprocessR   R   t   docutilsR   t   sixR   R¦   t   sphinx.errorsR   t   sphinx.localeR   R   t   sphinx.utilR	   t   sphinx.util.mathR
   R   t   sphinx.util.osutilR   R   R   t   sphinx.util.pngR   R   t   sphinx.util.pycompatR   R™   t   typingR   R   R   R   t   sphinx.addnodesR   t   sphinx.applicationR   t   sphinx.buildersR   t   sphinx.configR   t	   getLoggerR"   RI   R   R&   Rk   R-   R1   R0   t   compileR[   R7   R=   RS   RU   Rb   Rf   Rz   R€   R†   R”   Rœ   R¨   (    (    (    s1   lib/python2.7/site-packages/sphinx/ext/imgmath.pyt   <module>
   sX   "				"				9				 