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

tools: docs: parse_data_structs.py: add methods to return output

When running it from command line, we want to write an output
file, but when used as a class, one may just want the output
content returned as a string.

Split write_output() on two methods to allow both usecases.

Also add an extra method to produce a TOC.

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

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
242cfe3f cde49466

+62 -5
+58 -4
tools/docs/lib/parse_data_structs.py
··· 97 97 "prefix": "\\ ", 98 98 "suffix": "\\ ", 99 99 "ref_type": ":ref", 100 + "description": "IOCTL Commands", 100 101 }, 101 102 "define": { 102 103 "prefix": "\\ ", 103 104 "suffix": "\\ ", 104 105 "ref_type": ":ref", 106 + "description": "Macros and Definitions", 105 107 }, 106 108 # We're calling each definition inside an enum as "symbol" 107 109 "symbol": { 108 110 "prefix": "\\ ", 109 111 "suffix": "\\ ", 110 112 "ref_type": ":ref", 113 + "description": "Enumeration values", 111 114 }, 112 115 "typedef": { 113 116 "prefix": "\\ ", 114 117 "suffix": "\\ ", 115 118 "ref_type": ":c:type", 119 + "description": "Type Definitions", 116 120 }, 117 - # This is the name of the enum itself 121 + # This is the description of the enum itself 118 122 "enum": { 119 123 "prefix": "\\ ", 120 124 "suffix": "\\ ", 121 125 "ref_type": ":c:type", 126 + "description": "Enumerations", 122 127 }, 123 128 "struct": { 124 129 "prefix": "\\ ", 125 130 "suffix": "\\ ", 126 131 "ref_type": ":c:type", 132 + "description": "Structures", 127 133 }, 128 134 } 129 135 ··· 365 359 366 360 print() 367 361 368 - def write_output(self, file_in: str, file_out: str): 362 + def gen_output(self): 369 363 """Write the formatted output to a file.""" 370 364 371 365 # Avoid extra blank lines ··· 393 387 text = re.sub(r"\\ ([\n ])", r"\1", text) 394 388 text = re.sub(r" \\ ", " ", text) 395 389 390 + return text 396 391 392 + def gen_toc(self): 393 + """ 394 + Create a TOC table pointing to each symbol from the header 395 + """ 396 + text = [] 397 + 398 + # Add header 399 + text.append(".. contents:: Table of Contents") 400 + text.append(" :depth: 2") 401 + text.append(" :local:") 402 + text.append("") 403 + 404 + # Sort symbol types per description 405 + symbol_descriptions = [] 406 + for k, v in self.DEF_SYMBOL_TYPES.items(): 407 + symbol_descriptions.append((v['description'], k)) 408 + 409 + symbol_descriptions.sort() 410 + 411 + # Process each category 412 + for description, c_type in symbol_descriptions: 413 + 414 + refs = self.symbols[c_type] 415 + if not refs: # Skip empty categories 416 + continue 417 + 418 + text.append(f"{description}") 419 + text.append("-" * len(description)) 420 + text.append("") 421 + 422 + # Sort symbols alphabetically 423 + for symbol, ref in sorted(refs.items()): 424 + text.append(f"* :{ref}:") 425 + 426 + text.append("") # Add empty line between categories 427 + 428 + return "\n".join(text) 429 + 430 + def write_output(self, file_in: str, file_out: str, toc: bool): 397 431 title = os.path.basename(file_in) 432 + 433 + if toc: 434 + text = self.gen_toc() 435 + else: 436 + text = self.gen_output() 398 437 399 438 with open(file_out, "w", encoding="utf-8", errors="backslashreplace") as f: 400 439 f.write(".. -*- coding: utf-8; mode: rst -*-\n\n") 401 440 f.write(f"{title}\n") 402 - f.write("=" * len(title)) 403 - f.write("\n\n.. parsed-literal::\n\n") 441 + f.write("=" * len(title) + "\n\n") 442 + 443 + if not toc: 444 + f.write(".. parsed-literal::\n\n") 445 + 404 446 f.write(text)
+4 -1
tools/docs/parse-headers.py
··· 36 36 37 37 parser.add_argument("-d", "--debug", action="count", default=0, 38 38 help="Increase debug level. Can be used multiple times") 39 + parser.add_argument("-t", "--toc", action="store_true", 40 + help="instead of a literal block, outputs a TOC table at the RST file") 41 + 39 42 parser.add_argument("file_in", help="Input C file") 40 43 parser.add_argument("file_out", help="Output RST file") 41 44 parser.add_argument("file_rules", nargs="?", ··· 53 50 parser.process_exceptions(args.file_rules) 54 51 55 52 parser.debug_print() 56 - parser.write_output(args.file_in, args.file_out) 53 + parser.write_output(args.file_in, args.file_out, args.toc) 57 54 58 55 59 56 if __name__ == "__main__":