From nobody Sat Oct 4 01:42:15 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 982D5280328; Thu, 21 Aug 2025 14:21:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755786099; cv=none; b=ABGjkJfWcFCtJRMJC61nv+RnJoYCQewFgV63JAPNDYKI9PlBDpcLGXAw+xFoEMSqz1ulZpz4gb++gRk/lDDKru7ApNON6FmAVySIfnlgMAEM/fj9FjPcGVbwQElXgcz43b+rTDRdrlIgKLBy+Bqlqf0o78CIm8WxrDDF08hjub8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755786099; c=relaxed/simple; bh=uE8fFpGnhMJcK5GXfCzqeiiea156Y1/8xBUIdOSHKgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VV/tR9p0d1Bxz+WPEsp+Mj6qNKd1SyHi/I06GUSSf8xQulczhIsevLi+FapxvTlHOQS23u57gw+fyeIOsfe9hZvy+AdJfCQGIm9clCRJswZKqRPxm9JmOwJCVW2fy6Bnz2t4kAUNteeJyo3fniz4WTyNtGERWQIkHaeI08YEnN4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BtlkHiPL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BtlkHiPL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24C2FC19422; Thu, 21 Aug 2025 14:21:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755786099; bh=uE8fFpGnhMJcK5GXfCzqeiiea156Y1/8xBUIdOSHKgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BtlkHiPLBRNfXYO9ys83tffbtgIgbT6sHMOnthh2f9Mpm3nG/fSOkKLxnm+5bs3fR l63JoB1qK0FXSZQKP9aW/H/YcdjiXdX+ILuST7FEZx+bGmblHEX5HpoSB3YNH+zaT9 AQtNNGsxssIMfql2DSJypEYL+UyYqPJ89fGfpZY3rzM1ZtfUgsjUUrnYnwlAFF+xU9 UPsSy+xSmA1OJJf3TfemoBl86gFY21BqEfZaCbawAznJG2l99M5aGglgrSMFXyqZ2u CN2yefe9E4B2zIG6VoPz3rg2rdSB51PTzXrp8okl42jHIlADDPlMMLzCu3qd9YyOwH mxuBU5dEEc7kA== Received: from mchehab by mail.kernel.org with local (Exim 4.98.2) (envelope-from ) id 1up6Ab-0000000BT8S-0gsH; Thu, 21 Aug 2025 16:21:37 +0200 From: Mauro Carvalho Chehab To: Linux Doc Mailing List , Jonathan Corbet Cc: Mauro Carvalho Chehab , linux-kernel@vger.kernel.org Subject: [PATCH 07/24] tools: docs: parse_data_structs.py: add methods to return output Date: Thu, 21 Aug 2025 16:21:13 +0200 Message-ID: <1588eefd164040e5b2d946201705459d36f273de.1755784930.git.mchehab+huawei@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Mauro Carvalho Chehab Content-Type: text/plain; charset="utf-8" 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 --- tools/docs/lib/parse_data_structs.py | 62 ++++++++++++++++++++++++++-- tools/docs/parse-headers.py | 5 ++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/tools/docs/lib/parse_data_structs.py b/tools/docs/lib/parse_da= ta_structs.py index 2b7fa6bd8321..a5aa2e182052 100755 --- a/tools/docs/lib/parse_data_structs.py +++ b/tools/docs/lib/parse_data_structs.py @@ -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", }, } =20 @@ -359,7 +365,7 @@ class ParseDataStructs: =20 print() =20 - def write_output(self, file_in: str, file_out: str): + def gen_output(self): """Write the formatted output to a file.""" =20 # Avoid extra blank lines @@ -387,12 +393,60 @@ class ParseDataStructs: text =3D re.sub(r"\\ ([\n ])", r"\1", text) text =3D re.sub(r" \\ ", " ", text) =20 + return text =20 + def gen_toc(self): + """ + Create a TOC table pointing to each symbol from the header + """ + text =3D [] + + # Add header + text.append(".. contents:: Table of Contents") + text.append(" :depth: 2") + text.append(" :local:") + text.append("") + + # Sort symbol types per description + symbol_descriptions =3D [] + 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 =3D 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 =3D os.path.basename(file_in) =20 + if toc: + text =3D self.gen_toc() + else: + text =3D self.gen_output() + with open(file_out, "w", encoding=3D"utf-8", errors=3D"backslashre= place") as f: f.write(".. -*- coding: utf-8; mode: rst -*-\n\n") f.write(f"{title}\n") - f.write("=3D" * len(title)) - f.write("\n\n.. parsed-literal::\n\n") + f.write("=3D" * len(title) + "\n\n") + + if not toc: + f.write(".. parsed-literal::\n\n") + f.write(text) diff --git a/tools/docs/parse-headers.py b/tools/docs/parse-headers.py index 07d3b47c4834..bfa4e46a53e3 100755 --- a/tools/docs/parse-headers.py +++ b/tools/docs/parse-headers.py @@ -36,6 +36,9 @@ def main(): =20 parser.add_argument("-d", "--debug", action=3D"count", default=3D0, help=3D"Increase debug level. Can be used multiple= times") + parser.add_argument("-t", "--toc", action=3D"store_true", + help=3D"instead of a literal block, outputs a TOC = table at the RST file") + parser.add_argument("file_in", help=3D"Input C file") parser.add_argument("file_out", help=3D"Output RST file") parser.add_argument("file_rules", nargs=3D"?", @@ -50,7 +53,7 @@ def main(): parser.process_exceptions(args.file_rules) =20 parser.debug_print() - parser.write_output(args.file_in, args.file_out) + parser.write_output(args.file_in, args.file_out, args.toc) =20 =20 if __name__ =3D=3D "__main__": --=20 2.50.1