Drop lots of legacy code. Support Python 3.7-3.11.
[librarian.git] / src / librarian / embeds / mathml.py
1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from lxml import etree
5 from librarian import get_resource
6 from . import TreeEmbed, create_embed, downgrades_to
7
8
9 class MathML(TreeEmbed):
10     @downgrades_to('application/x-latex')
11     def to_latex(self):
12         """
13         >>> print(MathML(etree.fromstring(
14         ...     '<mat>a &lt; b</mat>'
15         ... )).to_latex().data.strip())
16         a < b
17
18         >>> print(MathML(etree.fromstring(
19         ...     '<mat>&lt; &amp; &amp;lt; &#65;</mat>'
20         ... )).to_latex().data.strip())
21         < & &lt; A
22
23         """
24         xslt = etree.parse(get_resource('res/embeds/mathml/mathml2latex.xslt'))
25         output = self.tree.xslt(xslt)
26         text = str(output)
27         # Workaround for entities being preserved in output.
28         # But there should be a better way.
29         text = text.replace('&lt;', '<').replace('&amp;', '&')
30         return create_embed('application/x-latex', data=text)