Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/docutils/utils/math/tex2mathml_extern.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # :Id: $Id: tex2mathml_extern.py 8376 2019-08-27 19:49:29Z milde $ | |
5 # :Copyright: © 2015 Günter Milde. | |
6 # :License: Released under the terms of the `2-Clause BSD license`_, in short: | |
7 # | |
8 # Copying and distribution of this file, with or without modification, | |
9 # are permitted in any medium without royalty provided the copyright | |
10 # notice and this notice are preserved. | |
11 # This file is offered as-is, without any warranty. | |
12 # | |
13 # .. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause | |
14 | |
15 # Wrappers for TeX->MathML conversion by external tools | |
16 # ===================================================== | |
17 | |
18 from __future__ import print_function | |
19 import subprocess | |
20 | |
21 document_template = r"""\documentclass{article} | |
22 \usepackage{amsmath} | |
23 \begin{document} | |
24 %s | |
25 \end{document} | |
26 """ | |
27 | |
28 def latexml(math_code, reporter=None): | |
29 """Convert LaTeX math code to MathML with LaTeXML_ | |
30 | |
31 .. _LaTeXML: http://dlmf.nist.gov/LaTeXML/ | |
32 """ | |
33 p = subprocess.Popen(['latexml', | |
34 '-', # read from stdin | |
35 # '--preload=amsmath', | |
36 '--inputencoding=utf8', | |
37 ], | |
38 stdin=subprocess.PIPE, | |
39 stdout=subprocess.PIPE, | |
40 stderr=subprocess.PIPE, | |
41 close_fds=True) | |
42 p.stdin.write((document_template % math_code).encode('utf8')) | |
43 p.stdin.close() | |
44 latexml_code = p.stdout.read() | |
45 latexml_err = p.stderr.read().decode('utf8') | |
46 if reporter and (latexml_err.find('Error') >= 0 or not latexml_code): | |
47 reporter.error(latexml_err) | |
48 | |
49 post_p = subprocess.Popen(['latexmlpost', | |
50 '-', | |
51 '--nonumbersections', | |
52 '--format=xhtml', | |
53 # '--linelength=78', # experimental | |
54 '--' | |
55 ], | |
56 stdin=subprocess.PIPE, | |
57 stdout=subprocess.PIPE, | |
58 stderr=subprocess.PIPE, | |
59 close_fds=True) | |
60 post_p.stdin.write(latexml_code) | |
61 post_p.stdin.close() | |
62 result = post_p.stdout.read().decode('utf8') | |
63 post_p_err = post_p.stderr.read().decode('utf8') | |
64 if reporter and (post_p_err.find('Error') >= 0 or not result): | |
65 reporter.error(post_p_err) | |
66 | |
67 # extract MathML code: | |
68 start, end = result.find('<math'), result.find('</math>')+7 | |
69 result = result[start:end] | |
70 if 'class="ltx_ERROR' in result: | |
71 raise SyntaxError(result) | |
72 return result | |
73 | |
74 def ttm(math_code, reporter=None): | |
75 """Convert LaTeX math code to MathML with TtM_ | |
76 | |
77 .. _TtM: http://hutchinson.belmont.ma.us/tth/mml/ | |
78 """ | |
79 p = subprocess.Popen(['ttm', | |
80 # '-i', # italic font for equations. Default roman. | |
81 '-u', # unicode character encoding. (Default iso-8859-1). | |
82 '-r', # output raw MathML (no preamble or postlude) | |
83 ], | |
84 stdin=subprocess.PIPE, | |
85 stdout=subprocess.PIPE, | |
86 stderr=subprocess.PIPE, | |
87 close_fds=True) | |
88 p.stdin.write((document_template % math_code).encode('utf8')) | |
89 p.stdin.close() | |
90 result = p.stdout.read() | |
91 err = p.stderr.read().decode('utf8') | |
92 if err.find('**** Unknown') >= 0: | |
93 msg = '\n'.join([line for line in err.splitlines() | |
94 if line.startswith('****')]) | |
95 raise SyntaxError('\nMessage from external converter TtM:\n'+ msg) | |
96 if reporter and err.find('**** Error') >= 0 or not result: | |
97 reporter.error(err) | |
98 start, end = result.find('<math'), result.find('</math>')+7 | |
99 result = result[start:end] | |
100 return result | |
101 | |
102 def blahtexml(math_code, inline=True, reporter=None): | |
103 """Convert LaTeX math code to MathML with blahtexml_ | |
104 | |
105 .. _blahtexml: http://gva.noekeon.org/blahtexml/ | |
106 """ | |
107 options = ['--mathml', | |
108 '--indented', | |
109 '--spacing', 'moderate', | |
110 '--mathml-encoding', 'raw', | |
111 '--other-encoding', 'raw', | |
112 '--doctype-xhtml+mathml', | |
113 '--annotate-TeX', | |
114 ] | |
115 if inline: | |
116 mathmode_arg = '' | |
117 else: | |
118 mathmode_arg = 'mode="display"' | |
119 options.append('--displaymath') | |
120 | |
121 p = subprocess.Popen(['blahtexml']+options, | |
122 stdin=subprocess.PIPE, | |
123 stdout=subprocess.PIPE, | |
124 stderr=subprocess.PIPE, | |
125 close_fds=True) | |
126 p.stdin.write(math_code.encode('utf8')) | |
127 p.stdin.close() | |
128 result = p.stdout.read().decode('utf8') | |
129 err = p.stderr.read().decode('utf8') | |
130 | |
131 if result.find('<error>') >= 0: | |
132 raise SyntaxError('\nMessage from external converter blahtexml:\n' | |
133 +result[result.find('<message>')+9:result.find('</message>')]) | |
134 if reporter and (err.find('**** Error') >= 0 or not result): | |
135 reporter.error(err) | |
136 start, end = result.find('<markup>')+9, result.find('</markup>') | |
137 result = ('<math xmlns="http://www.w3.org/1998/Math/MathML"%s>\n' | |
138 '%s</math>\n') % (mathmode_arg, result[start:end]) | |
139 return result | |
140 | |
141 # self-test | |
142 | |
143 if __name__ == "__main__": | |
144 example = (u'\\frac{\\partial \\sin^2(\\alpha)}{\\partial \\vec r}' | |
145 u'\\varpi \\, \\text{Grüße}') | |
146 # print(latexml(example).encode('utf8')) | |
147 # print(ttm(example)) | |
148 print(blahtexml(example).encode('utf8')) |