Skip to content

Commit d5af642

Browse files
committed
Allow to set a different dest_uri when creating a File
1 parent 2a232bf commit d5af642

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

Diff for: mkdocs/structure/files.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,24 @@ def dest_path(self, value):
183183

184184
page: Optional[Page]
185185

186-
def __init__(self, path: str, src_dir: str, dest_dir: str, use_directory_urls: bool) -> None:
186+
def __init__(
187+
self,
188+
path: str,
189+
src_dir: str,
190+
dest_dir: str,
191+
use_directory_urls: bool,
192+
*,
193+
dest_uri: Optional[str] = None,
194+
) -> None:
187195
self.page = None
188196
self.src_path = path
189-
self.abs_src_path = os.path.normpath(os.path.join(src_dir, self.src_path))
190197
self.name = self._get_stem()
191-
self.dest_uri = self._get_dest_path(use_directory_urls)
192-
self.abs_dest_path = os.path.normpath(os.path.join(dest_dir, self.dest_path))
198+
if dest_uri is None:
199+
dest_uri = self._get_dest_path(use_directory_urls)
200+
self.dest_uri = dest_uri
193201
self.url = self._get_url(use_directory_urls)
202+
self.abs_src_path = os.path.normpath(os.path.join(src_dir, self.src_uri))
203+
self.abs_dest_path = os.path.normpath(os.path.join(dest_dir, self.dest_uri))
194204

195205
def __eq__(self, other) -> bool:
196206
return (
@@ -207,10 +217,10 @@ def __repr__(self):
207217
)
208218

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

215225
def _get_dest_path(self, use_directory_urls: bool) -> str:
216226
"""Return destination path based on source path."""

Diff for: mkdocs/tests/structure/file_tests.py

+20
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ def test_file_name_with_space(self):
339339
self.assertEqual(f.url, 'foo%20bar.html')
340340
self.assertEqual(f.name, 'foo bar')
341341

342+
def test_file_name_with_custom_dest_uri(self):
343+
for use_directory_urls in True, False:
344+
with self.subTest(use_directory_urls=use_directory_urls):
345+
f = File(
346+
'stuff/foo.md',
347+
src_dir='/path/to/docs',
348+
dest_dir='/path/to/site',
349+
use_directory_urls=use_directory_urls,
350+
dest_uri='stuff/1-foo/index.html',
351+
)
352+
self.assertEqual(f.src_uri, 'stuff/foo.md')
353+
self.assertPathsEqual(f.abs_src_path, '/path/to/docs/stuff/foo.md')
354+
self.assertEqual(f.dest_uri, 'stuff/1-foo/index.html')
355+
self.assertPathsEqual(f.abs_dest_path, '/path/to/site/stuff/1-foo/index.html')
356+
if use_directory_urls:
357+
self.assertEqual(f.url, 'stuff/1-foo/')
358+
else:
359+
self.assertEqual(f.url, 'stuff/1-foo/index.html')
360+
self.assertEqual(f.name, 'foo')
361+
342362
def test_files(self):
343363
fs = [
344364
File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True),

0 commit comments

Comments
 (0)