TracMathPlugin / tracmath /

Diff from to


 """ TracMath - A trac plugin that renders latex formulas within a wiki page.
-import codecs
 import re
 import os
 import os.path
 from import IWikiSyntaxProvider
 from trac.mimeview.api import IHTMLPreviewRenderer
 from trac.web import IRequestHandler
+from import Chrome, ITemplateProvider
 from trac.util import escape
 from trac.util.text import to_unicode
-from trac.util.translation import _
+from trac.util.translation import _, deactivate, reactivate
 from trac import mimeview
 __author__ = 'Reza Lotun'
 __author_email__ = ''
-tex_preamble = r"""
 rePNG = re.compile(r'\.png$')
 reGARBAGE = [
 reLABEL = re.compile(r'\\label\{(.*?)\}')
 class TracMathPlugin(Component):
-    implements(IWikiMacroProvider, IHTMLPreviewRenderer, IRequestHandler, IWikiSyntaxProvider)
+    implements(IWikiMacroProvider, IHTMLPreviewRenderer, IRequestHandler, IWikiSyntaxProvider, ITemplateProvider)
     cache_dir_option = Option("tracmath", "cache_dir", "tmcache",
             """The directory that will be used to cache the generated images.
     use_dollars = BoolOption("tracmath", "use_dollars", False,
             """Should support for dollar wiki syntax be enabled.""")
+    def __init__(self, *args, **kwargs):
+        super(TracMathPlugin, self).__init__(*args, **kwargs)
+        self.template = Chrome(self.env).load_template("tracmath_template.tex", method="text")
+        self.template_digest = sha1(self.template.generate(content='').render(encoding='utf-8')).digest()
     # IWikiSyntaxProvider methods
     #   stolen from
             if m:
                 label =
-        key = sha1(content.encode('utf-8')).hexdigest()
+        key = sha1(content.encode('utf-8') + self.template_digest).hexdigest()
         imgname = key + '.png'
         imgpath = os.path.join(self.cache_dir, imgname)
             texname = key + '.tex'
             texpath = os.path.join(self.cache_dir, texname)
+            # Don't translate tex file
+            t = deactivate()
-                f =, encoding='utf-8', mode='w')
-                f.write(tex_preamble)
-                f.write(content)
-                f.write('\end{document}')
+                f = open(texpath, mode='w')
+                self.template.generate(content=content).render(encoding='utf-8', out=f)
             except Exception, e:
+                reactivate(t)
                 return self._show_err("Problem creating tex file: %s" % (e))
+            finally:
+                reactivate(t)
             args = [
                 tag.strong(_("TracMath macro processor has detected an error. "
                              "Please fix the problem before continuing.")),
                 msg, class_="system-message")
+    def get_templates_dirs(self):
+        from pkg_resources import resource_filename
+        return [resource_filename(__name__, 'templates')]
+    def get_htdocs_dirs(self):
+        return []
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.