Skip to content

Commit

Permalink
Fix error message on Windows (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored May 29, 2024
1 parent 73867ab commit eb65645
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
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 @@ def lineno_from_content_start(content: str, start: int) -> int:
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)
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 @@ def file_lineno_message(
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 @@ def found_include_tag( # noqa: PLR0912, PLR0915
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 @@ def found_include_markdown_tag( # noqa: PLR0912, PLR0915
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

0 comments on commit eb65645

Please sign in to comment.