1+ import logging
12import os
23import 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+ )
814from docutils .transforms import Transform
915
16+ from . import __version__
17+ from .math_dollar import split_dollars
18+
1019NODE_BLACKLIST = node_blacklist = (FixedTextElement , literal , math )
1120
1221DEBUG = bool (os .environ .get ("MATH_DOLLAR_DEBUG" , False ))
1322
23+
1424class 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+
4770class 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+
5782def 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+
6288def 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