Skip to content

Commit cfed396

Browse files
committed
doc: _extensions: kconfig: add :kconfig:option-regex: role
Allow users to create links to Kconfig regex searches. The new role generates links to the Kconfig search page with the regex pattern as the search query. Fixes #90571 Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 2c2d12f commit cfed396

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

doc/_extensions/zephyr/kconfig/__init__.py

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,26 @@ def found_kconfig_search_directive(self) -> bool:
233233
return self._found
234234

235235

236+
class KconfigRegexRole(XRefRole):
237+
"""Role for creating links to Kconfig regex searches."""
238+
239+
def process_link(self, env: BuildEnvironment, refnode: nodes.Element, has_explicit_title: bool,
240+
title: str, target: str) -> tuple[str, str]:
241+
# render as "normal" text when explicit title is provided, literal otherwise
242+
if has_explicit_title:
243+
self.innernodeclass = nodes.inline
244+
else:
245+
self.innernodeclass = nodes.literal
246+
return title, target
247+
248+
236249
class KconfigDomain(Domain):
237250
"""Kconfig domain"""
238251

239252
name = "kconfig"
240253
label = "Kconfig"
241254
object_types = {"option": ObjType("option", "option")}
242-
roles = {"option": XRefRole()}
255+
roles = {"option": XRefRole(), "option-regex": KconfigRegexRole()}
243256
directives = {"search": KconfigSearch}
244257
initial_data: dict[str, Any] = {"options": set()}
245258

@@ -259,20 +272,56 @@ def resolve_xref(
259272
node: pending_xref,
260273
contnode: nodes.Element,
261274
) -> nodes.Element | None:
262-
match = [
263-
(docname, anchor)
264-
for name, _, _, docname, anchor, _ in self.get_objects()
265-
if name == target
266-
]
267-
268-
if match:
269-
todocname, anchor = match[0]
270-
271-
return make_refnode(
272-
builder, fromdocname, todocname, anchor, contnode, anchor
273-
)
275+
if typ == "option-regex":
276+
# Handle regex search links
277+
search_docname = self._find_search_docname(env)
278+
if search_docname:
279+
# Create a reference to the search page with the regex as a fragment
280+
ref_uri = builder.get_relative_uri(fromdocname, search_docname) + f"#!{target}"
281+
ref_node = nodes.reference('', '', refuri=ref_uri, internal=True)
282+
ref_node.append(contnode)
283+
return ref_node
284+
else:
285+
# Fallback to plain text if no search page is found
286+
return contnode
274287
else:
275-
return None
288+
# Handle regular option links
289+
match = [
290+
(docname, anchor)
291+
for name, _, _, docname, anchor, _ in self.get_objects()
292+
if name == target
293+
]
294+
295+
if match:
296+
todocname, anchor = match[0]
297+
298+
return make_refnode(
299+
builder, fromdocname, todocname, anchor, contnode, anchor
300+
)
301+
else:
302+
return None
303+
304+
def _find_search_docname(self, env: BuildEnvironment) -> str | None:
305+
"""Find the document containing the kconfig search directive."""
306+
# Cache the result to avoid repeated searches
307+
if hasattr(env, '_kconfig_search_docname'):
308+
return env._kconfig_search_docname
309+
310+
for docname in env.all_docs:
311+
try:
312+
doctree = env.get_doctree(docname)
313+
visitor = _FindKconfigSearchDirectiveVisitor(doctree)
314+
doctree.walk(visitor)
315+
if visitor.found_kconfig_search_directive:
316+
env._kconfig_search_docname = docname
317+
return docname
318+
except Exception:
319+
# Skip documents that can't be loaded
320+
continue
321+
322+
# No search directive found
323+
env._kconfig_search_docname = None
324+
return None
276325

277326
def add_option(self, option):
278327
"""Register a new Kconfig option to the domain."""

doc/contribute/documentation/guidelines.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,31 @@ Make sure to use the full name of the Kconfig option, including the ``CONFIG_``
10831083

10841084
Check out :kconfig:option:`CONFIG_GPIO` for more information.
10851085

1086+
.. rst:role:: kconfig:option-regex
1087+
1088+
This role is used to create links to regex searches for Kconfig options. It generates a link to
1089+
the Kconfig search page with the provided regex pattern automatically filled in as the search
1090+
query. It is useful for referencing multiple Kconfig options that share a common prefix, or
1091+
belong to a common category. For example::
1092+
1093+
Check out :kconfig:option-regex:`CONFIG_SECURE_STORAGE_ITS_(STORE|TRANSFORM)_.*_CUSTOM` for
1094+
the various customization possibilities.
1095+
1096+
Will render as:
1097+
1098+
Check out :kconfig:option-regex:`CONFIG_SECURE_STORAGE_ITS_(STORE|TRANSFORM)_.*_CUSTOM` for
1099+
the various customization possibilities.
1100+
1101+
It is encouraged to provide a custom link text to make the reference more readable. For example::
1102+
1103+
Check out the :kconfig:option-regex:`ITS Kconfig options <CONFIG_SECURE_STORAGE_ITS_.*>`
1104+
for more information.
1105+
1106+
Will render as:
1107+
1108+
Check out the :kconfig:option-regex:`ITS Kconfig options <CONFIG_SECURE_STORAGE_ITS_.*>`
1109+
for more information.
1110+
10861111
Devicetree bindings
10871112
===================
10881113

0 commit comments

Comments
 (0)