[pypy-svn] pypy documentation-cleanup: (cfbolz, lac): kill the graphviz and latex formula support, since sphinx provides it for us anyway.
cfbolz
commits-noreply at bitbucket.org
Tue Apr 26 18:15:21 CEST 2011
Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: documentation-cleanup
Changeset: r43638:44a3480dd2bc
Date: 2011-04-26 14:40 +0200
http://bitbucket.org/pypy/pypy/changeset/44a3480dd2bc/
Log: (cfbolz, lac): kill the graphviz and latex formula support, since
sphinx provides it for us anyway.
diff --git a/pypy/tool/rest/convert.py b/pypy/tool/rest/convert.py
deleted file mode 100644
--- a/pypy/tool/rest/convert.py
+++ /dev/null
@@ -1,163 +0,0 @@
-import py
-
-ExecutionFailed = py.process.cmdexec.Error
-# utility functions to convert between various formats
-
-format_to_dotargument = {"png": "png",
- "eps": "ps",
- "ps": "ps",
- "pdf": "ps",
- }
-
-def ps2eps(ps):
- # XXX write a pure python version
- if not py.path.local.sysfind("ps2epsi") and \
- not py.path.local.sysfind("ps2eps"):
- raise SystemExit("neither ps2eps nor ps2epsi found")
- try:
- eps = ps.new(ext=".eps")
- py.process.cmdexec('ps2epsi "%s" "%s"' % (ps, eps))
- except ExecutionFailed:
- py.process.cmdexec('ps2eps -l -f "%s"' % ps)
-
-def ps2pdf(ps, compat_level="1.2"):
- if not py.path.local.sysfind("gs"):
- raise SystemExit("ERROR: gs not found")
- pdf = ps.new(ext=".pdf")
- options = dict(OPTIONS="-dSAFER -dCompatibilityLevel=%s" % compat_level,
- infile=ps, outfile=pdf)
- cmd = ('gs %(OPTIONS)s -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite '
- '"-sOutputFile=%(outfile)s" %(OPTIONS)s -c .setpdfwrite '
- '-f "%(infile)s"') % options
- py.process.cmdexec(cmd)
- return pdf
-
-def eps2pdf(eps):
- # XXX write a pure python version
- if not py.path.local.sysfind("epstopdf"):
- raise SystemExit("ERROR: epstopdf not found")
- py.process.cmdexec('epstopdf "%s"' % eps)
-
-def dvi2eps(dvi, dest=None):
- if dest is None:
- dest = eps.new(ext=".eps")
- command = 'dvips -q -E -n 1 -D 600 -p 1 -o "%s" "%s"' % (dest, dvi)
- if not py.path.local.sysfind("dvips"):
- raise SystemExit("ERROR: dvips not found")
- py.process.cmdexec(command)
-
-def convert_dot(fn, new_extension):
- if not py.path.local.sysfind("dot"):
- raise SystemExit("ERROR: dot not found")
- result = fn.new(ext=new_extension)
- print result
- arg = "-T%s" % (format_to_dotargument[new_extension], )
- py.std.os.system('dot "%s" "%s" > "%s"' % (arg, fn, result))
- if new_extension == "eps":
- ps = result.new(ext="ps")
- result.move(ps)
- ps2eps(ps)
- ps.remove()
- elif new_extension == "pdf":
- # convert to eps file first, to get the bounding box right
- eps = result.new(ext="eps")
- ps = result.new(ext="ps")
- result.move(ps)
- ps2eps(ps)
- eps2pdf(eps)
- ps.remove()
- eps.remove()
- return result
-
-
-class latexformula2png(object):
- def __init__(self, formula, dest, temp=None):
- self.formula = formula
- try:
- import Image
- self.Image = Image
- self.scale = 2 # create a larger image
- self.upscale = 5 # create the image upscale times larger, then scale it down
- except ImportError:
- self.scale = 2
- self.upscale = 1
- self.Image = None
- self.output_format = ('pngmono', 'pnggray', 'pngalpha')[2]
- if temp is None:
- temp = py.test.ensuretemp("latexformula")
- self.temp = temp
- self.latex = self.temp.join('formula.tex')
- self.dvi = self.temp.join('formula.dvi')
- self.eps = self.temp.join('formula.eps')
- self.png = self.temp.join('formula.png')
- self.saveas(dest)
-
- def saveas(self, dest):
- self.gen_latex()
- self.gen_dvi()
- dvi2eps(self.dvi, self.eps)
- self.gen_png()
- self.scale_image()
- self.png.copy(dest)
-
- def gen_latex(self):
- self.latex.write ("""
- \\documentclass{article}
- \\pagestyle{empty}
- \\begin{document}
-
- %s
- \\pagebreak
-
- \\end{document}
- """ % (self.formula))
-
- def gen_dvi(self):
- origdir = py.path.local()
- self.temp.chdir()
- py.process.cmdexec('latex "%s"' % (self.latex))
- origdir.chdir()
-
- def gen_png(self):
- tempdir = py.path.local.mkdtemp()
-
- re_bbox = py.std.re.compile('%%BoundingBox:\s*(\d+) (\d+) (\d+) (\d+)')
- eps = self.eps.read()
- x1, y1, x2, y2 = [int(i) for i in re_bbox.search(eps).groups()]
- X = x2 - x1 + 2
- Y = y2 - y1 + 2
- mx = -x1
- my = -y1
- ps = self.temp.join('temp.ps')
- source = self.eps
- ps.write("""
- 1 1 1 setrgbcolor
- newpath
- -1 -1 moveto
- %(X)d -1 lineto
- %(X)d %(Y)d lineto
- -1 %(Y)d lineto
- closepath
- fill
- %(mx)d %(my)d translate
- 0 0 0 setrgbcolor
- (%(source)s) run
-
- """ % locals())
-
- sx = int((x2 - x1) * self.scale * self.upscale)
- sy = int((y2 - y1) * self.scale * self.upscale)
- res = 72 * self.scale * self.upscale
- command = ('gs -q -g%dx%d -r%dx%d -sDEVICE=%s -sOutputFile="%s" '
- '-dNOPAUSE -dBATCH "%s"') % (
- sx, sy, res, res, self.output_format, self.png, ps)
- py.process.cmdexec(command)
-
- def scale_image(self):
- if self.Image is None:
- return
- image = self.Image.open(str(self.png))
- image.resize((image.size[0] / self.upscale,
- image.size[1] / self.upscale),
- self.Image.ANTIALIAS).save(str(self.png))
-
diff --git a/pypy/tool/rest/directive.py b/pypy/tool/rest/directive.py
--- a/pypy/tool/rest/directive.py
+++ b/pypy/tool/rest/directive.py
@@ -1,108 +1,9 @@
-# XXX this file is messy since it tries to deal with several docutils versions
import py
-from pypy.tool.rest.convert import convert_dot, latexformula2png
-
import sys
import docutils
from docutils import nodes
-from docutils.parsers.rst import directives, states, roles
-from docutils.parsers.rst.directives import images
-
-if hasattr(images, "image"):
- directives_are_functions = True
-else:
- directives_are_functions = False
-
-try:
- from docutils.utils import unescape # docutils version > 0.3.5
-except ImportError:
- from docutils.parsers.rst.states import unescape # docutils 0.3.5
-
-if not directives_are_functions:
- ImageClass = images.Image
-
-else:
- class ImageClass(object):
- option_spec = images.image.options
- def run(self):
- return images.image(u'image',
- self.arguments,
- self.options,
- self.content,
- self.lineno,
- self.content_offset,
- self.block_text,
- self.state,
- self.state_machine)
-
-
-backend_to_image_format = {"html": "png", "latex": "pdf"}
-
-class GraphvizDirective(ImageClass):
- def convert(self, fn, path):
- path = py.path.local(path).dirpath()
- dot = path.join(fn)
- result = convert_dot(dot, backend_to_image_format[_backend])
- return result.relto(path)
-
- def run(self):
- newname = self.convert(self.arguments[0],
- self.state.document.settings._source)
- text = self.block_text.replace("graphviz", "image", 1)
- self.block_text = text.replace(self.arguments[0], newname, 1)
- self.name = u'image'
- self.arguments = [newname]
- return ImageClass.run(self)
-
- def old_interface(self):
- def f(name, arguments, options, content, lineno,
- content_offset, block_text, state, state_machine):
- for arg in "name arguments options content lineno " \
- "content_offset block_text state state_machine".split():
- setattr(self, arg, locals()[arg])
- return self.run()
- f.arguments = (1, 0, 1)
- f.options = self.option_spec
- return f
-
-
-_backend = None
-def set_backend_and_register_directives(backend):
- #XXX this is only used to work around the inflexibility of docutils:
- # a directive does not know the target format
- global _backend
- _backend = backend
- if not directives_are_functions:
- directives.register_directive("graphviz", GraphvizDirective)
- else:
- directives.register_directive("graphviz",
- GraphvizDirective().old_interface())
- roles.register_canonical_role("latexformula", latexformula_role)
-
-def latexformula_role(name, rawtext, text, lineno, inliner,
- options={}, content=[]):
- if _backend == 'latex':
- options['format'] = 'latex'
- return roles.raw_role(name, rawtext, text, lineno, inliner,
- options, content)
- else:
- # XXX: make the place of the image directory configurable
- sourcedir = py.path.local(inliner.document.settings._source).dirpath()
- imagedir = sourcedir.join("img")
- if not imagedir.check():
- imagedir.mkdir()
- # create halfway senseful imagename:
- # use hash of formula + alphanumeric characters of it
- # could
- imagename = "%s_%s.png" % (
- hash(text), "".join([c for c in text if c.isalnum()]))
- image = imagedir.join(imagename)
- latexformula2png(unescape(text, True), image)
- imagenode = nodes.image(image.relto(sourcedir), uri=image.relto(sourcedir))
- return [imagenode], []
-latexformula_role.content = True
-latexformula_role.options = {}
+from docutils.parsers.rst import roles
def register_linkrole(role_name, callback):
def source_role(name, rawtext, text, lineno, inliner, options={},
More information about the Pypy-commit
mailing list