21
21
from sphinx .errors import SphinxError
22
22
from sphinx .util .osutil import ensuredir , relative_uri
23
23
24
+ if t .TYPE_CHECKING :
25
+ from sphinx .application import Sphinx
26
+
27
+
24
28
try :
25
29
import aafigure
26
30
except ImportError :
32
36
DEFAULT_FORMATS = {"html" : "svg" , "latex" : "pdf" , "text" : None }
33
37
34
38
35
- def merge_dict (dst , src ):
39
+ def merge_dict (
40
+ dst : t .Dict [str , t .Optional [str ]], src : t .Dict [str , t .Optional [str ]]
41
+ ) -> t .Dict [str , t .Optional [str ]]:
36
42
for k , v in src .items ():
37
43
if k not in dst :
38
44
dst [k ] = v
39
45
return dst
40
46
41
47
42
- def get_basename (text , options , prefix = "aafig" ):
48
+ def get_basename (
49
+ text : str , options : t .Dict [str , str ], prefix : t .Optional [str ] = "aafig"
50
+ ) -> str :
43
51
options = options .copy ()
44
52
if "format" in options :
45
53
del options ["format" ]
@@ -52,7 +60,7 @@ class AafigError(SphinxError):
52
60
category = "aafig error"
53
61
54
62
55
- class AafigDirective (images .Image ):
63
+ class AafigDirective (images .Image ): # type:ignore
56
64
"""
57
65
Directive to insert an ASCII art figure to be rendered by aafigure.
58
66
"""
@@ -71,7 +79,7 @@ class AafigDirective(images.Image):
71
79
option_spec = images .Image .option_spec .copy ()
72
80
option_spec .update (own_option_spec )
73
81
74
- def run (self ):
82
+ def run (self ) -> t . List [ nodes . Node ] :
75
83
aafig_options = {}
76
84
own_options_keys = [self .own_option_spec .keys (), "scale" ]
77
85
for k , v in self .options .items ():
@@ -93,7 +101,7 @@ def run(self):
93
101
return [image_node ]
94
102
95
103
96
- def render_aafig_images (app , doctree ) :
104
+ def render_aafig_images (app : "Sphinx" , doctree : nodes . Node ) -> None :
97
105
format_map = app .builder .config .aafig_format
98
106
merge_dict (format_map , DEFAULT_FORMATS )
99
107
if aafigure is None :
@@ -144,7 +152,9 @@ def __init__(self, *args: object, **kwargs: object) -> None:
144
152
return super ().__init__ ("aafigure module not installed" , * args , ** kwargs )
145
153
146
154
147
- def render_aafigure (app , text , options ):
155
+ def render_aafigure (
156
+ app : "Sphinx" , text : str , options : t .Dict [str , str ]
157
+ ) -> t .Tuple [str , str , t .Optional [str ], t .Optional [str ]]:
148
158
"""
149
159
Render an ASCII art figure into the requested format output file.
150
160
"""
@@ -186,7 +196,7 @@ def render_aafigure(app, text, options):
186
196
finally :
187
197
if f is not None :
188
198
f .close ()
189
- return relfn , outfn , id , extra
199
+ return relfn , outfn , None , extra
190
200
except AafigError :
191
201
pass
192
202
@@ -204,10 +214,10 @@ def render_aafigure(app, text, options):
204
214
with open (metadata_fname , "w" ) as f :
205
215
f .write (extra )
206
216
207
- return relfn , outfn , id , extra
217
+ return relfn , outfn , None , extra
208
218
209
219
210
- def setup (app ) :
220
+ def setup (app : "Sphinx" ) -> None :
211
221
app .add_directive ("aafig" , AafigDirective )
212
222
app .connect ("doctree-read" , render_aafig_images )
213
223
app .add_config_value ("aafig_format" , DEFAULT_FORMATS , "html" )
0 commit comments