Skip to content

Commit

Permalink
Allow to set a different dest_uri when creating a File
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed May 7, 2023
1 parent 2a232bf commit d5af642
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
22 changes: 16 additions & 6 deletions mkdocs/structure/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,24 @@ def dest_path(self, value):

page: Optional[Page]

def __init__(self, path: str, src_dir: str, dest_dir: str, use_directory_urls: bool) -> None:
def __init__(
self,
path: str,
src_dir: str,
dest_dir: str,
use_directory_urls: bool,
*,
dest_uri: Optional[str] = None,
) -> None:
self.page = None
self.src_path = path
self.abs_src_path = os.path.normpath(os.path.join(src_dir, self.src_path))
self.name = self._get_stem()
self.dest_uri = self._get_dest_path(use_directory_urls)
self.abs_dest_path = os.path.normpath(os.path.join(dest_dir, self.dest_path))
if dest_uri is None:
dest_uri = self._get_dest_path(use_directory_urls)
self.dest_uri = dest_uri
self.url = self._get_url(use_directory_urls)
self.abs_src_path = os.path.normpath(os.path.join(src_dir, self.src_uri))
self.abs_dest_path = os.path.normpath(os.path.join(dest_dir, self.dest_uri))

def __eq__(self, other) -> bool:
return (
Expand All @@ -207,10 +217,10 @@ def __repr__(self):
)

def _get_stem(self) -> str:
"""Return the name of the file without it's extension."""
"""Return the name of the file without its extension."""
filename = posixpath.basename(self.src_uri)
stem, ext = posixpath.splitext(filename)
return 'index' if stem in ('index', 'README') else stem
return 'index' if stem == 'README' else stem

def _get_dest_path(self, use_directory_urls: bool) -> str:
"""Return destination path based on source path."""
Expand Down
20 changes: 20 additions & 0 deletions mkdocs/tests/structure/file_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ def test_file_name_with_space(self):
self.assertEqual(f.url, 'foo%20bar.html')
self.assertEqual(f.name, 'foo bar')

def test_file_name_with_custom_dest_uri(self):
for use_directory_urls in True, False:
with self.subTest(use_directory_urls=use_directory_urls):
f = File(
'stuff/foo.md',
src_dir='/path/to/docs',
dest_dir='/path/to/site',
use_directory_urls=use_directory_urls,
dest_uri='stuff/1-foo/index.html',
)
self.assertEqual(f.src_uri, 'stuff/foo.md')
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/stuff/foo.md')
self.assertEqual(f.dest_uri, 'stuff/1-foo/index.html')
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/stuff/1-foo/index.html')
if use_directory_urls:
self.assertEqual(f.url, 'stuff/1-foo/')
else:
self.assertEqual(f.url, 'stuff/1-foo/index.html')
self.assertEqual(f.name, 'foo')

def test_files(self):
fs = [
File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True),
Expand Down

0 comments on commit d5af642

Please sign in to comment.