Skip to content

Commit 57f3f47

Browse files
authored
used rel path for local url (#27)
* used rel path for local url * used rel path for doc link * updated docs * updated docs * updated doc string * removed commented code * fixed typo
1 parent bb0461b commit 57f3f47

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

docs/source/components/directive.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,4 @@ The needs defined in source code are extracted and rendered to:
7575
:project: dcdc
7676
:directory: ./discharge
7777

78-
.. note:: **local-url** is not working on the website as it only supports local browsing.
79-
8078
To have a more customized configuration of ``CodeLinks``, please refer to :ref:`configuration <configuration>`.

docs/source/development/change_log.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
Changelog
44
=========
55

6+
.. _`unreleased`:
7+
8+
Unreleased
9+
----------
10+
11+
Fixes
12+
.....
13+
14+
- 🐛 Replace absolute path with relative path to fix ``local-url`` not working on the non-local environment
15+
616
.. _`release:1.0.0`:
717

818
1.0.0
@@ -26,7 +36,7 @@ New and Improved
2636

2737
- ✨ Added a new ``write rst`` CLI command.
2838

29-
The ``write rst`` command write a reStructuredText file with :external+needs:ref:`needextend <needextend>` directive from the extracted markers generated by ``analyse``.
39+
The ``write rst`` command writes a reStructuredText file with :external+needs:ref:`needextend <needextend>` directive from the extracted markers generated by ``analyse``.
3040
The generated RST can be included in the Sphinx documentation to create the source code links in the existing needs
3141

3242
- 🔨 Replaced ``virtual_docs`` with the new ``analyse`` module.
@@ -53,7 +63,7 @@ New and Improved
5363
Fixes
5464
.....
5565

56-
- 🐛 Applying default configuration value when not given
66+
- 🐛 Apply default configuration values when not given
5767

5868
When a user does not specify certain configuration options, the extension will automatically use predefined default
5969
values, allowing users to get started quickly without needing to customize every option.

src/sphinx_codelinks/sphinx_extension/directives/src_trace.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
logger = logging.getLogger(__name__)
3434

3535

36+
def get_rel_path(doc_path: Path, code_path: Path, base_dir: Path) -> tuple[Path, Path]:
37+
"""Get the relative path from the document to the source code file and vice versa."""
38+
doc_depth = len(doc_path.parents) - 1
39+
src_rel_path = Path(*[".."] * doc_depth) / code_path.relative_to(base_dir)
40+
code_depth = len(code_path.relative_to(base_dir).parents) - 1
41+
doc_rel_path = Path(*[".."] * code_depth) / doc_path
42+
return src_rel_path, doc_rel_path.with_suffix(".html")
43+
44+
3645
def generate_str_link_name(
3746
oneline_need: OneLineNeed,
3847
target_filepath: Path,
@@ -145,7 +154,7 @@ def run(self) -> list[nodes.Node]:
145154
to_remove_str = to_remove_str.replace("\\", "\\\\")
146155
self.env.config.needs_string_links[local_url_field] = {
147156
"regex": r"^(?P<value>.+?)\.[^\.]+#L(?P<lineno>\d+)",
148-
"link_url": ("file://{{value}}.html#L-{{lineno}}"),
157+
"link_url": ("{{value}}.html#L-{{lineno}}"),
149158
"link_name": f"{{{{value | replace('{to_remove_str}', '')}}}}#L{{{{lineno}}}}",
150159
"options": [local_url_field],
151160
}
@@ -259,7 +268,7 @@ def render_needs(
259268
# mapping between lineno and need link in docs for local url
260269

261270
# The link to the documentation page for the source file
262-
docs_href = f"{dirs['out_dir'] / self.env.docname}.html"
271+
263272
if local_url_field:
264273
# copy files to _build/html
265274
target_filepath.parent.mkdir(parents=True, exist_ok=True)
@@ -268,8 +277,15 @@ def render_needs(
268277
remote_link_name = None
269278
if local_url_field:
270279
# generate link name
280+
# calculate the relative path from the current doc to the target file
281+
local_rel_path, docs_href = get_rel_path(
282+
Path(self.env.docname), target_filepath, dirs["out_dir"]
283+
)
271284
local_link_name = generate_str_link_name(
272-
oneline_need, target_filepath, dirs, local=True
285+
oneline_need,
286+
local_rel_path,
287+
dirs,
288+
local=True,
273289
)
274290
if remote_url_field:
275291
remote_link_name = generate_str_link_name(

0 commit comments

Comments
 (0)