X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/12b5230d8fdb3ad995e867fb5d58a69e8a627e68..1f8cf8e0244045ed2b2a8efcff5d6db967ac2b11:/librarian/embeds/mathml.py diff --git a/librarian/embeds/mathml.py b/librarian/embeds/mathml.py index dd78f05..564a9f4 100644 --- a/librarian/embeds/mathml.py +++ b/librarian/embeds/mathml.py @@ -1,5 +1,8 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals + from lxml import etree +import six from librarian import get_resource from . import TreeEmbed, create_embed, downgrades_to @@ -7,6 +10,17 @@ from . import TreeEmbed, create_embed, downgrades_to class MathML(TreeEmbed): @downgrades_to('application/x-latex') def to_latex(self): + """ + >>> print(MathML(etree.fromstring('a < b')).to_latex().data.strip()) + a < b + + >>> print(MathML(etree.fromstring('< & &lt; A')).to_latex().data.strip()) + < & < A + + """ xslt = etree.parse(get_resource('res/embeds/mathml/mathml2latex.xslt')) output = self.tree.xslt(xslt) - return create_embed('application/x-latex', data=unicode(output)) + text = six.text_type(output) + # Workaround for entities being preserved in output. But there should be a better way. + text = text.replace('<', '<').replace('&', '&') + return create_embed('application/x-latex', data=text)