Skip to content

Commit

Permalink
fix: fail create file url on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1nj1 committed Sep 30, 2024
1 parent 61c203b commit 9848715
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
17 changes: 15 additions & 2 deletions src/mkdocs_gen_nav_plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import posixpath
from pathlib import Path

from mkdocs.plugins import BasePlugin
from mkdocs.config import config_options
Expand Down Expand Up @@ -38,9 +39,21 @@ def rename_item(self, path):
return os.path.join(parent_path, file)

def on_files(self, files, config):
"""
The files event is called after the files collection is populated from the
docs_dir. Use this event to add, remove, or alter files in the collection. Note
that Page objects have not yet been associated with the file objects in the
collection. Use Page Events to manipulate page specific data.
https://www.mkdocs.org/dev-guide/api/#mkdocs.structure.files.File
:param files:
:param config:
:return:
"""
for file in files:
file.dest_path = file.dest_uri = self.rename_item(file.dest_path)
file.url = self.rename_item(file.url)
file.dest_path = self.rename_item(file.dest_path)
file.dest_uri = Path(self.rename_item(file.dest_path)).as_posix()
file.url = Path(self.rename_item(file.url)).as_posix()
file.abs_dest_path = self.rename_item(file.abs_dest_path)
return files

Expand Down
79 changes: 66 additions & 13 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import pathlib
import posixpath
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import Mock

from mkdocs_gen_nav_plugin.plugin import GenNavPlugin
from tests.testutil import create_dirs_and_files_from_yaml
Expand Down Expand Up @@ -58,12 +60,12 @@ def test_create_nav_dict_listfiles(self):
result = self.plugin.create_nav_dict(self.root_path, self.root_path, '.md')
expected_result = [
{'Dir1': [
{'File1': posixpath.join('dir1', 'file1.md')},
{'File2': posixpath.join('dir1', 'file2.md')}
{'File1': 'dir1/file1.md'},
{'File2': 'dir1/file2.md'}
]},
{'Dir2': [
{'Sub Dir1': [
{'File3': posixpath.join('dir2', 'sub-dir1', 'file3.md')}
{'File3': 'dir2/sub-dir1/file3.md'}
]}
]}
]
Expand All @@ -85,12 +87,12 @@ def test_create_nav_dict_orderedList(self):
result = self.plugin.create_nav_dict(self.root_path, self.root_path, '.md')
expected_result = [
{'Dir2': [
{'File2': posixpath.join('01_dir2', 'file2.md')},
{'File3': posixpath.join('01_dir2', 'file3.md')}
{'File2': '01_dir2/file2.md'},
{'File3': '01_dir2/file3.md'}
]},
{'Dir1': [
{'File12': posixpath.join('02_dir1', '00_file12.md')},
{'File1': posixpath.join('02_dir1', '99_file1.md')}
{'File12': '02_dir1/00_file12.md'},
{'File1': '02_dir1/99_file1.md'}
]}
]
self.assertEqual(result, expected_result)
Expand All @@ -110,7 +112,7 @@ def test_create_nav_dict_index(self):
result = self.plugin.create_nav_dict(self.root_path, self.root_path, '.md')
expected_result = [
{'Blog': [
{'Index': posixpath.join('blog', 'index.md')}
{'Index': 'blog/index.md'}
]}
]
self.assertEqual(result, expected_result)
Expand All @@ -123,19 +125,70 @@ def test_create_nav_dict_example_with_material(self):
result = self.plugin.create_nav_dict(test_path, test_path, '.md')
expected_result = [
{'Blog': [
{'Index': posixpath.join('00_blog', 'index.md')}
{'Index': '00_blog/index.md'}
]},
{'A Second Menu': [
{'Index': posixpath.join('01_a-second-menu', 'index.md')}
{'Index': '01_a-second-menu/index.md'}
]},
{'C Menu': [
{'Index': posixpath.join('c-menu', 'index.md')},
{'Sub C Menu': posixpath.join('c-menu', 'sub-c-menu.md')}
{'Index': 'c-menu/index.md'},
{'Sub C Menu': 'c-menu/sub-c-menu.md'}
]},
{'D Menu': posixpath.join('d-menu.md')},
{'D Menu': 'd-menu.md'},
]
self.assertEqual(result, expected_result)

def test_on_files_with_valid_files(self):
files = [
Mock(dest_path="01_file.md", dest_uri="01_file.md", url="01_file.md",
abs_dest_path="/abs/path/01_file.md"),
Mock(dest_path="02_file.md", dest_uri="02_file.md", url="02_file.md",
abs_dest_path="/abs/path/02_file.md")
]
config = {}
result = self.plugin.on_files(files, config)
self.assertEqual(result[0].dest_path, "file.md")
self.assertEqual(result[0].dest_uri, "file.md")
self.assertEqual(result[0].url, "file.md")
self.assertEqual(result[0].abs_dest_path, "/abs/path/file.md")
self.assertEqual(result[1].dest_path, "file.md")
self.assertEqual(result[1].dest_uri, "file.md")
self.assertEqual(result[1].url, "file.md")
self.assertEqual(result[1].abs_dest_path, "/abs/path/file.md")

def test_on_files_with_no_files(self):
files = []
config = {}
result = self.plugin.on_files(files, config)
self.assertEqual(result, [])

def test_on_files_with_non_digit_prefix(self):
files = [
Mock(dest_path="ab_file.md", dest_uri="ab_file.md", url="ab_file.md",
abs_dest_path="/abs/path/ab_file.md")
]
config = {}
result = self.plugin.on_files(files, config)
self.assertEqual(result[0].dest_path, "ab_file.md")
self.assertEqual(result[0].dest_uri, "ab_file.md")
self.assertEqual(result[0].url, "ab_file.md")
self.assertEqual(result[0].abs_dest_path, "/abs/path/ab_file.md")

def test_on_files_with_posix_path_separator_on_url(self):
files = [
Mock(dest_path=os.path.join('dir', 'ab_file.md'),
dest_uri=posixpath.join('dir', 'ab_file.md'),
url=posixpath.join('dir', 'ab_file.md'),
abs_dest_path=os.path.join('abs', 'dir', 'ab_file.md'))
]
config = {}
result = self.plugin.on_files(files, config)
self.assertEqual(result[0].dest_path, os.path.join('dir', 'ab_file.md'))
self.assertEqual(result[0].dest_uri, "dir/ab_file.md")
self.assertEqual(result[0].url, "dir/ab_file.md")
self.assertEqual(result[0].abs_dest_path,
os.path.join('abs', 'dir', 'ab_file.md'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 9848715

Please sign in to comment.