[mod_python] mpservlets, hypertext and mathml

Daniel A Graham daniel.graham at duke.edu
Wed Oct 26 18:08:28 EDT 2005


Greetings,

I'm a big fan of mpservlets and have recently been involved in an effort
to incorporate mathml support into HyperText via itex2mml.  To do this I
added the following to _HTML.py variant of HyperText used in the
mpservlets tutorial:

---------------------------------------
# expand_codes taken from mpservlets _TutorialBase.py with one modification
def expand_codes(s):

    '''Expand codes in string to HTML elements.

    For any text of the form X{text}, replace it with
    <ELEMENT>text</ELEMENT>, where X and related elements are::

      C  <CODE>...</CODE>
      E  <EM>...</EM>
      S  <STRONG>...</STRONG>

    or A{TEXT<URL>} will generate: <A HREF="URL">TEXT</A>

    Inspired by epytext.  This simple version, however, does not
    allow embedding codes within each other.  :-(
    '''

    import re

    # so we can pass in non-string objects
    s = str(s)

    # iterate over our codes
    for code, elem in (('C', 'code'),
                       ('E', 'em'),
                       ('S', 'strong')):

        s = re.sub(r'%s{(.*?)}' % code,
                   r'<%s>\1</%s>' % (elem, elem),
                   s)

    s = re.sub(r'A{(.*?)<(.*?)>}',
               r'<a href="\2">\1</a>',
               s)

    # new support for inline math
    mre = re.compile(r'M{(.*)}')
    s = mre.sub(inline_math, s)

    return s

# called above to replace the itex math string with mathml
def inline_math(reo):
    from os import popen2
    s = reo.group(1)
    cmd = 'itex2mml --inline'
    (si, so) = popen2(cmd)
    si.write(s)
    si.close()
    s = so.read()
    so.close()
    return s

# used for display mathml
def M(s):
    from os import popen2
    cmd = 'itex2mml --display'
    (si, so) = popen2(cmd)
    si.write(s)
    si.close()
    s = so.read()
    so.close()
    return s
--------------------------------------------------

Now both
    expand_codes(P('''Here is some in-line math M{(x-y)^2}''')
and
    M('''F(b)-F(a) \equiv \int_a^b f(x) dx''')
work as expected.  Too cool!

One unexpected side effect (to me at least) is that the doctype needed
for mathml:

    DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0

causes all the upper case tags generated by HyperText to be ignored. 
This too, was easily fixed by adding '.lower()' to the occurances of
'self.name'.  This means that P('...') will now generate '<p>...</p>'.

Best wishes,
Dan Graham
daniel.graham at duke.edu



More information about the Mod_python mailing list