Skip to content

Commit 95c1c85

Browse files
committed
doc: extensions: Add line reference support to zephyr_file/zephyr_module
Enhance the zephyr_file link role to support referencing specific lines or line ranges in files. This change allows users to link to exact locations within a file using GitHub-style line references. Signed-off-by: Benjamin Cabé <[email protected]>
1 parent 6464ffa commit 95c1c85

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

doc/_extensions/zephyr/link-roles.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def role(
8484
link_text = text
8585
link = text
8686

87+
line_ref = ""
88+
line_match = re.search(r"(.+?)(?:#(L\d+(?:-L\d+)?))?$", link)
89+
if line_match and line_match.group(2):
90+
link = line_match.group(1)
91+
line_ref = f"?plain=1#{line_match.group(2)}"
92+
8793
module_match = re.search(r"(.+?):\s*(.+)", link)
8894
if module_match:
8995
module = module_match.group(1).strip()
@@ -124,7 +130,7 @@ def role(
124130
f"{link} not found in {config.link_roles_manifest_project} {trace}"
125131
)
126132

127-
url = f"{baseurl}/{format}/{rev}/{link}"
133+
url = f"{baseurl}/{format}/{rev}/{link}{line_ref}"
128134
node = nodes.reference(rawtext, link_text, refuri=url, **options)
129135
return [node], []
130136

doc/contribute/documentation/guidelines.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,26 @@ very file can be done using the :rst:role:`zephyr_file` role.
942942

943943
Check out :zephyr_file:`doc/contribute/documentation/guidelines.rst` for more information.
944944

945-
You may use the :rst:role:`zephyr_raw` role instead if you want to reference the "raw" content.
945+
You can reference specific lines or line ranges in a file by appending :samp:`#L{line_number}` or
946+
:samp:`#L{start_line}-L{end_line}` to the file path::
947+
948+
See :zephyr_file:`doc/contribute/documentation/guidelines.rst#L3` for the main heading of
949+
this document.
950+
951+
Will render as:
952+
953+
See :zephyr_file:`doc/contribute/documentation/guidelines.rst#L3` for the main heading of
954+
this document.
955+
956+
The role automatically verifies that the referenced file exists in the Zephyr tree and will
957+
generate a warning during documentation build if the file is not found.
958+
959+
.. note::
960+
961+
Use the line references sparingly as keeping them accurate over time can be challenging as the
962+
content of the linked file is subject to change.
963+
964+
You may use the :rst:role:`zephyr_raw` role instead if you want to reference the "raw" content.
946965

947966
.. rst:role:: zephyr_raw
948967
@@ -964,6 +983,7 @@ You may use the :rst:role:`zephyr_raw` role instead if you want to reference the
964983

965984
Check out :module_file:`hal_stm32:CMakeLists.txt` for more information.
966985

986+
Similar to :rst:role:`zephyr_file`, you can reference specific lines or line ranges in a file.
967987

968988
Cross-referencing GitHub issues and pull requests
969989
=================================================

0 commit comments

Comments
 (0)