44
55import html
66import os
7- from os import path
87import posixpath
98import shutil
9+ from os import path
1010from typing import TYPE_CHECKING , Any
1111
1212from docutils .io import StringOutput
13-
1413from sphinx .jinja2glue import BuiltinTemplateLoader
15- from sphinx .util .osutil import os_path , relative_uri , ensuredir , copyfile
16- from sphinxcontrib .serializinghtml import PickleHTMLBuilder
14+ from sphinx .util .osutil import copyfile , ensuredir , os_path , relative_uri
1715
16+ from sphinxcontrib .serializinghtml import PickleHTMLBuilder
1817from sphinxcontrib .websupport import package_dir
19- from sphinxcontrib .websupport .writer import WebSupportTranslator
2018from sphinxcontrib .websupport .utils import is_commentable
21-
19+ from sphinxcontrib . websupport . writer import WebSupportTranslator
2220
2321if TYPE_CHECKING :
24- from collections .abc import Iterable
22+ from collections .abc import Callable , Iterable
2523
2624 from docutils import nodes
2725 from sphinx .application import Sphinx
26+ from sphinx .builders .html ._assets import _CascadingStyleSheet , _JavaScript
27+
28+ from sphinxcontrib .websupport .search import BaseSearch
2829
2930RESOURCES = [
3031 'ajax-loader.gif' ,
@@ -48,34 +49,34 @@ class WebSupportBuilder(PickleHTMLBuilder):
4849 versioning_compare = True # for commentable node's uuid stability.
4950
5051 def init (self ) -> None :
51- PickleHTMLBuilder .init (self )
52+ super () .init ()
5253 # templates are needed for this builder, but the serializing
5354 # builder does not initialize them
5455 self .init_templates ()
5556 if not isinstance (self .templates , BuiltinTemplateLoader ):
56- raise RuntimeError ( 'websupport builder must be used with '
57- 'the builtin templates' )
57+ msg = 'websupport builder must be used with the builtin templates '
58+ raise RuntimeError ( msg )
5859 # add our custom JS
5960 self .add_js_file ('websupport.js' )
6061
6162 @property
62- def versioning_method (self ):
63+ def versioning_method (self ) -> Callable [[ nodes . Node ], bool ]: # type: ignore[override]
6364 return is_commentable
6465
6566 def set_webinfo (
6667 self ,
6768 staticdir : str ,
6869 virtual_staticdir : str ,
69- search : Any ,
70+ search : BaseSearch ,
7071 storage : str ,
7172 ) -> None :
7273 self .staticdir = staticdir
7374 self .virtual_staticdir = virtual_staticdir
74- self .search = search
75+ self .search : BaseSearch = search # type: ignore[assignment]
7576 self .storage = storage
7677
7778 def prepare_writing (self , docnames : Iterable [str ]) -> None :
78- PickleHTMLBuilder .prepare_writing (self , docnames )
79+ super () .prepare_writing (set ( docnames ) )
7980 self .globalcontext ['no_search_suffix' ] = True
8081
8182 def write_doc (self , docname : str , doctree : nodes .document ) -> None :
@@ -95,16 +96,16 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None:
9596 ctx = self .get_doc_context (docname , body , metatags )
9697 self .handle_page (docname , ctx , event_arg = doctree )
9798
98- def write_doc_serialized (self , docname : str , doctree : nodes .Node ) -> None :
99+ def write_doc_serialized (self , docname : str , doctree : nodes .document ) -> None :
99100 self .imgpath = '/' + posixpath .join (self .virtual_staticdir , self .imagedir )
100101 self .post_process_images (doctree )
101- title = self .env .longtitles .get (docname )
102- title = title and self .render_partial (title )['title' ] or ''
102+ title_node = self .env .longtitles .get (docname )
103+ title = title_node and self .render_partial (title_node )['title' ] or ''
103104 self .index_page (docname , doctree , title )
104105
105106 def load_indexer (self , docnames : Iterable [str ]) -> None :
106- self .indexer = self .search # type: ignore
107- self .indexer .init_indexing (changed = docnames ) # type: ignore
107+ self .indexer = self .search # type: ignore[assignment]
108+ self .indexer .init_indexing (changed = list ( docnames )) # type: ignore[union-attr]
108109
109110 def _render_page (
110111 self ,
@@ -135,7 +136,7 @@ def pathto(otheruri: str, resource: bool = False,
135136 self .add_sidebars (pagename , ctx )
136137 ctx .update (addctx )
137138
138- def css_tag (css ) -> str :
139+ def css_tag (css : _CascadingStyleSheet ) -> str :
139140 attrs = []
140141 for key , value in css .attributes .items ():
141142 if value is not None :
@@ -145,10 +146,10 @@ def css_tag(css) -> str:
145146
146147 ctx ['css_tag' ] = css_tag
147148
148- def js_tag (js ) -> str :
149+ def js_tag (js : _JavaScript ) -> str :
149150 if not hasattr (js , 'filename' ):
150151 # str value (old styled)
151- return f'<script src="{ pathto (js , resource = True )} "></script>'
152+ return f'<script src="{ pathto (js , resource = True )} "></script>' # type: ignore[arg-type]
152153
153154 attrs = []
154155 body = js .attributes .get ('body' , '' )
@@ -216,7 +217,7 @@ def handle_finish(self) -> None:
216217 self .globalcontext ['css' ] = doc_ctx ['css' ]
217218 self .globalcontext ['script' ] = doc_ctx ['script' ]
218219
219- PickleHTMLBuilder .handle_finish (self )
220+ super () .handle_finish ()
220221
221222 # move static stuff over to separate directory
222223 directories = [self .imagedir , '_static' ]
@@ -239,7 +240,7 @@ def copy_resources(self) -> None:
239240 shutil .copy (src , dst )
240241
241242 def dump_search_index (self ) -> None :
242- self .indexer .finish_indexing () # type: ignore
243+ self .indexer .finish_indexing () # type: ignore[union-attr]
243244
244245
245246def setup (app : Sphinx ) -> dict [str , Any ]:
0 commit comments