|
From: <mi...@us...> - 2024-01-07 10:21:26
|
Revision: 9507
http://sourceforge.net/p/docutils/code/9507
Author: milde
Date: 2024-01-07 10:21:21 +0000 (Sun, 07 Jan 2024)
Log Message:
-----------
New function `frontend.validate_math_output()`.
Define/Use a dedicated validating function for the value of the "math_output"
configuration setting.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/frontend.py
trunk/docutils/docutils/writers/_html_base.py
trunk/docutils/test/test_settings.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2024-01-07 10:21:12 UTC (rev 9506)
+++ trunk/docutils/HISTORY.txt 2024-01-07 10:21:21 UTC (rev 9507)
@@ -34,6 +34,7 @@
- Allow `validate_*()` functions to be called with just the "value"
argument but keep the legacy interface for use with optparse.
+ - New function `frontend.validate_math_output()`.
* docutils/io.py
Modified: trunk/docutils/docutils/frontend.py
===================================================================
--- trunk/docutils/docutils/frontend.py 2024-01-07 10:21:12 UTC (rev 9506)
+++ trunk/docutils/docutils/frontend.py 2024-01-07 10:21:21 UTC (rev 9507)
@@ -252,6 +252,42 @@
return value
+def validate_math_output(setting, value=None, option_parser=None,
+ config_parser=None, config_section=None):
+ """Check "math-output" setting, return list with "format" and "options".
+
+ See also https://docutils.sourceforge.io/docs/user/config.html#math-output
+
+ Argument list for compatibility with "optparse" module.
+ All arguments except `value` are ignored.
+ If there is only one positional argument, it is interpreted as `value`.
+ """
+ if value is None:
+ value = setting
+
+ formats = ('html', 'latex', 'mathml', 'mathjax')
+ tex2mathml_converters = ('', 'latexml', 'ttm', 'blahtexml', 'pandoc')
+
+ if not value:
+ return []
+ values = value.split(maxsplit=1)
+ format = values[0].lower()
+ try:
+ options = values[1]
+ except IndexError:
+ options = ''
+ if format not in formats:
+ raise LookupError(f'Unknown math output format: "{value}",\n'
+ f' choose from {formats}.')
+ if format == 'mathml':
+ converter = options.lower()
+ if converter not in tex2mathml_converters:
+ raise LookupError(f'MathML converter "{options}" not supported,\n'
+ f' choose from {tex2mathml_converters}.')
+ options = converter
+ return [format, options]
+
+
def validate_url_trailing_slash(setting, value=None, option_parser=None,
config_parser=None, config_section=None):
# All arguments except `value` are ignored
Modified: trunk/docutils/docutils/writers/_html_base.py
===================================================================
--- trunk/docutils/docutils/writers/_html_base.py 2024-01-07 10:21:12 UTC (rev 9506)
+++ trunk/docutils/docutils/writers/_html_base.py 2024-01-07 10:21:21 UTC (rev 9507)
@@ -117,7 +117,8 @@
'or "LaTeX") and option(s). '
'(default: "HTML math.css")',
['--math-output'],
- {'default': 'HTML math.css'}),
+ {'default': 'HTML math.css',
+ 'validator': frontend.validate_math_output}),
('Prepend an XML declaration. ',
['--xml-declaration'],
{'default': False, 'action': 'store_true',
@@ -300,9 +301,12 @@
FutureWarning, stacklevel=8)
self.image_loading = getattr(settings,
'image_loading', _image_loading_default)
- self.math_output = settings.math_output.split()
- self.math_output_options = self.math_output[1:]
- self.math_output = self.math_output[0].lower()
+ # backwards compatibiltiy: validate/convert programatically set strings
+ if isinstance(self.settings.math_output, str):
+ self.settings.math_output = frontend.validate_math_output(
+ self.settings.math_output)
+ (self.math_output,
+ self.math_options) = self.settings.math_output
# set up "parts" (cf. docs/api/publisher.html#publish-parts-details)
#
@@ -1276,11 +1280,6 @@
def visit_math(self, node, math_env=''):
# Also called from `visit_math_block()` (with math_env != '').
- if self.math_output not in self.math_tags:
- self.document.reporter.error(
- f'math-output format "{self.math_output}" not supported '
- 'falling back to "latex"', base_node=node)
- self.math_output = 'latex'
# LaTeX container
wrappers = {
# math_mode: (inline, block)
@@ -1291,8 +1290,8 @@
}
wrapper = wrappers[self.math_output][math_env != '']
if (self.math_output == 'mathml'
- and (not self.math_output_options
- or self.math_output_options[0] == 'blahtexml')):
+ and (not self.math_options
+ or self.math_options == 'blahtexml')):
wrapper = None
# get and wrap content
math_code = node.astext().translate(unichar2tex.uni2tex_table)
@@ -1305,9 +1304,9 @@
if self.math_output in ('latex', 'mathjax'):
math_code = self.encode(math_code)
if self.math_output == 'mathjax' and not self.math_header:
- try:
- self.mathjax_url = self.math_output_options[0]
- except IndexError:
+ if self.math_options:
+ self.mathjax_url = self.math_options
+ else:
self.document.reporter.warning(
'No MathJax URL specified, using local fallback '
'(see config.html).', base_node=node)
@@ -1317,11 +1316,11 @@
self.mathjax_url += '?config=TeX-AMS_CHTML'
self.math_header = [self.mathjax_script % self.mathjax_url]
elif self.math_output == 'html':
- if self.math_output_options and not self.math_header:
+ if self.math_options and not self.math_header:
self.math_header = [self.stylesheet_call(
utils.find_file_in_dirs(s, self.settings.stylesheet_dirs),
adjust_path=True)
- for s in self.math_output_options[0].split(',')]
+ for s in self.math_options.split(',')]
# TODO: fix display mode in matrices and fractions
math2html.DocumentParameters.displaymode = (math_env != '')
math_code = math2html.math2html(math_code)
@@ -1329,7 +1328,7 @@
if 'XHTML 1' in self.doctype:
self.doctype = self.doctype_mathml
self.content_type = self.content_type_mathml
- converter = ' '.join(self.math_output_options).lower()
+ converter = self.math_options
try:
if converter == 'latexml':
math_code = tex2mathml_extern.latexml(
Modified: trunk/docutils/test/test_settings.py
===================================================================
--- trunk/docutils/test/test_settings.py 2024-01-07 10:21:12 UTC (rev 9506)
+++ trunk/docutils/test/test_settings.py 2024-01-07 10:21:21 UTC (rev 9507)
@@ -331,6 +331,29 @@
for v, result in tests:
self.assertEqual(frontend.validate_comma_separated_list(v), result)
+ def test_validate_math_output(self):
+ tests = (('', []),
+ ('LaTeX ', ['latex', '']),
+ ('MathML', ['mathml', '']),
+ ('MathML PanDoc', ['mathml', 'pandoc']),
+ ('HTML math.css, X.css', ['html', 'math.css, X.css']),
+ ('MathJax /MathJax.js', ['mathjax', '/MathJax.js']),
+ )
+ for v, result in tests:
+ self.assertEqual(frontend.validate_math_output(v), result)
+
+ def test_validate_math_output_errors(self):
+ tests = (('XML', 'Unknown math output format: "XML",\n'
+ " choose from ('html', 'latex', 'mathml', 'mathjax')."),
+ ('MathML blame', 'MathML converter "blame" not supported,\n'
+ " choose from ('', 'latexml', 'ttm', 'blahtexml', "
+ "'pandoc')."),
+ )
+ for value, message in tests:
+ with self.assertRaises(LookupError) as cm:
+ frontend.validate_math_output(value)
+ self.assertEqual(message, str(cm.exception))
+
def test_validate_url_trailing_slash(self):
tests = (('', './'),
(None, './'),
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|