33import sys
44
55from docutils import nodes
6- from docutils .parsers .rst import directives
6+ from docutils .parsers .rst import Directive , directives
77import sphinx
88
99from matplotlib import rcParams
@@ -22,9 +22,6 @@ def fontset_choice(arg):
2222 return directives .choice (arg , ['cm' , 'stix' , 'stixsans' ])
2323
2424
25- options_spec = {'fontset' : fontset_choice }
26-
27-
2825def math_role (role , rawtext , text , lineno , inliner ,
2926 options = {}, content = []):
3027 i = rawtext .find ('`' )
@@ -33,11 +30,10 @@ def math_role(role, rawtext, text, lineno, inliner,
3330 node ['latex' ] = latex
3431 node ['fontset' ] = options .get ('fontset' , 'cm' )
3532 return [node ], []
33+ math_role .options = {'fontset' : fontset_choice }
3634
3735
38- math_role .options = options_spec
39-
40-
36+ @cbook .deprecated ("3.1" , alternative = "MathDirective" )
4137def math_directive (name , arguments , options , content , lineno ,
4238 content_offset , block_text , state , state_machine ):
4339 latex = '' .join (content )
@@ -47,6 +43,21 @@ def math_directive(name, arguments, options, content, lineno,
4743 return [node ]
4844
4945
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+
5061# This uses mathtext to render the expression
5162def latex2png (latex , filename , fontset = 'cm' ):
5263 latex = "$%s$" % latex
@@ -121,12 +132,10 @@ def depart_latex_math_latex(self, node):
121132 html = (visit_latex_math_html , depart_latex_math_html ),
122133 latex = (visit_latex_math_latex , depart_latex_math_latex ))
123134 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 )
126136 if sphinx .version_info < (1 , 8 ):
127137 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 )
130139
131140 metadata = {'parallel_read_safe' : True , 'parallel_write_safe' : True }
132141 return metadata
0 commit comments