Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

docs: kernel_include.py: move code and literal functions

Simplify run() even more by moving the code which handles
with code and literal blocks to their own functions.

No functional changes.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/78d08dfa3f08adabc30bf93b8a1cde4e19b7bd41.1755872208.git.mchehab+huawei@kernel.org

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
9be2a5c3 67faed5d

+59 -41
+59 -41
Documentation/sphinx/kernel_include.py
··· 160 160 161 161 return rawtext 162 162 163 + def literal(self, path, tab_width, rawtext): 164 + """Output a literal block""" 165 + 166 + # Convert tabs to spaces, if `tab_width` is positive. 167 + if tab_width >= 0: 168 + text = rawtext.expandtabs(tab_width) 169 + else: 170 + text = rawtext 171 + literal_block = nodes.literal_block(rawtext, source=path, 172 + classes=self.options.get("class", [])) 173 + literal_block.line = 1 174 + self.add_name(literal_block) 175 + if "number-lines" in self.options: 176 + try: 177 + startline = int(self.options["number-lines"] or 1) 178 + except ValueError: 179 + raise self.error(":number-lines: with non-integer start value") 180 + endline = startline + len(include_lines) 181 + if text.endswith("\n"): 182 + text = text[:-1] 183 + tokens = NumberLines([([], text)], startline, endline) 184 + for classes, value in tokens: 185 + if classes: 186 + literal_block += nodes.inline(value, value, 187 + classes=classes) 188 + else: 189 + literal_block += nodes.Text(value, value) 190 + else: 191 + literal_block += nodes.Text(text, text) 192 + return [literal_block] 193 + 194 + def code(self, path, include_lines): 195 + """Output a code block""" 196 + 197 + self.options["source"] = path 198 + codeblock = CodeBlock(self.name, 199 + [self.options.pop("code")], # arguments 200 + self.options, 201 + include_lines, 202 + self.lineno, 203 + self.content_offset, 204 + self.block_text, 205 + self.state, 206 + self.state_machine) 207 + return codeblock.run() 208 + 163 209 def run(self): 164 210 """Include a file as part of the content of this reST file.""" 165 211 env = self.state.document.settings.env ··· 246 200 startline = self.options.get("start-line", None) 247 201 endline = self.options.get("end-line", None) 248 202 203 + if "literal" in self.options: 204 + ouptut_type = "literal" 205 + elif "code" in self.options: 206 + ouptut_type = "code" 207 + else: 208 + ouptut_type = "normal" 209 + 249 210 # Get optional arguments to related to cross-references generation 250 211 if 'generate-cross-refs' in self.options: 251 212 rawtext = self.read_rawtext_with_xrefs(env, path) ··· 266 213 267 214 rawtext = self.apply_range(rawtext) 268 215 216 + if ouptut_type == "literal": 217 + return self.literal(path, tab_width, rawtext) 218 + 269 219 include_lines = statemachine.string2lines(rawtext, tab_width, 270 220 convert_whitespace=True) 271 - if "literal" in self.options: 272 - # Convert tabs to spaces, if `tab_width` is positive. 273 - if tab_width >= 0: 274 - text = rawtext.expandtabs(tab_width) 275 - else: 276 - text = rawtext 277 - literal_block = nodes.literal_block(rawtext, source=path, 278 - classes=self.options.get("class", []) 279 - ) 280 - literal_block.line = 1 281 - self.add_name(literal_block) 282 - if "number-lines" in self.options: 283 - try: 284 - startline = int(self.options["number-lines"] or 1) 285 - except ValueError: 286 - raise self.error(":number-lines: with non-integer start value") 287 - endline = startline + len(include_lines) 288 - if text.endswith("\n"): 289 - text = text[:-1] 290 - tokens = NumberLines([([], text)], startline, endline) 291 - for classes, value in tokens: 292 - if classes: 293 - literal_block += nodes.inline(value, value, 294 - classes=classes) 295 - else: 296 - literal_block += nodes.Text(value, value) 297 - else: 298 - literal_block += nodes.Text(text, text) 299 - return [literal_block] 300 221 301 - if "code" in self.options: 302 - self.options["source"] = path 303 - codeblock = CodeBlock(self.name, 304 - [self.options.pop("code")], # arguments 305 - self.options, 306 - include_lines, # content 307 - self.lineno, 308 - self.content_offset, 309 - self.block_text, 310 - self.state, 311 - self.state_machine) 312 - return codeblock.run() 222 + if ouptut_type == "code": 223 + return self.code(path, include_lines) 224 + 313 225 self.state_machine.insert_input(include_lines, path) 314 226 return [] 315 227