From nobody Sat Oct 4 00:26:51 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 601B1301476; Fri, 22 Aug 2025 14:19:48 +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=1755872388; cv=none; b=Zlk7+8MiYc/WhiUjxEdKcVkaE2R5bgg1KAHEM5pqbVOwf5puNYGn8EPF0OnS6DtM2HqeCujamulb3gHUFmvskfUHDv5nhLQZ+Wo6+LfAykmwEyqHg7W8thRju2lCROieGO/qIDsyC6gKSLwC0gFRPsNKR+BBeB84+a8C56QaXr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755872388; c=relaxed/simple; bh=uE8fFpGnhMJcK5GXfCzqeiiea156Y1/8xBUIdOSHKgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gu9U4mGr9QKzPrkygxrpVUyAW/4tbVVaeLhq+1CRPKX9acEs6lRGvk0Z8TLoG6zd24Hm6T9k4p4EDoDbifrJNSphwuiuy3BVPT4iLhuJrFLu8Ja30Sdon/4UKkxDAOvJ1eqIZpOVTCVhPE8hnH+Qq/QVEgz0m5soGqwIKVjeSpk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=J28/AtaW; 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="J28/AtaW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 167CAC16AAE; Fri, 22 Aug 2025 14:19:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755872388; bh=uE8fFpGnhMJcK5GXfCzqeiiea156Y1/8xBUIdOSHKgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J28/AtaWZHBdvAsacEffPQkK3sB0xJ44okSEHhpklKZ1OEurf+d1Ea+nOq4pAqdfQ vVSkW29ZSAjAcAIW/vA2nkXj9DRmpKuc33i01NSOI0OzudhcRQghmYKNZEqKurBgbJ fKwNviSheveADFSW8WTdbweDzWDMqlois6fhBnRY+Wap+kTqIRQW+kTtAS4yFoh/Cp QbkNwCksg/zDJK8PTOxfzDu0EVEZ7buAbKO46+Tn/pTyjRGXEl90KJ0Tr7+rPRje6I QORkTU1EJ2R1yWv39/E+6xbLkfyIVMmz5SU9fN3x+y7e/AfcrvxbfhtAdsWkxxhy3m TkgwRQDz/TUpg== Received: from mchehab by mail.kernel.org with local (Exim 4.98.2) (envelope-from ) id 1upScM-0000000CCrJ-1DXT; Fri, 22 Aug 2025 16:19:46 +0200 From: Mauro Carvalho Chehab To: Jonathan Corbet , Linux Doc Mailing List Cc: Mauro Carvalho Chehab , "Mauro Carvalho Chehab" , linux-kernel@vger.kernel.org Subject: [PATCH v2 07/24] tools: docs: parse_data_structs.py: add methods to return output Date: Fri, 22 Aug 2025 16:19:19 +0200 Message-ID: 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