data/plugin/parser/text_x_mathtran.py
author Reimar Bauer <rb.proj AT googlemail DOT com>
Fri, 07 May 2010 20:30:12 +0200
changeset 525 766d8ba45f84
parent 360 f57de7994cf6
permissions -rw-r--r--
macro.Image: return only for html formatter div container
     1 # -*- coding: iso-8859-1 -*-
     2 """
     3     MoinMoin - mathtran
     4 
     5     This parser is used to visualize latex formulars using a mathtran server, e.g.
     6     http://www.mathtran.org
     7 
     8     Many thanks to Jonathan Fine for our discussions at the EuroPython Conference during
     9     the MoinMoin sprint session in Vilnius
    10 
    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>"
    13 
    14     @copyright: 2008-2009 by MoinMoin:ReimarBauer
    15     @license: GNU GPL, see COPYING for details.
    16 """
    17 import urllib
    18 from MoinMoin import caching, config, wikiutil
    19 from MoinMoin.action import AttachFile, cache
    20 
    21 try:
    22     import urllib
    23 except ImportError:
    24     urllib = None
    25 
    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
    31     """
    32     return locals()
    33 
    34 Dependencies = ["page"]
    35 
    36 class Parser:
    37     """ mathtran parser """
    38     extensions = '*.tex'
    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
    44         self.form = None
    45         self._ = request.getText
    46 
    47         args = kw.get('format_args', '')
    48         self.init_settings = False
    49         try:
    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))
    58 
    59     def render(self, formatter):
    60         """ renders formular  """
    61 
    62         _ = self._
    63 
    64         # checks if initializing of all attributes in __init__ was done
    65         if not self.init_settings:
    66             return
    67 
    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,
    72                                                                                 }
    73         key = cache.key(self.request, itemname=self.pagename, content=text)
    74         if not cache.exists(self.request, key) and urllib:
    75             # ToDo add a timeout
    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)
    85 
    86     def format(self, formatter):
    87         """ parser output """
    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))
    93