8
8
import re
9
9
from collections .abc import Iterable
10
10
from os import path
11
- from typing import Any , cast
11
+ from pathlib import Path
12
+ from typing import TYPE_CHECKING , Any , cast
12
13
13
14
from docutils import nodes
14
- from docutils .nodes import Node
15
15
from sphinx import addnodes
16
- from sphinx .application import Sphinx
17
16
from sphinx .builders .html import StandaloneHTMLBuilder
18
17
from sphinx .environment .adapters .indexentries import IndexEntries
19
18
from sphinx .locale import get_translation
22
21
from sphinx .util .osutil import canon_path , make_filename
23
22
from sphinx .util .template import SphinxRenderer
24
23
24
+ if TYPE_CHECKING :
25
+ from docutils .nodes import Node
26
+ from sphinx .application import Sphinx
25
27
26
28
__version__ = '1.0.8'
27
29
__version_info__ = (1 , 0 , 8 )
@@ -78,7 +80,7 @@ def init(self) -> None:
78
80
self .link_suffix = '.html'
79
81
# self.config.html_style = 'traditional.css'
80
82
81
- def get_theme_config (self ) -> tuple [str , dict ]:
83
+ def get_theme_config (self ) -> tuple [str , dict [ str , str | int | bool ] ]:
82
84
return self .config .qthelp_theme , self .config .qthelp_theme_options
83
85
84
86
def handle_finish (self ) -> None :
@@ -97,55 +99,55 @@ def build_qhp(self, outdir: str | os.PathLike[str], outname: str) -> None:
97
99
98
100
sections = []
99
101
matcher = NodeMatcher (addnodes .compact_paragraph , toctree = True )
100
- for node in tocdoc .traverse (matcher ): # type: addnodes.compact_paragraph
102
+ for node in tocdoc .findall (matcher ):
101
103
sections .extend (self .write_toc (node ))
102
104
103
- for indexname , indexcls , content , collapse in self .domain_indices :
105
+ for indexname , indexcls , _content , _collapse in self .domain_indices :
104
106
item = section_template % {'title' : indexcls .localname ,
105
107
'ref' : indexname + self .out_suffix }
106
108
sections .append (' ' * 4 * 4 + item )
107
- sections = '\n ' .join (sections ) # type: ignore
109
+ sections = '\n ' .join (sections ) # type: ignore[assignment]
108
110
109
111
# keywords
110
112
keywords = []
111
113
index = IndexEntries (self .env ).create_index (self , group_entries = False )
112
- for (key , group ) in index :
113
- for title , (refs , subitems , key_ ) in group :
114
+ for (_group_key , group ) in index :
115
+ for title , (refs , subitems , _category_key ) in group :
114
116
keywords .extend (self .build_keywords (title , refs , subitems ))
115
- keywords = '\n ' .join (keywords ) # type: ignore
117
+ keywords = '\n ' .join (keywords ) # type: ignore[assignment]
116
118
117
119
# it seems that the "namespace" may not contain non-alphanumeric
118
120
# characters, and more than one successive dot, or leading/trailing
119
121
# dots, are also forbidden
120
122
if self .config .qthelp_namespace :
121
123
nspace = self .config .qthelp_namespace
122
124
else :
123
- nspace = 'org.sphinx.%s.%s' % ( outname , self .config .version )
125
+ nspace = f 'org.sphinx.{ outname } . { self .config .version } '
124
126
125
127
nspace = re .sub (r'[^a-zA-Z0-9.\-]' , '' , nspace )
126
128
nspace = re .sub (r'\.+' , '.' , nspace ).strip ('.' )
127
129
nspace = nspace .lower ()
128
130
129
131
# write the project file
130
- with open ( path . join ( outdir , outname + ' .qhp'), 'w' , encoding = 'utf-8' ) as f :
131
- body = render_file ( 'project.qhp' , outname = outname ,
132
- title = self .config .html_title , version = self . config . version ,
133
- project = self .config .project , namespace = nspace ,
134
- master_doc = self . config . master_doc ,
135
- sections = sections , keywords = keywords ,
136
- files = self . get_project_files (outdir ) )
137
- f . write (body )
132
+ body = render_file ( 'project .qhp', outname = outname ,
133
+ title = self . config . html_title , version = self . config . version ,
134
+ project = self .config .project , namespace = nspace ,
135
+ master_doc = self .config .master_doc ,
136
+ sections = sections , keywords = keywords ,
137
+ files = self . get_project_files ( outdir ))
138
+ filename = Path (outdir , f' { outname } .qhp' )
139
+ filename . write_text (body , encoding = 'utf-8' )
138
140
139
141
homepage = 'qthelp://' + posixpath .join (
140
142
nspace , 'doc' , self .get_target_uri (self .config .master_doc ))
141
- startpage = 'qthelp://' + posixpath .join (nspace , 'doc' , 'index%s' % self .link_suffix )
143
+ startpage = 'qthelp://' + posixpath .join (nspace , 'doc' , f 'index{ self .link_suffix } ' )
142
144
143
145
logger .info (__ ('writing collection project file...' ))
144
- with open ( path . join ( outdir , outname + ' .qhcp'), 'w' , encoding = 'utf-8' ) as f :
145
- body = render_file ( 'project.qhcp' , outname = outname ,
146
- title = self . config . html_short_title ,
147
- homepage = homepage , startpage = startpage )
148
- f . write (body )
146
+ body = render_file ( 'project .qhcp', outname = outname ,
147
+ title = self . config . html_short_title ,
148
+ homepage = homepage , startpage = startpage )
149
+ filename = Path ( outdir , f' { outname } .qhcp' )
150
+ filename . write_text (body , encoding = 'utf-8' )
149
151
150
152
def isdocnode (self , node : Node ) -> bool :
151
153
if not isinstance (node , nodes .list_item ):
@@ -156,9 +158,7 @@ def isdocnode(self, node: Node) -> bool:
156
158
return False
157
159
if not isinstance (node [0 ][0 ], nodes .reference ):
158
160
return False
159
- if not isinstance (node [1 ], nodes .bullet_list ):
160
- return False
161
- return True
161
+ return isinstance (node [1 ], nodes .bullet_list )
162
162
163
163
def write_toc (self , node : Node , indentlevel : int = 4 ) -> list [str ]:
164
164
parts : list [str ] = []
@@ -167,8 +167,7 @@ def write_toc(self, node: Node, indentlevel: int = 4) -> list[str]:
167
167
reference = cast (nodes .reference , compact_paragraph [0 ])
168
168
link = reference ['refuri' ]
169
169
title = html .escape (reference .astext ()).replace ('"' , '"' )
170
- item = '<section title="%(title)s" ref="%(ref)s">' % \
171
- {'title' : title , 'ref' : link }
170
+ item = f'<section title="{ title } " ref="{ link } ">'
172
171
parts .append (' ' * 4 * indentlevel + item )
173
172
174
173
bullet_list = cast (nodes .bullet_list , node [1 ])
@@ -185,10 +184,7 @@ def write_toc(self, node: Node, indentlevel: int = 4) -> list[str]:
185
184
item = section_template % {'title' : title , 'ref' : link }
186
185
item = ' ' * 4 * indentlevel + item
187
186
parts .append (item .encode ('ascii' , 'xmlcharrefreplace' ).decode ())
188
- elif isinstance (node , nodes .bullet_list ):
189
- for subnode in node :
190
- parts .extend (self .write_toc (subnode , indentlevel ))
191
- elif isinstance (node , addnodes .compact_paragraph ):
187
+ elif isinstance (node , (nodes .bullet_list , addnodes .compact_paragraph )):
192
188
for subnode in node :
193
189
parts .extend (self .write_toc (subnode , indentlevel ))
194
190
@@ -203,16 +199,16 @@ def keyword_item(self, name: str, ref: Any) -> str:
203
199
# descr = groupdict.get('descr')
204
200
if shortname .endswith ('()' ):
205
201
shortname = shortname [:- 2 ]
206
- id = html .escape ('%s.%s' % ( id , shortname ) , True )
202
+ id = html .escape (f' { id } . { shortname } ' , True )
207
203
else :
208
204
id = None
209
205
210
206
nameattr = html .escape (name , quote = True )
211
207
refattr = html .escape (ref [1 ], quote = True )
212
208
if id :
213
- item = ' ' * 12 + '<keyword name="%s " id="%s " ref="%s "/>' % ( nameattr , id , refattr )
209
+ item = ' ' * 12 + f '<keyword name="{ nameattr } " id="{ id } " ref="{ refattr } "/>'
214
210
else :
215
- item = ' ' * 12 + '<keyword name="%s " ref="%s "/>' % ( nameattr , refattr )
211
+ item = ' ' * 12 + f '<keyword name="{ nameattr } " ref="{ refattr } "/>'
216
212
item .encode ('ascii' , 'xmlcharrefreplace' )
217
213
return item
218
214
@@ -224,7 +220,7 @@ def build_keywords(self, title: str, refs: list[Any], subitems: Any) -> list[str
224
220
if len (refs ) == 1 :
225
221
keywords .append (self .keyword_item (title , refs [0 ]))
226
222
elif len (refs ) > 1 :
227
- for i , ref in enumerate (refs ): # XXX
223
+ for _i , ref in enumerate (refs ): # XXX # NoQA: FURB148
228
224
# item = (' '*12 +
229
225
# '<keyword name="%s [%d]" ref="%s"/>' % (
230
226
# title, i, ref))
@@ -242,7 +238,7 @@ def get_project_files(self, outdir: str | os.PathLike[str]) -> list[str]:
242
238
project_files = []
243
239
staticdir = path .join (outdir , '_static' )
244
240
imagesdir = path .join (outdir , self .imagedir )
245
- for root , dirs , files in os .walk (outdir ):
241
+ for root , _dirs , files in os .walk (outdir ):
246
242
resourcedir = root .startswith ((staticdir , imagesdir ))
247
243
for fn in sorted (files ):
248
244
if (resourcedir and not fn .endswith ('.js' )) or fn .endswith ('.html' ):
0 commit comments