3
3
import sys
4
4
5
5
from docutils import nodes
6
- from docutils .parsers .rst import directives
6
+ from docutils .parsers .rst import Directive , directives
7
7
import sphinx
8
8
9
9
from matplotlib import rcParams
@@ -22,9 +22,6 @@ def fontset_choice(arg):
22
22
return directives .choice (arg , ['cm' , 'stix' , 'stixsans' ])
23
23
24
24
25
- options_spec = {'fontset' : fontset_choice }
26
-
27
-
28
25
def math_role (role , rawtext , text , lineno , inliner ,
29
26
options = {}, content = []):
30
27
i = rawtext .find ('`' )
@@ -33,11 +30,10 @@ def math_role(role, rawtext, text, lineno, inliner,
33
30
node ['latex' ] = latex
34
31
node ['fontset' ] = options .get ('fontset' , 'cm' )
35
32
return [node ], []
33
+ math_role .options = {'fontset' : fontset_choice }
36
34
37
35
38
- math_role .options = options_spec
39
-
40
-
36
+ @cbook .deprecated ("3.1" , alternative = "MathDirective" )
41
37
def math_directive (name , arguments , options , content , lineno ,
42
38
content_offset , block_text , state , state_machine ):
43
39
latex = '' .join (content )
@@ -47,6 +43,21 @@ def math_directive(name, arguments, options, content, lineno,
47
43
return [node ]
48
44
49
45
46
+ class MathDirective (Directive ):
47
+ has_content = True
48
+ required_arguments = 0
49
+ optional_arguments = 0
50
+ final_argument_whitespace = False
51
+ option_spec = {'fontset' : fontset_choice }
52
+
53
+ def run (self ):
54
+ latex = '' .join (self .content )
55
+ node = latex_math (self .block_text )
56
+ node ['latex' ] = latex
57
+ node ['fontset' ] = self .options .get ('fontset' , 'cm' )
58
+ return [node ]
59
+
60
+
50
61
# This uses mathtext to render the expression
51
62
def latex2png (latex , filename , fontset = 'cm' ):
52
63
latex = "$%s$" % latex
@@ -121,12 +132,10 @@ def depart_latex_math_latex(self, node):
121
132
html = (visit_latex_math_html , depart_latex_math_html ),
122
133
latex = (visit_latex_math_latex , depart_latex_math_latex ))
123
134
app .add_role ('mathmpl' , math_role )
124
- app .add_directive ('mathmpl' , math_directive ,
125
- True , (0 , 0 , 0 ), ** options_spec )
135
+ app .add_directive ('mathmpl' , MathDirective )
126
136
if sphinx .version_info < (1 , 8 ):
127
137
app .add_role ('math' , math_role )
128
- app .add_directive ('math' , math_directive ,
129
- True , (0 , 0 , 0 ), ** options_spec )
138
+ app .add_directive ('math' , MathDirective )
130
139
131
140
metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True }
132
141
return metadata
0 commit comments