Skip to content

Commit 19c1797

Browse files
authored
Merge pull request #4 from useblocks/fix-default-config
🔧Fix default config
2 parents 438639c + 0c17a49 commit 19c1797

32 files changed

+323
-273
lines changed

docs/source/basics/local_link.gif

515 KB
Loading

docs/source/basics/quickstart.rst

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,54 @@
11
Quick Start
22
===========
33

4-
``CodeLinks`` provides ``src-trace`` directive and it can be used in the following ways:
4+
.. image:: local_link.gif
55

6-
.. code-block:: rst
6+
Three steps to quickly run ``CodeLinks`` to achieve **the above**:
77

8-
.. src-trace:: example_with_file
9-
:project: project_config
10-
:file: example.cpp
8+
- Configure Sphinx
9+
- Apply One-line comment for a ``Sphinx-Needs`` need item
10+
- Use ``src-trace`` directive
1111

12-
or
13-
14-
.. code-block:: rst
15-
16-
.. src-trace:: example_with_directory
17-
:project: project_config
18-
:directory: ./example
19-
20-
``src-trace`` directive has the following options:
21-
22-
* **project**: the project config specified in ``conf.py`` or ``toml`` file to be used for source tracing.
23-
* **file**: the source file to be traced.
24-
* **directory**: the source files in the directory to be traced recursively.
25-
26-
Regarding the **file** and **directory** options:
27-
28-
- they are optional and mutually exclusive.
29-
- the given paths are relative to ``src_dir`` defined in the source tracing configuration
30-
- if not given, the whole project will be examined.
31-
32-
Example
33-
-------
34-
35-
With the following configuration for a demo source code project `dcdc <https://github.com/useblocks/sphinx-codelinks/tree/main/tests/data/dcdc>`_,
12+
Sphinx Config
13+
-------------
3614

3715
.. code-block:: python
3816
:caption: conf.py
3917
18+
extensions = [
19+
'sphinx_needs',
20+
'sphinx_codelinks'
21+
]
4022
src_trace_config_from_toml = "src_trace.toml"
4123
4224
.. literalinclude:: ./../../src_trace.toml
4325
:caption: src_trace.toml
4426
:language: toml
27+
:lines: 27-29
4528

46-
``src-trace`` directive can be used with **file** option:
29+
One-line comment
30+
----------------
4731

48-
.. code-block:: rst
32+
.. literalinclude:: ./../../../tests/doc_test/minimum_config/dummy_src.cpp
33+
:caption: dummy_src.cpp
34+
:language: cpp
4935

50-
.. src-trace:: dcdc demo_1
51-
:project: dcdc
52-
:file: ./charge/demo_1.cpp
5336

54-
The needs defined in source code are extracted and rendered to:
37+
Directive
38+
---------
5539

56-
.. src-trace:: dcdc demo_1
57-
:project: dcdc
58-
:file: ./charge/demo_1.cpp
40+
.. literalinclude:: ./../../../tests/doc_test/minimum_config/index.rst
41+
:caption: index.rst
42+
:language: rst
5943

60-
``src-trace`` directive can be used with **directory** option:
6144

62-
.. code-block:: rst
6345

64-
.. src-trace:: dcdc charge
65-
:project: dcdc
66-
:directory: ./discharge
46+
Example
47+
-------
6748

68-
The needs defined in source code are extracted and rendered to:
49+
.. src-trace:: dummy src
50+
:project: src
6951

70-
.. src-trace:: dcdc charge
71-
:project: dcdc
72-
:directory: ./discharge
52+
.. note:: **local-url** is not working on the website as it only supports local browse
7353

74-
To have a more customized configuration of ``CodeLinks``, please refer to :ref:`configuration <configuration>`.
54+
Section :ref:`Directive <directive>` provides more adavanced usage.

docs/source/components/configuration.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ but with the ``src_trace_`` prefix removed.
3737
src_trace_set_local_url
3838
~~~~~~~~~~~~~~~~~~~~~~~
3939

40-
Set this option to ``False``, if the local link between a need to the local source code where it is defined is not required.
40+
Set this option to ``True``, if the local link between a need to the local source code where it is defined is required.
4141

42-
Default: **True**
42+
Default: **False**
4343

4444
.. tabs::
4545

@@ -77,12 +77,12 @@ Default: **local-url**
7777
src_trace_set_remote_url
7878
~~~~~~~~~~~~~~~~~~~~~~~~
7979

80-
Set this option to ``False``, if the remote link between a need to the remote source code
81-
where it is defined is not required.
80+
Set this option to ``True``, if the remote link between a need to the remote source code
81+
where it is defined is required.
8282

8383
The remote means where the source code is hosted such as GitHub.
8484

85-
Default: **True**
85+
Default: **False**
8686

8787
.. tabs::
8888

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. _directive:
2+
3+
Directive
4+
=========
5+
6+
``CodeLinks`` provides ``src-trace`` directive and it can be used in the following ways:
7+
8+
.. code-block:: rst
9+
10+
.. src-trace:: example_with_file
11+
:project: project_config
12+
:file: example.cpp
13+
14+
or
15+
16+
.. code-block:: rst
17+
18+
.. src-trace:: example_with_directory
19+
:project: project_config
20+
:directory: ./example
21+
22+
``src-trace`` directive has the following options:
23+
24+
* **project**: the project config specified in ``conf.py`` or ``toml`` file to be used for source tracing.
25+
* **file**: the source file to be traced.
26+
* **directory**: the source files in the directory to be traced recursively.
27+
28+
Regarding the **file** and **directory** options:
29+
30+
- they are optional and mutually exclusive.
31+
- the given paths are relative to ``src_dir`` defined in the source tracing configuration
32+
- if not given, the whole project will be examined.
33+
34+
Example
35+
-------
36+
37+
With the following configuration for a demo source code project `dcdc <https://github.com/useblocks/sphinx-codelinks/tree/main/tests/data/dcdc>`_,
38+
39+
.. code-block:: python
40+
:caption: conf.py
41+
42+
src_trace_config_from_toml = "src_trace.toml"
43+
44+
.. literalinclude:: ./../../src_trace.toml
45+
:caption: src_trace.toml
46+
:language: toml
47+
:lines: 1-25
48+
49+
``src-trace`` directive can be used with **file** option:
50+
51+
.. code-block:: rst
52+
53+
.. src-trace:: dcdc demo_1
54+
:project: dcdc
55+
:file: ./charge/demo_1.cpp
56+
57+
The needs defined in source code are extracted and rendered to:
58+
59+
.. src-trace:: dcdc demo_1
60+
:project: dcdc
61+
:file: ./charge/demo_1.cpp
62+
63+
``src-trace`` directive can be used with **directory** option:
64+
65+
.. code-block:: rst
66+
67+
.. src-trace:: dcdc charge
68+
:project: dcdc
69+
:directory: ./discharge
70+
71+
The needs defined in source code are extracted and rendered to:
72+
73+
.. src-trace:: dcdc charge
74+
:project: dcdc
75+
:directory: ./discharge
76+
77+
.. note:: **local-url** is not working on the website as it only supports local browse
78+
79+
To have a more customized configuration of ``CodeLinks``, please refer to :ref:`configuration <configuration>`.

docs/source/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ Contents
5757
:maxdepth: 1
5858
:caption: Components
5959

60+
components/cli
6061
components/configuration
62+
components/directive
6163
components/oneline
62-
components/cli
64+
6365

6466
.. toctree::
6567
:maxdepth: 1

docs/src_trace.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ remote_url_field = "remote-url" # Need's field name for remote URL
77

88
[src_trace.projects.dcdc]
99
# Configuration for source tracing project "dcdc"
10-
comment_type = "cpp" # Type of the comment, only support C/C++ for now
1110
src_dir = "../tests/data/dcdc" # Relative path from conf.py to the source directory
1211
remote_url_pattern = "https://github.com/useblocks/sphinx-codelinks/blob/{commit}/{path}#L{line}" # URL pattern for remote source code
13-
exclude = ["dcdc/src/ubt/ubt.cpp"] # Exclude files from source tracing
14-
include = ["**/*.cpp", "**/*.hpp"] # Include files for source tracing
15-
gitignore = true # Respect .gitignore to filter files
1612

1713
[src_trace.projects.dcdc.oneline_comment_style]
1814
# Configuration for oneline comment style
@@ -27,3 +23,7 @@ needs_fields = [
2723
{ "name" = "links", "type" = "list[str]", "default" = [
2824
] },
2925
]
26+
27+
[src_trace.projects.src]
28+
src_dir = "../tests/doc_test/minimum_config"
29+
remote_url_pattern = "https://github.com/useblocks/sphinx-codelinks/blob/{commit}/{path}#L{line}"

src/sphinx_codelinks/cmd.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@app.command(no_args_is_help=True)
2828
def discover(
29-
root_dir: Annotated[
29+
src_dir: Annotated[
3030
Path,
3131
typer.Argument(
3232
...,
@@ -68,7 +68,7 @@ def discover(
6868
from sphinx_codelinks.source_discovery.source_discover import SourceDiscover
6969

7070
source_discover = SourceDiscover(
71-
root_dir=root_dir,
71+
src_dir=src_dir,
7272
exclude=exclude,
7373
include=include,
7474
file_types=file_types,
@@ -134,8 +134,8 @@ def vdoc(
134134
if oneline_errors:
135135
errors.appendleft("Invalid oneline comment style configuration:")
136136
errors.extend(oneline_errors)
137-
138-
src_discovery_dict, src_discovery_errors = build_src_discovery_dict(project_config)
137+
src_discovery_errors = []
138+
src_discovery_dict = build_src_discovery_dict(project_config)
139139
if src_discovery_dict:
140140
src_discovery_config = SourceDiscoveryConfig(**src_discovery_dict)
141141
else:
@@ -151,9 +151,9 @@ def vdoc(
151151

152152
from sphinx_codelinks.source_discovery.source_discover import SourceDiscover
153153

154-
src_root_dir = (config.parent / src_discovery_config.root_dir).resolve()
154+
src_dir = (config.parent / src_discovery_config.src_dir).resolve()
155155
source_discover = SourceDiscover(
156-
root_dir=src_root_dir,
156+
src_dir=src_dir,
157157
exclude=src_discovery_config.exclude,
158158
include=src_discovery_config.include,
159159
file_types=src_discovery_config.file_types,
@@ -164,7 +164,7 @@ def vdoc(
164164

165165
virtual_docs = VirtualDocs(
166166
src_files=source_discover.source_paths,
167-
src_dir=str(src_root_dir),
167+
src_dir=str(src_dir),
168168
output_dir=str(output_dir),
169169
oneline_comment_style=oneline_comment_style,
170170
comment_type=src_discovery_config.file_types[0]
@@ -178,7 +178,7 @@ def vdoc(
178178
typer.echo("The virtual documents are generated:")
179179
for v_doc in virtual_docs.virtual_docs:
180180
json_path = output_dir / v_doc.filepath.with_suffix(".json").relative_to(
181-
src_root_dir
181+
src_dir
182182
)
183183
typer.echo(json_path)
184184
else:

src/sphinx_codelinks/source_discovery/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class SourceDiscoveryConfigType(TypedDict, total=False):
9-
root_dir: Path
9+
src_dir: Path
1010
exclude: list[str]
1111
include: list[str]
1212
gitignore: bool
@@ -19,8 +19,8 @@ class SourceDiscoveryConfig:
1919
def field_names(cls) -> set[str]:
2020
return {item.name for item in fields(cls)}
2121

22-
root_dir: Path = field(
23-
default_factory=lambda: Path.cwd(), metadata={"schema": {"type": "string"}}
22+
src_dir: Path = field(
23+
default_factory=lambda: Path("./"), metadata={"schema": {"type": "string"}}
2424
)
2525
"""The root of the source directory."""
2626

@@ -41,7 +41,9 @@ def field_names(cls) -> set[str]:
4141

4242
file_types: list[str] = field(
4343
default_factory=lambda: ["c", "h", "cpp", "hpp"],
44-
metadata={"schema": {"type": "array", "items": {"type": "string"}}},
44+
metadata={
45+
"schema": {"type": "array", "items": {"enum": ["c", "h", "cpp", "hpp"]}}
46+
},
4547
)
4648
"""The file types to discover."""
4749

src/sphinx_codelinks/source_discovery/source_discover.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
class SourceDiscover:
1010
def __init__(
1111
self,
12-
root_dir: Path,
12+
src_dir: Path,
1313
exclude: list[str] | None = None,
1414
include: list[str] | None = None,
1515
gitignore: bool = True,
1616
file_types: list[str] | None = None,
1717
):
18-
self.root_path = root_dir
18+
self.root_path = src_dir
1919
self.exclude = exclude
2020
self.include = include
2121
# Only gitignore at source root is considered.

0 commit comments

Comments
 (0)