From: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
To: Jonathan Corbet <corbet@lwn.net>,
Linux Doc Mailing List <linux-doc@vger.kernel.org>
Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
"Mauro Carvalho Chehab" <mchehab+huawei@kernel.org>,
Kees Cook <mchehab+huawei@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH v2 11/24] docs: kernel_include.py: allow cross-reference generation
Date: Fri, 22 Aug 2025 16:19:23 +0200 [thread overview]
Message-ID: <efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org> (raw)
In-Reply-To: <cover.1755872208.git.mchehab+huawei@kernel.org>
kernel_include extension was originally designed to be used by the
media comprehensive uAPI documentation, where, instead of simpler
kernel-doc markups, the uAPI documentation is enriched with a larger
text, with images, complex tables, graphs, etc.
There, we wanted to include the much simpler yet documented .h
file.
This extension is needed to include files from other parts of the
Kernel tree outside Documentation, because the original Sphinx
include tag doesn't allow going outside of the directory passed
via sphinx-build command line.
Yet, the cross-references themselves to the full documentation
were using a perl script to create cross-references against the
comprehensive documentation.
As the perl script is now converted to Phython and there is a
Python class producing an include-compatible output with cross
references, add two optional arguments to kernel_include.py:
1. :generate-cross-refs:
If present, instead of reading the file, it calls ParseDataStructs()
class, which converts C data structures into cross-references to
be linked to ReST files containing a more comprehensive documentation;
Don't use it together with :start-line: and/or :end-line:, as
filtering input file line range is currently not supported.
2. :exception-file:
Used together with :generate-cross-refs:. Points to a file containing
rules to ignore C data structs or to use a different reference name,
optionally using a different reference type.
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
Documentation/sphinx/kernel_include.py | 94 ++++++++++++++++++++------
1 file changed, 74 insertions(+), 20 deletions(-)
diff --git a/Documentation/sphinx/kernel_include.py b/Documentation/sphinx/kernel_include.py
index 1212786ac516..fc37e6fa9d96 100755
--- a/Documentation/sphinx/kernel_include.py
+++ b/Documentation/sphinx/kernel_include.py
@@ -25,6 +25,24 @@
Substrings of the form $name or ${name} are replaced by the value of
environment variable name. Malformed variable names and references to
non-existing variables are left unchanged.
+
+ This extension overrides Sphinx include directory, adding two extra
+ arguments:
+
+ 1. :generate-cross-refs:
+
+ If present, instead of reading the file, it calls ParseDataStructs()
+ class, which converts C data structures into cross-references to
+ be linked to ReST files containing a more comprehensive documentation;
+
+ Don't use it together with :start-line: and/or :end-line:, as
+ filtering input file line range is currently not supported.
+
+ 2. :exception-file:
+
+ Used together with :generate-cross-refs:. Points to a file containing
+ rules to ignore C data structs or to use a different reference name,
+ optionally using a different reference type.
"""
# ==============================================================================
@@ -32,6 +50,7 @@
# ==============================================================================
import os.path
+import sys
from docutils import io, nodes, statemachine
from docutils.utils.error_reporting import SafeString, ErrorString
@@ -39,6 +58,11 @@ from docutils.parsers.rst import directives
from docutils.parsers.rst.directives.body import CodeBlock, NumberLines
from docutils.parsers.rst.directives.misc import Include
+srctree = os.path.abspath(os.environ["srctree"])
+sys.path.insert(0, os.path.join(srctree, "tools/docs/lib"))
+
+from parse_data_structs import ParseDataStructs
+
__version__ = "1.0"
@@ -57,6 +81,14 @@ def setup(app):
class KernelInclude(Include):
"""KernelInclude (``kernel-include``) directive"""
+ # Add extra options
+ option_spec = Include.option_spec.copy()
+
+ option_spec.update({
+ 'generate-cross-refs': directives.flag,
+ 'exception-file': directives.unchanged,
+ })
+
def run(self):
env = self.state.document.settings.env
path = os.path.realpath(os.path.expandvars(self.arguments[0]))
@@ -99,28 +131,49 @@ class KernelInclude(Include):
e_handler = self.state.document.settings.input_encoding_error_handler
tab_width = self.options.get("tab-width",
self.state.document.settings.tab_width)
- try:
- self.state.document.settings.record_dependencies.add(path)
- include_file = io.FileInput(source_path=path, encoding=encoding,
- error_handler=e_handler)
- except UnicodeEncodeError:
- raise self.severe('Problems with "%s" directive path:\n'
- 'Cannot encode input file path "%s" '
- "(wrong locale?)." % (self.name, SafeString(path)))
- except IOError as error:
- raise self.severe('Problems with "%s" directive path:\n%s.'
- % (self.name, ErrorString(error)))
startline = self.options.get("start-line", None)
endline = self.options.get("end-line", None)
- try:
- if startline or (endline is not None):
- lines = include_file.readlines()
- rawtext = "".join(lines[startline:endline])
- else:
- rawtext = include_file.read()
- except UnicodeError as error:
- raise self.severe('Problem with "%s" directive:\n%s' %
- (self.name, ErrorString(error)))
+
+ # Get optional arguments to related to cross-references generation
+ if 'generate-cross-refs' in self.options:
+ parser = ParseDataStructs()
+ parser.parse_file(path)
+
+ exceptions_file = self.options.get('exception-file')
+ if exceptions_file:
+ exceptions_file = os.path.join(source_dir, exceptions_file)
+ parser.process_exceptions(exceptions_file)
+
+ title = os.path.basename(path)
+ rawtext = parser.gen_output()
+ if startline or endline:
+ raise self.severe('generate-cross-refs can\'t be used together with "start-line" or "end-line"')
+
+ if "code" not in self.options:
+ rawtext = ".. parsed-literal::\n\n" + rawtext
+ else:
+ try:
+ self.state.document.settings.record_dependencies.add(path)
+ include_file = io.FileInput(source_path=path, encoding=encoding,
+ error_handler=e_handler)
+ except UnicodeEncodeError:
+ raise self.severe('Problems with "%s" directive path:\n'
+ 'Cannot encode input file path "%s" '
+ "(wrong locale?)." % (self.name, SafeString(path)))
+ except IOError as error:
+ raise self.severe('Problems with "%s" directive path:\n%s.'
+ % (self.name, ErrorString(error)))
+
+ try:
+ if startline or (endline is not None):
+ lines = include_file.readlines()
+ rawtext = "".join(lines[startline:endline])
+ else:
+ rawtext = include_file.read()
+ except UnicodeError as error:
+ raise self.severe('Problem with "%s" directive:\n%s' %
+ (self.name, ErrorString(error)))
+
# start-after/end-before: no restrictions on newlines in match-text,
# and no restrictions on matching inside lines vs. line boundaries
after_text = self.options.get("start-after", None)
@@ -171,6 +224,7 @@ class KernelInclude(Include):
else:
literal_block += nodes.Text(text, text)
return [literal_block]
+
if "code" in self.options:
self.options["source"] = path
codeblock = CodeBlock(self.name,
--
2.50.1
next prev parent reply other threads:[~2025-08-22 14:19 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-22 14:19 [PATCH v2 00/24] better handle media headers Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 01/24] docs: parse-headers.pl: improve its debug output format Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 02/24] docs: parse-headers.py: convert parse-headers.pl Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 03/24] docs: parse-headers.py: improve --help logic Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 04/24] docs: parse-headers.py: better handle @var arguments Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 05/24] docs: parse-headers.py: simplify the rules for hashes Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 06/24] tools: docs: parse-headers.py: move it from sphinx dir Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 07/24] tools: docs: parse_data_structs.py: add methods to return output Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 08/24] MAINTAINERS: add files from tools/docs to documentation entry Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 09/24] docs: uapi: media: Makefile: use parse-headers.py Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 10/24] docs: kernel_include.py: Update its coding style Mauro Carvalho Chehab
2025-08-22 14:19 ` Mauro Carvalho Chehab [this message]
2025-08-22 14:19 ` [PATCH v2 12/24] docs: kernel_include.py: generate warnings for broken refs Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 13/24] docs: kernel_include.py: move rawtext logic to separate functions Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 14/24] docs: kernel_include.py: move range logic to a separate function Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 15/24] docs: kernel_include.py: remove range restriction for gen docs Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 16/24] docs: kernel_include.py: move code and literal functions Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 17/24] docs: kernel_include.py: add support to generate a TOC table Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 18/24] docs: kernel_include.py: append line numbers to better report errors Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 19/24] docs: kernel_include.py: move apply_range() and add a docstring Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 20/24] docs: kernel_include.py: remove line numbers from parsed-literal Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 21/24] docs: kernel_include.py: remove Include class inheritance Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 22/24] docs: kernel_include.py: document all supported parameters Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 23/24] scripts: sphinx-build-wrapper: get rid of uapi/media Makefile Mauro Carvalho Chehab
2025-08-22 14:32 ` Mauro Carvalho Chehab
2025-08-22 14:19 ` [PATCH v2 24/24] docs: sphinx: drop parse-headers.pl Mauro Carvalho Chehab
2025-08-29 22:03 ` [PATCH v2 00/24] better handle media headers Jonathan Corbet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=efc39c8e54a2056ae2fdb94d5006fcb19e227198.1755872208.git.mchehab+huawei@kernel.org \
--to=mchehab+huawei@kernel.org \
--cc=corbet@lwn.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).