diff --git a/sphinx_math_dollar/extension.py b/sphinx_math_dollar/extension.py index 85e9705..1ea074e 100644 --- a/sphinx_math_dollar/extension.py +++ b/sphinx_math_dollar/extension.py @@ -3,27 +3,48 @@ from .math_dollar import split_dollars -from docutils.nodes import GenericNodeVisitor, Text, math, math_block, FixedTextElement, literal +from docutils.nodes import ( + GenericNodeVisitor, + Text, + math, + math_block, + FixedTextElement, + literal, +) from docutils.transforms import Transform NODE_BLACKLIST = node_blacklist = (FixedTextElement, literal, math) DEBUG = bool(os.environ.get("MATH_DOLLAR_DEBUG", False)) + class MathDollarReplacer(GenericNodeVisitor): def default_visit(self, node): return node + def unknown_visit(self, node): + """Pass node as-is.""" + # FIXES #34, without, NotImplemented could get raisesd for unknown nodes. + return node + def visit_Text(self, node): parent = node.parent while parent: if isinstance(parent, node_blacklist): - if DEBUG and any(i == 'math' for i, _ in split_dollars(str(node).replace('\x00', '\\'))): - print("sphinx-math-dollar: Skipping", node, "(node_blacklist = %s)" % (node_blacklist,), file=sys.stderr) + if DEBUG and any( + i == "math" + for i, _ in split_dollars(str(node).replace("\x00", "\\")) + ): + print( + "sphinx-math-dollar: Skipping", + node, + "(node_blacklist = %s)" % (node_blacklist,), + file=sys.stderr, + ) return parent = parent.parent # See https://github.com/sympy/sphinx-math-dollar/issues/22 - data = split_dollars(str(node).replace('\x00', '\\')) + data = split_dollars(str(node).replace("\x00", "\\")) nodes = [] has_math = False for typ, text in data: @@ -35,14 +56,15 @@ def visit_Text(self, node): elif typ == "display math": has_math = True new_node = math_block(text, Text(text)) - new_node.attributes.setdefault('nowrap', False) - new_node.attributes.setdefault('number', None) + new_node.attributes.setdefault("nowrap", False) + new_node.attributes.setdefault("number", None) nodes.append(new_node) else: raise ValueError("Unrecognized type from split_dollars %r" % typ) if has_math: node.parent.replace(node, nodes) + class TransformMath(Transform): # See http://docutils.sourceforge.net/docs/ref/transforms.html. We want it # to apply before things that change rawsource, since we have to use that @@ -50,20 +72,23 @@ class TransformMath(Transform): # transforms are relevant here, other than SmartQuotes, so this may need # to be adjusted. default_priority = 500 + def apply(self, **kwargs): self.document.walk(MathDollarReplacer(self.document)) + def config_inited(app, config): global node_blacklist, DEBUG node_blacklist = config.math_dollar_node_blacklist DEBUG = config.math_dollar_debug + def setup(app): app.add_transform(TransformMath) # We can't force a rebuild here because it will always appear different # since the tuple contains classes - app.add_config_value('math_dollar_node_blacklist', NODE_BLACKLIST, '') - app.add_config_value('math_dollar_debug', DEBUG, '') - app.add_config_value('parallel_read_safe', True, '') + app.add_config_value("math_dollar_node_blacklist", NODE_BLACKLIST, "") + app.add_config_value("math_dollar_debug", DEBUG, "") + app.add_config_value("parallel_read_safe", True, "") - app.connect('config-inited', config_inited) + app.connect("config-inited", config_inited)