Skip to content

Commit eb65645

Browse files
authored
Fix error message on Windows (#210)
1 parent 73867ab commit eb65645

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ repos:
3333
- id: end-of-file-fixer
3434
name: end-of-file-fixer
3535
- repo: https://github.com/astral-sh/ruff-pre-commit
36-
rev: v0.4.2
36+
rev: v0.4.6
3737
hooks:
3838
- id: ruff
3939
args:
@@ -99,7 +99,7 @@ repos:
9999
- platformdirs
100100
- wcmatch
101101
- repo: https://github.com/tcort/markdown-link-check
102-
rev: v3.12.1
102+
rev: v3.12.2
103103
hooks:
104104
- id: markdown-link-check
105105
name: markdown-link-check

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mkdocs-include-markdown-plugin"
3-
version = "6.0.6"
3+
version = "6.0.7"
44
description = "Mkdocs Markdown includer plugin."
55
readme = "README.md"
66
license = "Apache-2.0"

src/mkdocs_include_markdown_plugin/event.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def lineno_from_content_start(content: str, start: int) -> int:
5858
return content[:start].count('\n') + 1
5959

6060

61+
def safe_os_path_relpath(path: str, start: str) -> str:
62+
"""Return the relative path of a file from a start directory.
63+
64+
Safe version of `os.path.relpath` that catches `ValueError` exceptions
65+
on Windows and returns the original path in case of error.
66+
On Windows, `ValueError` is raised when `path` and `start` are on
67+
different drives.
68+
"""
69+
if os.name != 'nt': # pragma: nt no cover
70+
return os.path.relpath(path, start)
71+
try: # pragma: nt cover
72+
return os.path.relpath(path, start)
73+
except ValueError: # pragma: no cover
74+
return path
75+
76+
6177
def file_lineno_message(
6278
page_src_path: str | None,
6379
docs_dir: str,
@@ -67,7 +83,7 @@ def file_lineno_message(
6783
if page_src_path is None: # pragma: no cover
6884
return f'generated page content (line {lineno})'
6985
return (
70-
f'{os.path.relpath(page_src_path, docs_dir)}'
86+
f'{safe_os_path_relpath(page_src_path, docs_dir)}'
7187
f':{lineno}'
7288
)
7389

@@ -287,7 +303,7 @@ def found_include_tag( # noqa: PLR0912, PLR0915
287303
if expected_but_any_found[i]:
288304
delimiter_value = locals()[delimiter_name]
289305
readable_files_to_include = ', '.join([
290-
os.path.relpath(fpath, docs_dir)
306+
safe_os_path_relpath(fpath, docs_dir)
291307
for fpath in file_paths_to_include
292308
])
293309
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''
@@ -593,7 +609,7 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
593609
if expected_but_any_found[i]:
594610
delimiter_value = locals()[delimiter_name]
595611
readable_files_to_include = ', '.join([
596-
os.path.relpath(fpath, docs_dir)
612+
safe_os_path_relpath(fpath, docs_dir)
597613
for fpath in file_paths_to_include
598614
])
599615
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''

0 commit comments

Comments
 (0)