[py-svn] r21149 - in py/dist/py/rest: . testing testing/data

cfbolz at codespeak.net cfbolz at codespeak.net
Wed Dec 14 00:50:50 CET 2005


Author: cfbolz
Date: Wed Dec 14 00:50:48 2005
New Revision: 21149

Added:
   py/dist/py/rest/testing/data/formula.txt
Modified:
   py/dist/py/rest/directive.py
   py/dist/py/rest/testing/test_convert.py
Log:
add a latexformula text role for rest. now :latexformula:`$x^2$` produces the
formula, in latex if the backend is latex and as an image if the backend is
html. Add testfile and remove accidentially checked line.


Modified: py/dist/py/rest/directive.py
==============================================================================
--- py/dist/py/rest/directive.py	(original)
+++ py/dist/py/rest/directive.py	Wed Dec 14 00:50:48 2005
@@ -1,12 +1,11 @@
 import py
 
-from py.__.rest.convert import convert_dot
+from py.__.rest.convert import convert_dot, latexformula2png
 
 import sys
 from docutils import nodes, utils
-from docutils.parsers.rst import directives, states
+from docutils.parsers.rst import directives, states, roles
 from docutils.parsers.rst.directives import images
-from docutils.nodes import whitespace_normalize_name
 
 backend_to_image_format = {"html": "png", "latex": "pdf"}
 
@@ -19,6 +18,7 @@
         self.backend = backend
         self.convert_to_format = backend_to_image_format[backend]
         directives.register_directive("graphviz", self.graphviz_directive)
+        roles.register_canonical_role("latexformula", self.latexformula_role)
 
     def convert(self, fn, path):
         path = py.path.local(path).dirpath()
@@ -43,3 +43,27 @@
                                   'target': directives.unchanged_required,
                                   'class': directives.class_option}
 
+    def latexformula_role(self, name, rawtext, text, lineno, inliner,
+                          options={}, content=[]):
+        if self.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(text, image)
+            imagenode = nodes.image(image.relto(sourcedir), uri=image.relto(sourcedir))
+            return [imagenode], []
+    latexformula_role.content = True
+    latexformula_role.options = {}
+

Added: py/dist/py/rest/testing/data/formula.txt
==============================================================================
--- (empty file)
+++ py/dist/py/rest/testing/data/formula.txt	Wed Dec 14 00:50:48 2005
@@ -0,0 +1,8 @@
+Euklids proof about the infinitude of primes
+============================================
+
+If there were only a finite amount of primes then there would be a largest
+prime :latexformula:`p`. However, the number :latexformula:`p! + 1` is not
+divisible by any number :latexformula:`1, ..., p`. Therefore, a prime dividing
+:latexformula:`p! + 1` has to be bigger than :latexformula:`p`. Therefore there
+is an infinite number of primes. 

Modified: py/dist/py/rest/testing/test_convert.py
==============================================================================
--- py/dist/py/rest/testing/test_convert.py	(original)
+++ py/dist/py/rest/testing/test_convert.py	Wed Dec 14 00:50:48 2005
@@ -33,5 +33,4 @@
     #does not crash
     latexformula2png(formula, png)
     assert png.check()
-    py.process.cmdexec("qiv %s" % png)
     png.remove()



More information about the pytest-commit mailing list