"""Module with some functions for MathML, like transforming MathML
content in MathML presentation.
To use this module, you will need lxml.
"""
from sympy.utilities.pkgdata import get_resource
from sympy.utilities.decorator import doctest_depends_on
__doctest_requires__ = {('apply_xsl', 'c2p'): ['lxml']}
def add_mathml_headers(s):
return """"
@doctest_depends_on(modules=('lxml',))
def apply_xsl(mml, xsl):
"""Apply a xsl to a MathML string
@param mml: a string with MathML code
@param xsl: a string representing a path to a xsl (xml stylesheet)
file. This file name is relative to the PYTHONPATH
>>> from sympy.utilities.mathml import apply_xsl
>>> xsl = 'mathml/data/simple_mmlctop.xsl'
>>> mml = ' a b '
>>> res = apply_xsl(mml,xsl)
>>> ''.join(res.splitlines())
' a + b'
"""
from lxml import etree
s = etree.XML(get_resource(xsl).read())
transform = etree.XSLT(s)
doc = etree.XML(mml)
result = transform(doc)
s = str(result)
return s
@doctest_depends_on(modules=('lxml',))
def c2p(mml, simple=False):
"""Transforms a document in MathML content (like the one that sympy produces)
in one document in MathML presentation, more suitable for printing, and more
widely accepted
>>> from sympy.utilities.mathml import c2p
>>> mml = ' 2 '
>>> c2p(mml,simple=True) != c2p(mml,simple=False)
True
"""
if not mml.startswith('