1
+ import logging
1
2
import os
2
3
import sys
3
4
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
+ )
8
14
from docutils .transforms import Transform
9
15
16
+ from . import __version__
17
+ from .math_dollar import split_dollars
18
+
10
19
NODE_BLACKLIST = node_blacklist = (FixedTextElement , literal , math )
11
20
12
21
DEBUG = bool (os .environ .get ("MATH_DOLLAR_DEBUG" , False ))
13
22
23
+
14
24
class MathDollarReplacer (GenericNodeVisitor ):
15
25
def default_visit (self , node ):
16
26
return node
17
27
28
+ def unknown_visit (self , node ):
29
+ logging .warning ("sphinx-math-dollar: Skipping unknown node type %s" , type (node ))
30
+ raise SkipNode
31
+
18
32
def visit_Text (self , node ):
19
33
parent = node .parent
20
34
while parent :
21
35
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
+ )
24
46
return
25
47
parent = parent .parent
26
48
# 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 " , " \\ " ))
28
50
nodes = []
29
51
has_math = False
30
52
for typ , text in data :
@@ -36,41 +58,45 @@ def visit_Text(self, node):
36
58
elif typ == "display math" :
37
59
has_math = True
38
60
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 )
41
63
nodes .append (new_node )
42
64
else :
43
65
raise ValueError ("Unrecognized type from split_dollars %r" % typ )
44
66
if has_math :
45
67
node .parent .replace (node , nodes )
46
68
69
+
47
70
class TransformMath (Transform ):
48
71
# See http://docutils.sourceforge.net/docs/ref/transforms.html. We want it
49
72
# to apply before things that change rawsource, since we have to use that
50
73
# to get the version of the text with backslashes. I'm not sure which all
51
74
# transforms are relevant here, other than SmartQuotes, so this may need
52
75
# to be adjusted.
53
76
default_priority = 500
77
+
54
78
def apply (self , ** kwargs ):
55
79
self .document .walk (MathDollarReplacer (self .document ))
56
80
81
+
57
82
def config_inited (app , config ):
58
83
global node_blacklist , DEBUG
59
84
node_blacklist = config .math_dollar_node_blacklist
60
85
DEBUG = config .math_dollar_debug
61
86
87
+
62
88
def setup (app ):
63
89
app .add_transform (TransformMath )
64
90
# We can't force a rebuild here because it will always appear different
65
91
# 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 , "" )
69
95
70
- app .connect (' config-inited' , config_inited )
96
+ app .connect (" config-inited" , config_inited )
71
97
72
98
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