Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added unknwon_visit #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions sphinx_math_dollar/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -35,35 +56,39 @@ 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
# to get the version of the text with backslashes. I'm not sure which all
# 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)