1 # -*- coding: iso-8859-1 -*-
5 This parser is used to visualize latex formulars using a mathtran server, e.g.
6 http://www.mathtran.org
8 Many thanks to Jonathan Fine for our discussions at the EuroPython Conference during
9 the MoinMoin sprint session in Vilnius
11 Please honor this work by adding a credits entry to the mathtran project on your wiki.
12 "<a href="http://www.mathtran.org/" title="MoinMoin uses the Mathtran Online translation of mathematical content software.">MathTran Powered</a>"
15 @license: GNU GPL, see COPYING for details.
18 from MoinMoin import caching, config, wikiutil
19 from MoinMoin.action import AttachFile, cache
26 def mathtran_settings(formular=u'', scale_factor=(u"3", u"1", u"2", u"4", u"5"),
27 mathtran_server=u"http://www.mathtran.org/cgi-bin/toy/"):
28 """ dummy function to initialize all default parameters for mathtran. The parameters are checked for wrong input.
29 @param formular: latex formular
30 @param scale_factor: scale factor for result image of latex formular
34 Dependencies = ["page"]
37 """ mathtran parser """
39 def __init__(self, raw, request, **kw):
40 self.pagename = request.page.page_name
41 self.raw = raw.strip('\n')
42 self.request = request
43 self.formatter = request.formatter
45 self._ = request.getText
47 args = kw.get('format_args', '')
48 self.init_settings = False
50 settings = wikiutil.invoke_extension_function(request, mathtran_settings, args)
51 for key, value in settings.items():
52 setattr(self, key, value)
53 # saves the state of valid input
54 self.init_settings = True
55 except ValueError, err:
56 msg = u"mathtran: %s" % err.args[0]
57 request.write(self.formatter.text(msg))
59 def render(self, formatter):
60 """ renders formular """
64 # checks if initializing of all attributes in __init__ was done
65 if not self.init_settings:
68 text = """%(mathtran_server)s?;D=%(scale_factor)s;tex=%(formular)s""" % {
69 "mathtran_server": self.mathtran_server,
70 "formular": wikiutil.url_quote(self.raw),
71 "scale_factor": self.scale_factor,
73 key = cache.key(self.request, itemname=self.pagename, content=text)
74 if not cache.exists(self.request, key) and urllib:
76 image = urllib.urlopen(text)
77 cache.put(self.request, key, image.read(),
78 content_type="image/png")
79 if cache.exists(self.request, key):
80 credits = """<a href="http://www.mathtran.org/" title="MoinMoin uses the Mathtran Online translation of mathematical content software.">MathTran Powered</a>"""
81 if not credits in self.request.cfg.page_credits:
82 self.request.cfg.page_credits.append(credits)
83 # ToDo show the credits only on pages using the parser extension
84 return formatter.image(src=cache.url(self.request, key), alt=self.raw)
86 def format(self, formatter):
88 # checks if initializing of all attributes in __init__ was done
89 if self.init_settings:
90 self.request.write(self.formatter.div(1, css_class="mathtran"))
91 self.request.write(self.render(formatter))
92 self.request.write(self.formatter.div(0))