Skip to content

Commit 57714cb

Browse files
committed
add hashed id when not given
1 parent 1086470 commit 57714cb

File tree

10 files changed

+93
-6
lines changed

10 files changed

+93
-6
lines changed

docs/source/basics/quickstart.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Example
4646
-------
4747

4848
.. src-trace:: dummy src
49-
:id: SRC_000
5049
:project: src
5150

5251
.. note:: **local-url** is not working on the website as it only supports local browse

src/sphinx_codelinks/sphinx_extension/directives/src_trace.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from collections.abc import Callable
2+
import hashlib
23
import os
34
from pathlib import Path
45
from typing import Any, ClassVar, cast
@@ -7,6 +8,7 @@
78
from docutils.parsers.rst import directives
89
from packaging.version import Version
910
import sphinx
11+
from sphinx.config import Config as _SphinxConfig
1012
from sphinx.util.docutils import SphinxDirective
1113
from sphinx_needs.api import add_need # type: ignore[import-untyped]
1214
from sphinx_needs.utils import add_doc # type: ignore[import-untyped]
@@ -33,6 +35,42 @@
3335
logger = logging.getLogger(__name__)
3436

3537

38+
def _check_id(
39+
config: _SphinxConfig,
40+
id: str | None,
41+
src_strings: list[str],
42+
options: dict[str, str],
43+
extra_options: dict[str, str],
44+
) -> None:
45+
"""Check and set the id for the need.
46+
47+
src_strings[0] is always the title.
48+
src_strings[1] is always the project.
49+
"""
50+
if config.needs_id_required:
51+
if id:
52+
extra_options["id"] = id
53+
else:
54+
if "directory" in options:
55+
src_strings.append(options["directory"])
56+
if "file" in options:
57+
src_strings.append(options["file"])
58+
59+
extra_options["id"] = _make_hashed_id("SRCTRACE_", src_strings, config)
60+
61+
62+
def _make_hashed_id(
63+
type_prefix: str, src_strings: list[str], config: _SphinxConfig
64+
) -> str:
65+
"""Create an ID based on the type and title of the need."""
66+
full_title = src_strings[0] # title is always the first element
67+
hashable_content = "_".join(src_strings)
68+
hashed = hashlib.sha256(hashable_content.encode("UTF-8")).hexdigest().upper()
69+
if config.needs_id_from_title:
70+
hashed = full_title.upper().replace(" ", "_") + "_" + hashed
71+
return f"{type_prefix}{hashed[: config.needs_id_length]}"
72+
73+
3674
def get_rel_path(doc_path: Path, code_path: Path, base_dir: Path) -> tuple[Path, Path]:
3775
"""Get the relative path from the document to the source code file and vice versa."""
3876
doc_depth = len(doc_path.parents) - 1
@@ -110,8 +148,9 @@ def run(self) -> list[nodes.Node]:
110148
target_dir = out_dir / src_dir.name
111149

112150
extra_options = {"project": project}
113-
if id:
114-
extra_options["id"] = id
151+
152+
_check_id(self.env.config, id, [title, project], self.options, extra_options)
153+
115154
source_files = self.get_src_files(self.options, src_dir, src_discover_config)
116155

117156
# add source files into the dependency
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<document source="<source>">
2-
<target anonymous="" ids="SRC_000" refid="SRC_000">
3-
<Need classes="need need-srctrace" ids="SRC_000" refid="SRC_000">
2+
<target anonymous="" ids="ST_670E3" refid="ST_670E3">
3+
<Need classes="need need-srctrace" ids="ST_670E3" refid="ST_670E3">
44
<target anonymous="" ids="IMPL_1" refid="IMPL_1">
55
<Need classes="need need-impl" ids="IMPL_1" refid="IMPL_1">
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<document source="<source>">
2+
<target anonymous="" ids="SRCTRACE_F70E0" refid="SRCTRACE_F70E0">
3+
<Need classes="need need-srctrace" ids="SRCTRACE_F70E0" refid="SRCTRACE_F70E0">
4+
<target anonymous="" ids="IMPL_1" refid="IMPL_1">
5+
<Need classes="need need-impl" ids="IMPL_1" refid="IMPL_1">

tests/doc_test/id_required/conf.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project information -----------------------------------------------------
7+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
8+
9+
project = "source-tracing-demo"
10+
copyright = "2025, useblocks"
11+
author = "useblocks"
12+
13+
# -- General configuration ---------------------------------------------------
14+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
15+
16+
extensions = ["sphinx_needs", "sphinx_codelinks"]
17+
18+
templates_path = ["_templates"]
19+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
20+
21+
src_trace_config_from_toml = "src_trace.toml"
22+
23+
# -- Options for HTML output -------------------------------------------------
24+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
25+
26+
html_theme = "alabaster"
27+
html_static_path = ["_static"]
28+
29+
# Sphinx-Needs configuration
30+
needs_id_required = True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
3+
// @ title here, IMPL_1, impl
4+
void singleLineExample()
5+
{
6+
std::cout << "Single-line comment example" << std::endl;
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. src-trace:: dummy src
2+
:project: src
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[codelinks.projects.src]
2+
remote_url_pattern = "https://github.com/useblocks/sphinx-codelinks/blob/{commit}/{path}#L{line}"
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
.. src-trace:: dummy src
2-
:id: SRC_000
32
:project: src

tests/test_src_trace.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ def test_src_tracing_config_positive(make_app: Callable[..., SphinxTestApp], tmp
187187
Path("doc_test") / "minimum_config",
188188
Path("doc_test") / "minimum_config",
189189
),
190+
(
191+
Path("doc_test") / "id_required",
192+
Path("doc_test") / "id_required",
193+
),
190194
],
191195
)
192196
def test_build_html(

0 commit comments

Comments
 (0)