aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/sphinx/kernel_include.py
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab+huawei@kernel.org>2025-08-22 16:19:26 +0200
committerJonathan Corbet <corbet@lwn.net>2025-08-29 15:54:43 -0600
commit3f7f3d494119170dc6636228e8425048effa2931 (patch)
treeebae5f799c86bb372ededee79edfdbaa87429f17 /Documentation/sphinx/kernel_include.py
parentdocs: kernel_include.py: move rawtext logic to separate functions (diff)
downloadlinux-3f7f3d494119170dc6636228e8425048effa2931.tar.gz
linux-3f7f3d494119170dc6636228e8425048effa2931.zip
docs: kernel_include.py: move range logic to a separate function
Cleanup run() function by moving the range logic to a separate function. Here, I ended checking the current Sphinx implementation, as it has some extra logic for the range check. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Link: https://lore.kernel.org/r/12fa2204a9e7e309ae4b8694a37ebad9327ca634.1755872208.git.mchehab+huawei@kernel.org
Diffstat (limited to 'Documentation/sphinx/kernel_include.py')
-rwxr-xr-xDocumentation/sphinx/kernel_include.py51
1 files changed, 33 insertions, 18 deletions
diff --git a/Documentation/sphinx/kernel_include.py b/Documentation/sphinx/kernel_include.py
index ef86ee9e79d6..c5f4f34e22cb 100755
--- a/Documentation/sphinx/kernel_include.py
+++ b/Documentation/sphinx/kernel_include.py
@@ -131,6 +131,38 @@ class KernelInclude(Include):
return parser.gen_output()
+ def apply_range(self, rawtext):
+ # Get to-be-included content
+ startline = self.options.get('start-line', None)
+ endline = self.options.get('end-line', None)
+ try:
+ if startline or (endline is not None):
+ lines = rawtext.splitlines()
+ rawtext = '\n'.join(lines[startline:endline])
+ except UnicodeError as error:
+ raise self.severe(f'Problem with "{self.name}" directive:\n'
+ + io.error_string(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)
+ if after_text:
+ # skip content in rawtext before *and incl.* a matching text
+ after_index = rawtext.find(after_text)
+ if after_index < 0:
+ raise self.severe('Problem with "start-after" option of "%s" '
+ "directive:\nText not found." % self.name)
+ rawtext = rawtext[after_index + len(after_text) :]
+ before_text = self.options.get("end-before", None)
+ if before_text:
+ # skip content in rawtext after *and incl.* a matching text
+ before_index = rawtext.find(before_text)
+ if before_index < 0:
+ raise self.severe('Problem with "end-before" option of "%s" '
+ "directive:\nText not found." % self.name)
+ rawtext = rawtext[:before_index]
+
+ return rawtext
+
def run(self):
"""Include a file as part of the content of this reST file."""
env = self.state.document.settings.env
@@ -185,24 +217,7 @@ class KernelInclude(Include):
else:
rawtext = self.read_rawtext(path, encoding)
- # 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)
- if after_text:
- # skip content in rawtext before *and incl.* a matching text
- after_index = rawtext.find(after_text)
- if after_index < 0:
- raise self.severe('Problem with "start-after" option of "%s" '
- "directive:\nText not found." % self.name)
- rawtext = rawtext[after_index + len(after_text) :]
- before_text = self.options.get("end-before", None)
- if before_text:
- # skip content in rawtext after *and incl.* a matching text
- before_index = rawtext.find(before_text)
- if before_index < 0:
- raise self.severe('Problem with "end-before" option of "%s" '
- "directive:\nText not found." % self.name)
- rawtext = rawtext[:before_index]
+ rawtext = self.apply_range(rawtext)
include_lines = statemachine.string2lines(rawtext, tab_width,
convert_whitespace=True)