9
9
:license: BSD, see LICENSE for details.
10
10
"""
11
11
12
+ from __future__ import annotations
13
+
12
14
from os import path
13
15
import posixpath
14
16
import shutil
17
+ from typing import TYPE_CHECKING , Any
15
18
16
19
from docutils .io import StringOutput
17
20
24
27
from sphinxcontrib .websupport .utils import is_commentable
25
28
26
29
27
- if False :
28
- # For type annotation
29
- from typing import Any , Dict , Iterable , Tuple # NOQA
30
- from docutils import nodes # NOQA
31
- from sphinx .application import Sphinx # NOQA
30
+ if TYPE_CHECKING :
31
+ from collections . abc import Iterable
32
+
33
+ from docutils import nodes
34
+ from sphinx .application import Sphinx
32
35
33
36
RESOURCES = [
34
37
'ajax-loader.gif' ,
@@ -51,8 +54,7 @@ class WebSupportBuilder(PickleHTMLBuilder):
51
54
default_translator_class = WebSupportTranslator
52
55
versioning_compare = True # for commentable node's uuid stability.
53
56
54
- def init (self ):
55
- # type: () -> None
57
+ def init (self ) -> None :
56
58
PickleHTMLBuilder .init (self )
57
59
# templates are needed for this builder, but the serializing
58
60
# builder does not initialize them
@@ -67,20 +69,22 @@ def init(self):
67
69
def versioning_method (self ):
68
70
return is_commentable
69
71
70
- def set_webinfo (self , staticdir , virtual_staticdir , search , storage ):
71
- # type: (str, str, Any, str) -> None
72
+ def set_webinfo (self ,
73
+ staticdir : str ,
74
+ virtual_staticdir : str ,
75
+ search : Any ,
76
+ storage : str ,
77
+ ) -> None :
72
78
self .staticdir = staticdir
73
79
self .virtual_staticdir = virtual_staticdir
74
80
self .search = search
75
81
self .storage = storage
76
82
77
- def prepare_writing (self , docnames ):
78
- # type: (Iterable[str]) -> None
83
+ def prepare_writing (self , docnames : Iterable [str ]) -> None :
79
84
PickleHTMLBuilder .prepare_writing (self , docnames )
80
85
self .globalcontext ['no_search_suffix' ] = True
81
86
82
- def write_doc (self , docname , doctree ):
83
- # type: (str, nodes.document) -> None
87
+ def write_doc (self , docname : str , doctree : nodes .document ) -> None :
84
88
destination = StringOutput (encoding = 'utf-8' )
85
89
doctree .settings = self .docsettings
86
90
@@ -97,30 +101,31 @@ def write_doc(self, docname, doctree):
97
101
ctx = self .get_doc_context (docname , body , metatags )
98
102
self .handle_page (docname , ctx , event_arg = doctree )
99
103
100
- def write_doc_serialized (self , docname , doctree ):
101
- # type: (str, nodes.Node) -> None
104
+ def write_doc_serialized (self , docname : str , doctree : nodes .Node ) -> None :
102
105
self .imgpath = '/' + posixpath .join (self .virtual_staticdir , self .imagedir )
103
106
self .post_process_images (doctree )
104
107
title = self .env .longtitles .get (docname )
105
108
title = title and self .render_partial (title )['title' ] or ''
106
109
self .index_page (docname , doctree , title )
107
110
108
- def load_indexer (self , docnames ):
109
- # type: (Iterable[str]) -> None
111
+ def load_indexer (self , docnames : Iterable [str ]) -> None :
110
112
self .indexer = self .search # type: ignore
111
113
self .indexer .init_indexing (changed = docnames ) # type: ignore
112
114
113
- def _render_page (self , pagename , addctx , templatename , event_arg = None ):
114
- # type: (str, Dict, str, Any) -> Tuple[Dict, Dict]
115
+ def _render_page (self ,
116
+ pagename : str ,
117
+ addctx : dict ,
118
+ templatename : str ,
119
+ event_arg : Any = None ,
120
+ ) -> tuple [dict , dict ]:
115
121
# This is mostly copied from StandaloneHTMLBuilder. However, instead
116
122
# of rendering the template and saving the html, create a context
117
123
# dict and pickle it.
118
124
ctx = self .globalcontext .copy ()
119
125
ctx ['pagename' ] = pagename
120
126
121
- def pathto (otheruri , resource = False ,
122
- baseuri = self .get_target_uri (pagename )):
123
- # type: (str, bool, str) -> str
127
+ def pathto (otheruri : str , resource : bool = False ,
128
+ baseuri : str = self .get_target_uri (pagename )) -> str :
124
129
if resource and '://' in otheruri :
125
130
return otheruri
126
131
elif not resource :
@@ -156,9 +161,8 @@ def pathto(otheruri, resource=False,
156
161
157
162
return ctx , doc_ctx
158
163
159
- def handle_page (self , pagename , addctx , templatename = 'page.html' ,
160
- outfilename = None , event_arg = None ):
161
- # type: (str, Dict, str, str | None, Any) -> None
164
+ def handle_page (self , pagename : str , addctx : dict , templatename : str = 'page.html' ,
165
+ outfilename : str | None = None , event_arg : Any = None ) -> None :
162
166
ctx , doc_ctx = self ._render_page (pagename , addctx ,
163
167
templatename , event_arg )
164
168
@@ -176,8 +180,7 @@ def handle_page(self, pagename, addctx, templatename='page.html',
176
180
ensuredir (path .dirname (source_name ))
177
181
copyfile (self .env .doc2path (pagename ), source_name )
178
182
179
- def handle_finish (self ):
180
- # type: () -> None
183
+ def handle_finish (self ) -> None :
181
184
# get global values for css and script files
182
185
_ , doc_ctx = self ._render_page ('tmp' , {}, 'page.html' )
183
186
self .globalcontext ['css' ] = doc_ctx ['css' ]
@@ -196,8 +199,7 @@ def handle_finish(self):
196
199
shutil .move (src , dst )
197
200
self .copy_resources ()
198
201
199
- def copy_resources (self ):
200
- # type: () -> None
202
+ def copy_resources (self ) -> None :
201
203
# copy resource files to static dir
202
204
dst = path .join (self .staticdir , '_static' )
203
205
@@ -206,13 +208,11 @@ def copy_resources(self):
206
208
src = path .join (package_dir , 'files' , resource )
207
209
shutil .copy (src , dst )
208
210
209
- def dump_search_index (self ):
210
- # type: () -> None
211
+ def dump_search_index (self ) -> None :
211
212
self .indexer .finish_indexing () # type: ignore
212
213
213
214
214
- def setup (app ):
215
- # type: (Sphinx) -> Dict[str, Any]
215
+ def setup (app : Sphinx ) -> dict [str , Any ]:
216
216
app .add_builder (WebSupportBuilder )
217
217
218
218
return {
0 commit comments