mirror of
https://github.com/torvalds/linux.git
synced 2025-12-01 07:26:02 +07:00
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
This commit is contained in:
committed by
Jonathan Corbet
parent
cde494660f
commit
242cfe3f77
@@ -97,33 +97,39 @@ class ParseDataStructs:
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":ref",
|
||||
"description": "IOCTL Commands",
|
||||
},
|
||||
"define": {
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":ref",
|
||||
"description": "Macros and Definitions",
|
||||
},
|
||||
# We're calling each definition inside an enum as "symbol"
|
||||
"symbol": {
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":ref",
|
||||
"description": "Enumeration values",
|
||||
},
|
||||
"typedef": {
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":c:type",
|
||||
"description": "Type Definitions",
|
||||
},
|
||||
# This is the name of the enum itself
|
||||
# This is the description of the enum itself
|
||||
"enum": {
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":c:type",
|
||||
"description": "Enumerations",
|
||||
},
|
||||
"struct": {
|
||||
"prefix": "\\ ",
|
||||
"suffix": "\\ ",
|
||||
"ref_type": ":c:type",
|
||||
"description": "Structures",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -359,7 +365,7 @@ class ParseDataStructs:
|
||||
|
||||
print()
|
||||
|
||||
def write_output(self, file_in: str, file_out: str):
|
||||
def gen_output(self):
|
||||
"""Write the formatted output to a file."""
|
||||
|
||||
# Avoid extra blank lines
|
||||
@@ -387,12 +393,60 @@ class ParseDataStructs:
|
||||
text = re.sub(r"\\ ([\n ])", r"\1", text)
|
||||
text = re.sub(r" \\ ", " ", text)
|
||||
|
||||
return text
|
||||
|
||||
def gen_toc(self):
|
||||
"""
|
||||
Create a TOC table pointing to each symbol from the header
|
||||
"""
|
||||
text = []
|
||||
|
||||
# Add header
|
||||
text.append(".. contents:: Table of Contents")
|
||||
text.append(" :depth: 2")
|
||||
text.append(" :local:")
|
||||
text.append("")
|
||||
|
||||
# Sort symbol types per description
|
||||
symbol_descriptions = []
|
||||
for k, v in self.DEF_SYMBOL_TYPES.items():
|
||||
symbol_descriptions.append((v['description'], k))
|
||||
|
||||
symbol_descriptions.sort()
|
||||
|
||||
# Process each category
|
||||
for description, c_type in symbol_descriptions:
|
||||
|
||||
refs = self.symbols[c_type]
|
||||
if not refs: # Skip empty categories
|
||||
continue
|
||||
|
||||
text.append(f"{description}")
|
||||
text.append("-" * len(description))
|
||||
text.append("")
|
||||
|
||||
# Sort symbols alphabetically
|
||||
for symbol, ref in sorted(refs.items()):
|
||||
text.append(f"* :{ref}:")
|
||||
|
||||
text.append("") # Add empty line between categories
|
||||
|
||||
return "\n".join(text)
|
||||
|
||||
def write_output(self, file_in: str, file_out: str, toc: bool):
|
||||
title = os.path.basename(file_in)
|
||||
|
||||
if toc:
|
||||
text = self.gen_toc()
|
||||
else:
|
||||
text = self.gen_output()
|
||||
|
||||
with open(file_out, "w", encoding="utf-8", errors="backslashreplace") as f:
|
||||
f.write(".. -*- coding: utf-8; mode: rst -*-\n\n")
|
||||
f.write(f"{title}\n")
|
||||
f.write("=" * len(title))
|
||||
f.write("\n\n.. parsed-literal::\n\n")
|
||||
f.write("=" * len(title) + "\n\n")
|
||||
|
||||
if not toc:
|
||||
f.write(".. parsed-literal::\n\n")
|
||||
|
||||
f.write(text)
|
||||
|
||||
@@ -36,6 +36,9 @@ def main():
|
||||
|
||||
parser.add_argument("-d", "--debug", action="count", default=0,
|
||||
help="Increase debug level. Can be used multiple times")
|
||||
parser.add_argument("-t", "--toc", action="store_true",
|
||||
help="instead of a literal block, outputs a TOC table at the RST file")
|
||||
|
||||
parser.add_argument("file_in", help="Input C file")
|
||||
parser.add_argument("file_out", help="Output RST file")
|
||||
parser.add_argument("file_rules", nargs="?",
|
||||
@@ -50,7 +53,7 @@ def main():
|
||||
parser.process_exceptions(args.file_rules)
|
||||
|
||||
parser.debug_print()
|
||||
parser.write_output(args.file_in, args.file_out)
|
||||
parser.write_output(args.file_in, args.file_out, args.toc)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user