Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message on Windows #210

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repos:
- id: end-of-file-fixer
name: end-of-file-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
rev: v0.4.6
hooks:
- id: ruff
args:
Expand Down Expand Up @@ -99,7 +99,7 @@ repos:
- platformdirs
- wcmatch
- repo: https://github.com/tcort/markdown-link-check
rev: v3.12.1
rev: v3.12.2
hooks:
- id: markdown-link-check
name: markdown-link-check
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mkdocs-include-markdown-plugin"
version = "6.0.6"
version = "6.0.7"
description = "Mkdocs Markdown includer plugin."
readme = "README.md"
license = "Apache-2.0"
Expand Down
22 changes: 19 additions & 3 deletions src/mkdocs_include_markdown_plugin/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@
return content[:start].count('\n') + 1


def safe_os_path_relpath(path: str, start: str) -> str:
"""Return the relative path of a file from a start directory.

Safe version of `os.path.relpath` that catches `ValueError` exceptions
on Windows and returns the original path in case of error.
On Windows, `ValueError` is raised when `path` and `start` are on
different drives.
"""
if os.name != 'nt': # pragma: nt no cover
return os.path.relpath(path, start)
try: # pragma: nt cover
return os.path.relpath(path, start)

Check warning on line 72 in src/mkdocs_include_markdown_plugin/event.py

View check run for this annotation

Codecov / codecov/patch

src/mkdocs_include_markdown_plugin/event.py#L71-L72

Added lines #L71 - L72 were not covered by tests
except ValueError: # pragma: no cover
return path


def file_lineno_message(
page_src_path: str | None,
docs_dir: str,
Expand All @@ -67,7 +83,7 @@
if page_src_path is None: # pragma: no cover
return f'generated page content (line {lineno})'
return (
f'{os.path.relpath(page_src_path, docs_dir)}'
f'{safe_os_path_relpath(page_src_path, docs_dir)}'
f':{lineno}'
)

Expand Down Expand Up @@ -287,7 +303,7 @@
if expected_but_any_found[i]:
delimiter_value = locals()[delimiter_name]
readable_files_to_include = ', '.join([
os.path.relpath(fpath, docs_dir)
safe_os_path_relpath(fpath, docs_dir)
for fpath in file_paths_to_include
])
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''
Expand Down Expand Up @@ -593,7 +609,7 @@
if expected_but_any_found[i]:
delimiter_value = locals()[delimiter_name]
readable_files_to_include = ', '.join([
os.path.relpath(fpath, docs_dir)
safe_os_path_relpath(fpath, docs_dir)
for fpath in file_paths_to_include
])
plural_suffix = 's' if len(file_paths_to_include) > 1 else ''
Expand Down
Loading