Skip to content

Commit dc4627b

Browse files
juiwenchenubmarco
andauthored
👌Remove the need item for src-trace (#39)
* remove the node from src-trace - updated TCs - updated docs * updated chang_log * updated fixture for the latest syrups * Update docs/source/development/change_log.rst Co-authored-by: Marco Heinemann <[email protected]> * Update docs/source/development/change_log.rst Co-authored-by: Marco Heinemann <[email protected]> * Update docs/source/development/change_log.rst Co-authored-by: Marco Heinemann <[email protected]> * Update docs/source/development/change_log.rst Co-authored-by: Marco Heinemann <[email protected]> * Update docs/source/development/change_log.rst Co-authored-by: Marco Heinemann <[email protected]> * updated ubcode config --------- Co-authored-by: Marco Heinemann <[email protected]>
1 parent 2412820 commit dc4627b

File tree

14 files changed

+76
-100
lines changed

14 files changed

+76
-100
lines changed

‎docs/source/basics/quickstart.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Directive
4545
Example
4646
-------
4747

48-
.. src-trace:: dummy src
48+
.. src-trace::
4949
:project: src
5050

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

‎docs/source/components/directive.rst‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Directive
99

1010
.. code-block:: rst
1111
12-
.. src-trace:: example_with_file
12+
.. src-trace::
1313
:project: project_config
1414
:file: example.cpp
1515
1616
or
1717

1818
.. code-block:: rst
1919
20-
.. src-trace:: example_with_directory
20+
.. src-trace::
2121
:project: project_config
2222
:directory: ./example
2323
@@ -51,31 +51,27 @@ The ``src-trace`` directive can be used with the **file** option:
5151

5252
.. code-block:: rst
5353
54-
.. src-trace:: dcdc demo_1
55-
:id: SRC_001
54+
.. src-trace::
5655
:project: dcdc
5756
:file: ./charge/demo_1.cpp
5857
5958
The needs defined in source code are extracted and rendered to:
6059

61-
.. src-trace:: dcdc demo_1
62-
:id: SRC_001
60+
.. src-trace::
6361
:project: dcdc
6462
:file: ./charge/demo_1.cpp
6563

6664
The ``src-trace`` directive can be used with the **directory** option:
6765

6866
.. code-block:: rst
6967
70-
.. src-trace:: dcdc charge
71-
:id: SRC_001
68+
.. src-trace::
7269
:project: dcdc
7370
:directory: ./discharge
7471
7572
The needs defined in source code are extracted and rendered to:
7673

77-
.. src-trace:: dcdc charge
78-
:id: SRC_002
74+
.. src-trace::
7975
:project: dcdc
8076
:directory: ./discharge
8177

‎docs/source/development/change_log.rst‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ Changelog
88
Unreleased
99
----------
1010

11+
New and Improved
12+
................
13+
14+
- ✨ Added C# parser for ``analyse`` module.
15+
16+
Need ID references and marked RST blocks can be extracted from C# source files.
17+
The comments styles supported are:(``//``, ``/* */``, ``///``)
18+
19+
- ✨ Added YAML parser for ``analyse`` module.
20+
21+
Need ID references can be extracted from YAML files.
22+
The supported comment style is ``#`` as well as inline comment style, e.g. ``key: value # comment``.
23+
24+
- 👌 Directive ``src-trace`` itself does not create need items anymore and only generate need items from the one-line need definition in the given source.
25+
26+
The need item is removed because:
27+
28+
- It has no use cases so far.
29+
- It creates extra need items users may not actually want in their documentation
30+
- It creates errors with some Sphinx-Needs configurations, e.g., when ``need_id_required`` or ``needs_statuses`` is defined.
31+
1132
Fixes
1233
.....
1334

@@ -40,17 +61,17 @@ New and Improved
4061
The ``write rst`` command writes a reStructuredText file with :external+needs:ref:`needextend <needextend>` directive from the extracted markers generated by ``analyse``.
4162
The generated RST can be included in the Sphinx documentation to create the source code links in the existing needs
4263

43-
- 🔨 Replaced ``virtual_docs`` with the new ``analyse`` module.
64+
- 👌 Replaced ``virtual_docs`` with the new ``analyse`` module.
4465

4566
The ``virtual_docs`` feature, which handled one-line need definitions (:ref:`OneLineCommentStyle <oneline>`),
4667
has been migrated into the new ``analyse`` module and removed from the core.
4768
The caching feature of ``virtual_docs`` is temporarily removed and may be reintroduced later.
4869

49-
- 🔨 Updated the ``src-trace`` Sphinx directive.
70+
- 👌 Updated the ``src-trace`` Sphinx directive.
5071

5172
The ``src-trace`` directive now uses the new ``analyse`` API instead of the old ``virtual_docs`` one.
5273

53-
- 🔨 Unified configuration in TOML
74+
- 👌 Unified configuration in TOML
5475

5576
The configuration for ``src-trace`` directive defined in TOML is now compatible with the new ``analyse`` module.
5677

‎docs/ubproject.toml‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,37 @@ ignore = ["block.title_line"]
55

66
[needs]
77
id_required = true
8+
9+
[parse.extend_directives.src-trace]
10+
argument = false
11+
options = true
12+
content = false
13+
content_required = false
14+
parse_content = false
15+
description = "A directive to generate need items from one-line definition"
16+
extension = "sphinx-codelinks" # The extension that provides the directive
17+
18+
[parse.extend_directives.video]
19+
argument = true
20+
content = false
21+
options = true
22+
23+
[parse.extend_directives.tabs]
24+
argument = true
25+
content = true
26+
options = true
27+
28+
[parse.extend_directives.typer]
29+
argument = true
30+
content = true
31+
options = true
32+
33+
[parse.extend_directives.button-ref]
34+
argument = true
35+
content = true
36+
options = true
37+
38+
[parse.extend_directives.button-link]
39+
argument = true
40+
content = true
41+
options = true

‎src/sphinx_codelinks/sphinx_extension/directives/src_trace.py‎

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from collections.abc import Callable
2-
import hashlib
32
import os
43
from pathlib import Path
54
from typing import Any, ClassVar, cast
@@ -8,7 +7,6 @@
87
from docutils.parsers.rst import directives
98
from packaging.version import Version
109
import sphinx
11-
from sphinx.config import Config as _SphinxConfig
1210
from sphinx.util.docutils import SphinxDirective
1311
from sphinx_needs.api import add_need # type: ignore[import-untyped]
1412
from sphinx_needs.utils import add_doc # type: ignore[import-untyped]
@@ -35,42 +33,6 @@
3533
logger = logging.getLogger(__name__)
3634

3735

38-
def _check_id(
39-
config: _SphinxConfig,
40-
id: str | None,
41-
src_strings: list[str],
42-
options: dict[str, str],
43-
additional_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-
additional_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-
additional_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-
7436
def get_rel_path(doc_path: Path, code_path: Path, base_dir: Path) -> tuple[Path, Path]:
7537
"""Get the relative path from the document to the source code file and vice versa."""
7638
doc_depth = len(doc_path.parents) - 1
@@ -114,13 +76,12 @@ class SourceTracing(nodes.General, nodes.Element):
11476

11577

11678
class SourceTracingDirective(SphinxDirective):
117-
required_arguments = 1
79+
required_arguments = 0
11880
optional_arguments = 0
11981
final_argument_whitespace = True
12082
# this enables content in the directive
121-
has_content = True
83+
has_content = False
12284
option_spec: ClassVar[dict[str, Callable[[str], str]] | None] = {
123-
"id": directives.unchanged_required,
12485
"project": directives.unchanged_required,
12586
"file": directives.unchanged_required,
12687
"directory": directives.unchanged_required,
@@ -131,8 +92,6 @@ def run(self) -> list[nodes.Node]:
13192
validate_option(self.options)
13293

13394
project = self.options["project"]
134-
id = self.options.get("id")
135-
title = self.arguments[0]
13695
# get source tracing config
13796
src_trace_sphinx_config = CodeLinksConfig.from_sphinx(self.env.config)
13897

@@ -147,12 +106,6 @@ def run(self) -> list[nodes.Node]:
147106
# the directory where the source files are copied to
148107
target_dir = out_dir / src_dir.name
149108

150-
additional_options = {"project": project}
151-
152-
_check_id(
153-
self.env.config, id, [title, project], self.options, additional_options
154-
)
155-
156109
source_files = self.get_src_files(self.options, src_dir, src_discover_config)
157110

158111
# add source files into the dependency
@@ -166,20 +119,6 @@ def run(self) -> list[nodes.Node]:
166119
src_analyse = SourceAnalyse(analyse_config)
167120
src_analyse.run()
168121

169-
needs = []
170-
171-
# create the need for src-trace directive
172-
src_trace_need = add_need(
173-
app=self.env.app, # The Sphinx application object
174-
state=self.state, # The docutils state object
175-
docname=self.env.docname, # The current document name
176-
lineno=self.lineno, # The line number where the directive is used
177-
need_type="srctrace", # The type of the need
178-
title=title, # The title of the need
179-
**additional_options,
180-
)
181-
needs.extend(src_trace_need)
182-
183122
dirs = {
184123
"src_dir": src_dir,
185124
"out_dir": out_dir,
@@ -233,14 +172,12 @@ def run(self) -> list[nodes.Node]:
233172
remote_url_field,
234173
dirs,
235174
)
236-
if rendered_needs:
237-
needs.extend(rendered_needs)
238175

239176
# for post-processing of need links
240177
# https://github.com/useblocks/sphinx-needs/issues/1210
241178
add_doc(self.env, self.env.docname)
242179

243-
return needs
180+
return rendered_needs
244181

245182
def get_src_files(
246183
self,

‎tests/__snapshots__/test_src_trace/test_build_html[sphinx_project0-source_code0].doctree.xml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<document source="<source>">
2-
<target anonymous="" ids="ST_776C6" refid="ST_776C6">
3-
<Need classes="need need-srctrace" ids="ST_776C6" refid="ST_776C6">
42
<target anonymous="" ids="IMPL_singleLineExample" refid="IMPL_singleLineExample">
53
<Need classes="need need-impl" ids="IMPL_singleLineExample" refid="IMPL_singleLineExample">
64
<target anonymous="" ids="IMPL_multiLineExample" refid="IMPL_multiLineExample">
@@ -9,8 +7,6 @@
97
<Need classes="need need-impl" ids="IMPL_14" refid="IMPL_14">
108
<target anonymous="" ids="IMPL_main_supercharge" refid="IMPL_main_supercharge">
119
<Need classes="need need-impl" ids="IMPL_main_supercharge" refid="IMPL_main_supercharge">
12-
<target anonymous="" ids="ST_FB728" refid="ST_FB728">
13-
<Need classes="need need-srctrace" ids="ST_FB728" refid="ST_FB728">
1410
<target anonymous="" ids="IMPL_processAssemble" refid="IMPL_processAssemble">
1511
<Need classes="need need-impl" ids="IMPL_processAssemble" refid="IMPL_processAssemble">
1612
<target anonymous="" ids="IMPL_filterData" refid="IMPL_filterData">
@@ -21,8 +17,6 @@
2117
<Need classes="need need-impl" ids="IMPL_processAggregate" refid="IMPL_processAggregate">
2218
<target anonymous="" ids="IMPL_main_demo2" refid="IMPL_main_demo2">
2319
<Need classes="need need-impl" ids="IMPL_main_demo2" refid="IMPL_main_demo2">
24-
<target anonymous="" ids="ST_9F1BA" refid="ST_9F1BA">
25-
<Need classes="need need-srctrace" ids="ST_9F1BA" refid="ST_9F1BA">
2620
<target anonymous="" ids="IMPL_displayUI" refid="IMPL_displayUI">
2721
<Need classes="need need-impl" ids="IMPL_displayUI" refid="IMPL_displayUI">
2822
<target anonymous="" ids="IMPL_backupData" refid="IMPL_backupData">

‎tests/__snapshots__/test_src_trace/test_build_html[sphinx_project1-source_code1].doctree.xml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<document source="<source>">
2-
<target anonymous="" ids="ST_670E3" refid="ST_670E3">
3-
<Need classes="need need-srctrace" ids="ST_670E3" refid="ST_670E3">
42
<target anonymous="" ids="IMPL_1" refid="IMPL_1">
53
<Need classes="need need-impl" ids="IMPL_1" refid="IMPL_1">
64
<target anonymous="" ids="IMPL_2" refid="IMPL_2">
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<document source="<source>">
2-
<target anonymous="" ids="ST_670E3" refid="ST_670E3">
3-
<Need classes="need need-srctrace" ids="ST_670E3" refid="ST_670E3">
42
<target anonymous="" ids="IMPL_1" refid="IMPL_1">
53
<Need classes="need need-impl" ids="IMPL_1" refid="IMPL_1">
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<document source="<source>">
2-
<target anonymous="" ids="SRCTRACE_F70E0" refid="SRCTRACE_F70E0">
3-
<Need classes="need need-srctrace" ids="SRCTRACE_F70E0" refid="SRCTRACE_F70E0">
42
<target anonymous="" ids="IMPL_1" refid="IMPL_1">
53
<Need classes="need need-impl" ids="IMPL_1" refid="IMPL_1">

‎tests/conftest.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def temporary_gitignore(source_directory: Path):
5858

5959
class DoctreeSnapshotExtension(SingleFileSnapshotExtension):
6060
_write_mode = WriteMode.TEXT
61-
_file_extension = "doctree.xml"
61+
file_extension = "doctree.xml"
6262

6363
def serialize(self, data, **_kwargs):
6464
if not isinstance(data, document):
@@ -84,7 +84,7 @@ def snapshot_doctree(snapshot):
8484

8585
class AnchorsSnapshotExtension(SingleFileSnapshotExtension):
8686
_write_mode = WriteMode.TEXT
87-
_file_extension = "anchors.json"
87+
file_extension = "anchors.json"
8888

8989
def serialize(self, data, **_kwargs):
9090
if not isinstance(data, list):

0 commit comments

Comments
 (0)