def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]: """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. """ image_format = self.builder.config.imgmath_image_format.lower() if image_format not in SUPPORT_FORMAT: raise MathExtError( 'imgmath_image_format must be either "png" or "svg"') latex = generate_latex_macro(image_format, math, self.builder.config, self.builder.confdir) filename = "%s.%s" % (sha1(latex.encode()).hexdigest(), image_format) relfn = posixpath.join(self.builder.imgpath, 'math', filename) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename) if path.isfile(outfn): if image_format == 'png': depth = read_png_depth(outfn) elif image_format == 'svg': depth = read_svg_depth(outfn) return relfn, depth # if latex or dvipng (dvisvgm) has failed once, don't bother to try again if hasattr(self.builder, '_imgmath_warned_latex') or \ hasattr(self.builder, '_imgmath_warned_image_translator'): return None, None # .tex -> .dvi try: dvipath = compile_math(latex, self.builder) except InvokeError: self.builder._imgmath_warned_latex = True # type: ignore return None, None # .dvi -> .png/.svg try: if image_format == 'png': imgpath, depth = convert_dvi_to_png(dvipath, self.builder) elif image_format == 'svg': imgpath, depth = convert_dvi_to_svg(dvipath, self.builder) except InvokeError: self.builder._imgmath_warned_image_translator = True # type: ignore return None, None # Move generated image on tempdir to build dir ensuredir(path.dirname(outfn)) shutil.move(imgpath, outfn) return relfn, depth
def render_math(self, math): # type: (nodes.NodeVisitor, unicode) -> Tuple[unicode, int] """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. """ image_format = self.builder.config.imgmath_image_format.lower() if image_format not in SUPPORT_FORMAT: raise MathExtError('imgmath_image_format must be either "png" or "svg"') latex = generate_latex_macro(math, self.builder.config) filename = "%s.%s" % (sha1(latex.encode('utf-8')).hexdigest(), image_format) relfn = posixpath.join(self.builder.imgpath, 'math', filename) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng (dvisvgm) has failed once, don't bother to try again if hasattr(self.builder, '_imgmath_warned_latex') or \ hasattr(self.builder, '_imgmath_warned_image_translator'): return None, None # .tex -> .dvi try: dvipath = compile_math(latex, self.builder) except InvokeError: self.builder._imgmath_warned_latex = True return None, None # .dvi -> .png/.svg try: if image_format == 'png': imgpath, depth = convert_dvi_to_png(dvipath, self.builder) elif image_format == 'svg': imgpath, depth = convert_dvi_to_svg(dvipath, self.builder) except InvokeError: self.builder._imgmath_warned_image_translator = True return None, None # Move generated image on tempdir to build dir ensuredir(path.dirname(outfn)) shutil.move(imgpath, outfn) return relfn, depth
def render_math(self, math): # type: (nodes.NodeVisitor, unicode) -> Tuple[unicode, int] """Render the LaTeX math expression *math* using latex and dvipng. 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 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. """ use_preview = self.builder.config.pngmath_use_preview latex = DOC_HEAD + self.builder.config.pngmath_latex_preamble latex += (use_preview and DOC_BODY_PREVIEW or DOC_BODY) % math shasum = "%s.png" % sha1(latex.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'math', shasum) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng has failed once, don't bother to try again if hasattr(self.builder, '_mathpng_warned_latex') or \ hasattr(self.builder, '_mathpng_warned_dvipng'): return None, None # 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) if not hasattr(self.builder, '_mathpng_tempdir'): tempdir = self.builder._mathpng_tempdir = tempfile.mkdtemp() else: tempdir = self.builder._mathpng_tempdir with codecs.open(path.join(tempdir, 'math.tex'), 'w', 'utf-8') as tf: # type: ignore tf.write(latex) # build latex command; old versions of latex don't have the # --output-directory option, so we have to manually chdir to the # temp dir to run it. ltx_args = [self.builder.config.pngmath_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.pngmath_latex_args) ltx_args.append('math.tex') with cd(tempdir): try: p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise logger.warning( 'LaTeX command %r cannot be run (needed for math ' 'display), check the pngmath_latex setting', self.builder.config.pngmath_latex) self.builder._mathpng_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('latex exited with error', stderr, stdout) ensuredir(path.dirname(outfn)) # use some standard dvipng arguments dvipng_args = [self.builder.config.pngmath_dvipng] dvipng_args += ['-o', outfn, '-T', 'tight', '-z9'] # add custom ones from config value dvipng_args.extend(self.builder.config.pngmath_dvipng_args) if use_preview: dvipng_args.append('--depth') # last, the input file name dvipng_args.append(path.join(tempdir, 'math.dvi')) try: p = Popen(dvipng_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise logger.warning( 'dvipng command %r cannot be run (needed for math ' 'display), check the pngmath_dvipng setting', self.builder.config.pngmath_dvipng) self.builder._mathpng_warned_dvipng = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('dvipng exited with error', stderr, stdout) depth = None if use_preview: for line in stdout.splitlines(): m = depth_re.match(line) if m: depth = int(m.group(1)) write_png_depth(outfn, depth) break return relfn, depth
def render_circuit(self, circuit): '''given the raw circuit code, produce the png file''' shasum = "%s.png" % sha(circuit.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'circuit', shasum) outfn = path.join(self.builder.outdir, '_images', 'circuit', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng has failed once, don't bother to try again if hasattr(self.builder, '_circuit_warned_latex') or \ hasattr(self.builder, '_circuit_warned_dvipng'): return None, None latex = (DOC_BODY) % circuit if not hasattr(self.builder, '_circuitpng_tempdir'): tempdir = self.builder._circuit_tempdir = LATEX_BUILD_DIR else: tempdir = self.builder._circuit_tempdir tf = codecs.open(path.join(tempdir, 'circuit.tex'), 'w', 'utf-8') tf.write(latex) tf.close() ltx_args = [self.builder.config.circuit_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.circuit_latex_args) ltx_args.append('circuit.tex') curdir = getcwd() chdir(tempdir) # process this latex script with circuit try: try: # first latex run p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( 'LaTeX command %r cannot be run (needed for circuit ' 'display), check the circuit_latex setting' % self.builder.config.circuit_latex) self.builder._circuit_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise CircuitExtError('latex exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) print 'first run of latex', ltx_args try: # first circuit run circuit_args = ['circuit', 'circuit'] p = Popen(circuit_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: raise self.builder, warn('Circuit command failed') self.builder._circuit_warned_latex = True return None, None
def render_math(self, math): """Render the LaTeX math expression *math* using latex and dvipng. 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 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. """ use_preview = self.builder.config.pngmath_use_preview shasum = "%s.png" % sha(math.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'math', shasum) outfn = path.join(self.builder.outdir, '_images', 'math', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng has failed once, don't bother to try again if hasattr(self.builder, '_mathpng_warned_latex') or \ hasattr(self.builder, '_mathpng_warned_dvipng'): return None, None latex = DOC_HEAD + self.builder.config.pngmath_latex_preamble latex += (use_preview and DOC_BODY_PREVIEW or DOC_BODY) % math # 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) if not hasattr(self.builder, '_mathpng_tempdir'): tempdir = self.builder._mathpng_tempdir = tempfile.mkdtemp() else: tempdir = self.builder._mathpng_tempdir tf = codecs.open(path.join(tempdir, 'math.tex'), 'w', 'utf-8') tf.write(latex) tf.close() # build latex command; old versions of latex don't have the # --output-directory option, so we have to manually chdir to the # temp dir to run it. ltx_args = [self.builder.config.pngmath_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.pngmath_latex_args) ltx_args.append('math.tex') curdir = getcwd() chdir(tempdir) try: try: p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn('LaTeX command %r cannot be run (needed for math ' 'display), check the pngmath_latex setting' % self.builder.config.pngmath_latex) self.builder._mathpng_warned_latex = True return None, None finally: chdir(curdir) stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('latex exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) ensuredir(path.dirname(outfn)) # use some standard dvipng arguments dvipng_args = [self.builder.config.pngmath_dvipng] dvipng_args += ['-o', outfn, '-T', 'tight', '-z9'] # add custom ones from config value dvipng_args.extend(self.builder.config.pngmath_dvipng_args) if use_preview: dvipng_args.append('--depth') # last, the input file name dvipng_args.append(path.join(tempdir, 'math.dvi')) try: p = Popen(dvipng_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn('dvipng command %r cannot be run (needed for math ' 'display), check the pngmath_dvipng setting' % self.builder.config.pngmath_dvipng) self.builder._mathpng_warned_dvipng = True return None, None
def render_math(self, math): """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. """ image_format = self.builder.config.imgmath_image_format if image_format not in ('png', 'svg'): raise MathExtError( 'imgmath_image_format must be either "png" or "svg"') font_size = self.builder.config.imgmath_font_size use_preview = self.builder.config.imgmath_use_preview latex = DOC_HEAD + self.builder.config.imgmath_latex_preamble latex += (use_preview and DOC_BODY_PREVIEW or DOC_BODY) % (font_size, int(round(font_size * 1.2)), math) shasum = "%s.%s" % (sha1(latex.encode('utf-8')).hexdigest(), image_format) relfn = posixpath.join(self.builder.imgpath, 'math', shasum) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng (dvisvgm) has failed once, don't bother to try again if hasattr(self.builder, '_imgmath_warned_latex') or \ hasattr(self.builder, '_imgmath_warned_image_translator'): return None, None # 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) if not hasattr(self.builder, '_imgmath_tempdir'): tempdir = self.builder._imgmath_tempdir = tempfile.mkdtemp() else: tempdir = self.builder._imgmath_tempdir tf = codecs.open(path.join(tempdir, 'math.tex'), 'w', 'utf-8') tf.write(latex) tf.close() # build latex command; old versions of latex don't have the # --output-directory option, so we have to manually chdir to the # temp dir to run it. ltx_args = [self.builder.config.imgmath_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.imgmath_latex_args) ltx_args.append('math.tex') with cd(tempdir): try: p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( 'LaTeX command %r cannot be run (needed for math ' 'display), check the imgmath_latex setting' % self.builder.config.imgmath_latex) self.builder._imgmath_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('latex exited with error', stderr, stdout) ensuredir(path.dirname(outfn)) if image_format == 'png': image_translator = 'dvipng' image_translator_executable = self.builder.config.imgmath_dvipng # use some standard dvipng arguments image_translator_args = [self.builder.config.imgmath_dvipng] image_translator_args += ['-o', outfn, '-T', 'tight', '-z9'] # add custom ones from config value image_translator_args.extend(self.builder.config.imgmath_dvipng_args) if use_preview: image_translator_args.append('--depth') elif image_format == 'svg': image_translator = 'dvisvgm' image_translator_executable = self.builder.config.imgmath_dvisvgm # use some standard dvisvgm arguments image_translator_args = [self.builder.config.imgmath_dvisvgm] image_translator_args += ['-o', outfn] # add custom ones from config value image_translator_args.extend(self.builder.config.imgmath_dvisvgm_args) else: raise MathExtError( 'imgmath_image_format must be either "png" or "svg"') # last, the input file name image_translator_args.append(path.join(tempdir, 'math.dvi')) try: p = Popen(image_translator_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( '%s command %r cannot be run (needed for math ' 'display), check the imgmath_%s setting' % (image_translator, image_translator_executable, image_translator)) self.builder._imgmath_warned_image_translator = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('%s exited with error' % image_translator, stderr, stdout) depth = None if use_preview and image_format == 'png': # depth is only useful for png for line in stdout.splitlines(): m = depth_re.match(line) if m: depth = int(m.group(1)) write_png_depth(outfn, depth) break return relfn, depth
def render_rail(self, rail): shasum = "%s.png" % sha(rail.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'rail', shasum) outfn = path.join(self.builder.outdir, '_images', 'rail', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng has failed once, don't bother to try again if hasattr(self.builder, '_rail_warned_latex') or \ hasattr(self.builder, '_rail_warned_dvipng'): return None, None latex = DOC_HEAD + self.builder.config.rail_latex_preamble latex += (DOC_BODY) % rail if not hasattr(self.builder, '_railpng_tempdir'): tempdir = self.builder._rail_tempdir = LATEX_BUILD_DIR else: tempdir = self.builder._rail_tempdir tf = codecs.open(path.join(tempdir, 'rail.tex'), 'w', 'utf-8') tf.write(latex) tf.close() ltx_args = [self.builder.config.rail_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.rail_latex_args) ltx_args.append('rail.tex') curdir = getcwd() chdir(tempdir) # process this latex script with rail try: try: # first latex run p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( 'LaTeX command %r cannot be run (needed for rail ' 'display), check the rail_latex setting' % self.builder.config.rail_latex) self.builder._rail_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('latex exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) print('first run of latex', ltx_args) try: # first rail run rail_args = ['rail', 'rail'] p = Popen(rail_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: raise self.builder, warn('Rail command failed') self.builder._rail_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('rail exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) print('first rail run:', rail_args) try: p = Popen(rail_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: raise self.builder, warn('Rail command failed') self.builder._rail_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('rail exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) print('second rail run:', rail_args) try: p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( 'LaTeX command %r cannot be run (needed for rail ' 'display), check the rail_latex setting' % self.builder.config.rail_latex) self.builder._railpng_warned_latex = True return None, None finally: chdir(curdir) print('second run of latex', ltx_args) stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('latex exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) ensuredir(path.dirname(outfn)) # Now, convert the image to a PNG file # use some standard dvipng arguments dvipng_args = [self.builder.config.rail_dvipng] dvipng_args += ['-o', outfn, '-T', 'tight', '-z9'] # add custom ones from config value dvipng_args.extend(self.builder.config.rail_dvipng_args) # last, the input file name dvipng_args.append(path.join(tempdir, 'rail.dvi')) try: p = Popen(dvipng_args, stdout=PIPE, stderr=PIPE) print('dvipng run', dvipng_args) except OSError as err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn( 'dvipngpng command %r cannot be run (needed for rail ' 'display), check the rail_dvipng setting' % self.builder.config.rail_dvipng) self.builder._rail_warned_dvipng = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('dvipng exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) depth = None return relfn, depth
def render_math(self, math): # type: (nodes.NodeVisitor, unicode) -> Tuple[unicode, int] """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. """ image_format = self.builder.config.imgmath_image_format if image_format not in ('png', 'svg'): raise MathExtError( 'imgmath_image_format must be either "png" or "svg"') font_size = self.builder.config.imgmath_font_size use_preview = self.builder.config.imgmath_use_preview latex = DOC_HEAD + self.builder.config.imgmath_latex_preamble latex += (use_preview and DOC_BODY_PREVIEW or DOC_BODY) % ( font_size, int(round(font_size * 1.2)), math) shasum = "%s.%s" % (sha1(latex.encode('utf-8')).hexdigest(), image_format) relfn = posixpath.join(self.builder.imgpath, 'math', shasum) outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng (dvisvgm) has failed once, don't bother to try again if hasattr(self.builder, '_imgmath_warned_latex') or \ hasattr(self.builder, '_imgmath_warned_image_translator'): return None, None # 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) if not hasattr(self.builder, '_imgmath_tempdir'): tempdir = self.builder._imgmath_tempdir = tempfile.mkdtemp() else: tempdir = self.builder._imgmath_tempdir with codecs.open(path.join(tempdir, 'math.tex'), 'w', 'utf-8') as tf: tf.write(latex) # build latex command; old versions of latex don't have the # --output-directory option, so we have to manually chdir to the # temp dir to run it. ltx_args = [self.builder.config.imgmath_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.imgmath_latex_args) ltx_args.append('math.tex') with cd(tempdir): try: p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise logger.warning('LaTeX command %r cannot be run (needed for math ' 'display), check the imgmath_latex setting', self.builder.config.imgmath_latex) self.builder._imgmath_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('latex exited with error', stderr, stdout) ensuredir(path.dirname(outfn)) if image_format == 'png': image_translator = 'dvipng' image_translator_executable = self.builder.config.imgmath_dvipng # use some standard dvipng arguments image_translator_args = [self.builder.config.imgmath_dvipng] image_translator_args += ['-o', outfn, '-T', 'tight', '-z9'] # add custom ones from config value image_translator_args.extend(self.builder.config.imgmath_dvipng_args) if use_preview: image_translator_args.append('--depth') elif image_format == 'svg': image_translator = 'dvisvgm' image_translator_executable = self.builder.config.imgmath_dvisvgm # use some standard dvisvgm arguments image_translator_args = [self.builder.config.imgmath_dvisvgm] image_translator_args += ['-o', outfn] # add custom ones from config value image_translator_args.extend(self.builder.config.imgmath_dvisvgm_args) else: raise MathExtError( 'imgmath_image_format must be either "png" or "svg"') # last, the input file name image_translator_args.append(path.join(tempdir, 'math.dvi')) try: p = Popen(image_translator_args, stdout=PIPE, stderr=PIPE) except OSError as err: if err.errno != ENOENT: # No such file or directory raise logger.warning('%s command %r cannot be run (needed for math ' 'display), check the imgmath_%s setting', image_translator, image_translator_executable, image_translator) self.builder._imgmath_warned_image_translator = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise MathExtError('%s exited with error' % image_translator, stderr, stdout) depth = None if use_preview and image_format == 'png': # depth is only useful for png for line in stdout.splitlines(): m = depth_re.match(line) if m: depth = int(m.group(1)) write_png_depth(outfn, depth) break return relfn, depth
def render_rail(self, rail): shasum = "%s.png" % sha(rail.encode('utf-8')).hexdigest() relfn = posixpath.join(self.builder.imgpath, 'rail', shasum) outfn = path.join(self.builder.outdir, '_images', 'rail', shasum) if path.isfile(outfn): depth = read_png_depth(outfn) return relfn, depth # if latex or dvipng has failed once, don't bother to try again if hasattr(self.builder, '_rail_warned_latex') or \ hasattr(self.builder, '_rail_warned_dvipng'): return None, None latex = DOC_HEAD + self.builder.config.rail_latex_preamble latex += (DOC_BODY) % rail if not hasattr(self.builder, '_railpng_tempdir'): tempdir = self.builder._rail_tempdir = LATEX_BUILD_DIR else: tempdir = self.builder._rail_tempdir tf = codecs.open(path.join(tempdir, 'rail.tex'), 'w', 'utf-8') tf.write(latex) tf.close() ltx_args = [self.builder.config.rail_latex, '--interaction=nonstopmode'] # add custom args from the config file ltx_args.extend(self.builder.config.rail_latex_args) ltx_args.append('rail.tex') curdir = getcwd() chdir(tempdir) # process this latex script with rail try: try: # first latex run p = Popen(ltx_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: # No such file or directory raise self.builder.warn('LaTeX command %r cannot be run (needed for rail ' 'display), check the rail_latex setting' % self.builder.config.rail_latex) self.builder._rail_warned_latex = True return None, None stdout, stderr = p.communicate() if p.returncode != 0: raise RailExtError('latex exited with error:\n[stderr]\n%s\n' '[stdout]\n%s' % (stderr, stdout)) print 'first run of latex',ltx_args try: # first rail run rail_args = ['rail','rail'] p = Popen(rail_args, stdout=PIPE, stderr=PIPE) except OSError, err: if err.errno != ENOENT: raise self.builder,warn('Rail command failed') self.builder._rail_warned_latex = True return None, None