1
1
from sphinx .ext .mathbase import math
2
- from sphinx .ext .mathbase import displaymath
3
2
from sphinx .ext .mathbase import MathDirective
4
- from sphinx .ext . mathjax import html_visit_math
5
- from sphinx .ext . mathjax import html_visit_displaymath
6
- from sphinx .util . texescape import tex_escape_map , tex_replace_map
7
- from docutils import nodes , utils
3
+ from sphinx .util . texescape import tex_replace_map
4
+ from sphinx .writers . html5 import HTML5Translator
5
+ from sphinx .writers . latex import LaTeXTranslator
6
+ from docutils import nodes
8
7
from docutils .parsers .rst .directives .misc import Replace
9
- from docutils .parsers .rst .roles import math_role
10
8
from six import text_type
11
9
import re
12
10
@@ -22,14 +20,6 @@ def html_transform_math_xref(node):
22
20
new_text = xref_re .sub (lambda m : html_hyperlink (m .group (1 ), m .group (2 )), node .astext ())
23
21
node .children [0 ] = nodes .Text (new_text )
24
22
25
- def ext_html_visit_math (self , node ):
26
- html_transform_math_xref (node )
27
- html_visit_math (self , node )
28
-
29
- def ext_html_visit_displaymath (self , node ):
30
- html_transform_math_xref (node )
31
- html_visit_displaymath (self , node )
32
-
33
23
# Mirrors sphinx/writers/latex
34
24
def latex_hyperlink (file , id ):
35
25
id = text_type (id ).translate (tex_replace_map ).\
@@ -41,42 +31,6 @@ def latex_transform_math_xref(node):
41
31
new_text = xref_re .sub (lambda m : latex_hyperlink (m .group (1 ), m .group (2 )), node .astext ())
42
32
node .children [0 ] = nodes .Text (new_text )
43
33
44
- # TODO: this is duplicated from sphinx.writers.latex.LaTeXTranslator, figure out
45
- # a better way to extend it so that we don't have to duplicate this code.
46
- def latex_visit_math (self , node ):
47
- if self .in_title :
48
- self .body .append (r'\protect\(%s\protect\)' % node .astext ())
49
- else :
50
- self .body .append (r'\(%s\)' % node .astext ())
51
- raise nodes .SkipNode
52
-
53
- def ext_latex_visit_math (self , node ):
54
- latex_transform_math_xref (node )
55
- latex_visit_math (self , node )
56
-
57
- # TODO: this is duplicated from sphinx.writers.latex.LaTeXTranslator, figure out
58
- # a better way to extend it so that we don't have to duplicate this code.
59
- def latex_visit_displaymath (self , node ):
60
- if node .get ('label' ):
61
- label = "equation:%s:%s" % (node ['docname' ], node ['label' ])
62
- else :
63
- label = None
64
-
65
- if node .get ('nowrap' ):
66
- if label :
67
- self .body .append (r'\label{%s}' % label )
68
- self .body .append (node .astext ())
69
- else :
70
- from sphinx .util .math import wrap_displaymath
71
- self .body .append (wrap_displaymath (node .astext (), label ,
72
- self .builder .config .math_number_all ))
73
- raise nodes .SkipNode
74
-
75
- def ext_latex_visit_displaymath (self , node ):
76
- latex_transform_math_xref (node )
77
- latex_visit_displaymath (self , node )
78
-
79
-
80
34
# Expand mathdef names in math roles and directives
81
35
82
36
def_re = re .compile ('\\ \\ [A-Za-z][0-9A-Za-z]*' , re .M )
@@ -110,7 +64,7 @@ def run(self):
110
64
self .content [i ] = replace_mathdefs (doc , s )
111
65
for i , s in enumerate (self .arguments ):
112
66
self .arguments [i ] = replace_mathdefs (doc , s )
113
- return super (ExtMathDirective , self ).run ()
67
+ return super ().run ()
114
68
115
69
class MathdefDirective (Replace ):
116
70
def run (self ):
@@ -130,20 +84,39 @@ def run(self):
130
84
doc .mathdefs [name ] = [arity , '' .join (self .content )]
131
85
self .content [0 ] = ':math:`' + self .content [0 ]
132
86
self .content [- 1 ] = self .content [- 1 ] + '`'
133
- return super (MathdefDirective , self ).run ()
134
-
87
+ return super ().run ()
88
+
89
+ class WebAssemblyHTML5Translator (HTML5Translator ):
90
+ """
91
+ Customize HTML5Translator.
92
+ Convert xref in math and math block nodes to hrefs.
93
+ """
94
+ def visit_math (self , node , math_env = '' ):
95
+ html_transform_math_xref (node )
96
+ super ().visit_math (node , math_env )
97
+
98
+ def visit_math_block (self , node , math_env = '' ):
99
+ html_transform_math_xref (node )
100
+ super ().visit_math_block (node , math_env )
101
+
102
+ class WebAssemblyLaTeXTranslator (LaTeXTranslator ):
103
+ """
104
+ Customize LaTeXTranslator.
105
+ Convert xref in math and math block nodes to hyperrefs.
106
+ """
107
+ def visit_math (self , node ):
108
+ latex_transform_math_xref (node )
109
+ super ().visit_math (node )
110
+
111
+ def visit_math_block (self , node ):
112
+ latex_transform_math_xref (node )
113
+ super ().visit_math_block (node )
135
114
136
115
# Setup
137
116
138
117
def setup (app ):
139
- app .add_node (math ,
140
- override = True ,
141
- html = (ext_html_visit_math , None ),
142
- latex = (ext_latex_visit_math , None ))
143
- app .add_node (displaymath ,
144
- override = True ,
145
- html = (ext_html_visit_displaymath , None ),
146
- latex = (ext_latex_visit_displaymath , None ))
118
+ app .set_translator ('html' , WebAssemblyHTML5Translator )
119
+ app .set_translator ('latex' , WebAssemblyLaTeXTranslator )
147
120
app .add_role ('math' , ext_math_role )
148
- app .add_directive ('math' , ExtMathDirective )
121
+ app .add_directive ('math' , ExtMathDirective , override = True )
149
122
app .add_directive ('mathdef' , MathdefDirective )
0 commit comments