Skip to content

Commit 2f005d6

Browse files
authored
Merge pull request #38 from MarelliF/master
Update build files and fix incompatibility with other extensions
2 parents 442369d + 653e6a2 commit 2f005d6

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools>=42.0.0"]
3+
build-backend = "setuptools.build_meta"

setup.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import pathlib
2+
import sys
3+
4+
sys.path.append(str(pathlib.Path(__file__).parent))
15
import setuptools
26

37
with open("README.rst", "r") as fh:
@@ -14,15 +18,13 @@
1418
description="Sphinx extension to let you write LaTeX math using $$",
1519
long_description=long_description,
1620
url="https://github.com/sympy/sphinx-math-dollar/",
17-
packages=setuptools.find_packages(include=['sphinx_math_dollar']),
21+
packages=setuptools.find_packages(include=["sphinx_math_dollar"]),
1822
classifiers=[
1923
"Programming Language :: Python :: 3",
2024
"License :: OSI Approved :: MIT License",
2125
"Operating System :: OS Independent",
2226
],
23-
python_requires= '>=3.6',
24-
install_requires=[
25-
'sphinx'
26-
],
27-
license='MIT',
27+
python_requires=">=3.6",
28+
install_requires=["sphinx"],
29+
license="MIT",
2830
)

sphinx_math_dollar/extension.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1+
import logging
12
import os
23
import sys
34

4-
from .math_dollar import split_dollars
5-
from . import __version__
6-
7-
from docutils.nodes import GenericNodeVisitor, Text, math, math_block, FixedTextElement, literal
5+
from docutils.nodes import (
6+
FixedTextElement,
7+
GenericNodeVisitor,
8+
SkipNode,
9+
Text,
10+
literal,
11+
math,
12+
math_block,
13+
)
814
from docutils.transforms import Transform
915

16+
from . import __version__
17+
from .math_dollar import split_dollars
18+
1019
NODE_BLACKLIST = node_blacklist = (FixedTextElement, literal, math)
1120

1221
DEBUG = bool(os.environ.get("MATH_DOLLAR_DEBUG", False))
1322

23+
1424
class MathDollarReplacer(GenericNodeVisitor):
1525
def default_visit(self, node):
1626
return node
1727

28+
def unknown_visit(self, node):
29+
logging.warning("sphinx-math-dollar: Skipping unknown node type %s", type(node))
30+
raise SkipNode
31+
1832
def visit_Text(self, node):
1933
parent = node.parent
2034
while parent:
2135
if isinstance(parent, node_blacklist):
22-
if DEBUG and any(i == 'math' for i, _ in split_dollars(str(node).replace('\x00', '\\'))):
23-
print("sphinx-math-dollar: Skipping", node, "(node_blacklist = %s)" % (node_blacklist,), file=sys.stderr)
36+
if DEBUG and any(
37+
i == "math"
38+
for i, _ in split_dollars(str(node).replace("\x00", "\\"))
39+
):
40+
print(
41+
"sphinx-math-dollar: Skipping",
42+
node,
43+
"(node_blacklist = %s)" % (node_blacklist,),
44+
file=sys.stderr,
45+
)
2446
return
2547
parent = parent.parent
2648
# See https://github.com/sympy/sphinx-math-dollar/issues/22
27-
data = split_dollars(str(node).replace('\x00', '\\'))
49+
data = split_dollars(str(node).replace("\x00", "\\"))
2850
nodes = []
2951
has_math = False
3052
for typ, text in data:
@@ -36,41 +58,45 @@ def visit_Text(self, node):
3658
elif typ == "display math":
3759
has_math = True
3860
new_node = math_block(text, Text(text))
39-
new_node.attributes.setdefault('nowrap', False)
40-
new_node.attributes.setdefault('number', None)
61+
new_node.attributes.setdefault("nowrap", False)
62+
new_node.attributes.setdefault("number", None)
4163
nodes.append(new_node)
4264
else:
4365
raise ValueError("Unrecognized type from split_dollars %r" % typ)
4466
if has_math:
4567
node.parent.replace(node, nodes)
4668

69+
4770
class TransformMath(Transform):
4871
# See http://docutils.sourceforge.net/docs/ref/transforms.html. We want it
4972
# to apply before things that change rawsource, since we have to use that
5073
# to get the version of the text with backslashes. I'm not sure which all
5174
# transforms are relevant here, other than SmartQuotes, so this may need
5275
# to be adjusted.
5376
default_priority = 500
77+
5478
def apply(self, **kwargs):
5579
self.document.walk(MathDollarReplacer(self.document))
5680

81+
5782
def config_inited(app, config):
5883
global node_blacklist, DEBUG
5984
node_blacklist = config.math_dollar_node_blacklist
6085
DEBUG = config.math_dollar_debug
6186

87+
6288
def setup(app):
6389
app.add_transform(TransformMath)
6490
# We can't force a rebuild here because it will always appear different
6591
# since the tuple contains classes
66-
app.add_config_value('math_dollar_node_blacklist', NODE_BLACKLIST, '')
67-
app.add_config_value('math_dollar_debug', DEBUG, '')
68-
app.add_config_value('parallel_read_safe', True, '')
92+
app.add_config_value("math_dollar_node_blacklist", NODE_BLACKLIST, "")
93+
app.add_config_value("math_dollar_debug", DEBUG, "")
94+
app.add_config_value("parallel_read_safe", True, "")
6995

70-
app.connect('config-inited', config_inited)
96+
app.connect("config-inited", config_inited)
7197

7298
return {
73-
'version': __version__,
74-
'parallel_read_safe': True,
75-
'parallel_write_safe': True,
76-
}
99+
"version": __version__,
100+
"parallel_read_safe": True,
101+
"parallel_write_safe": True,
102+
}

0 commit comments

Comments
 (0)