From 1ad6a91e3ff04a7f61b3bf376dbca84c5169279a Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sun, 26 Apr 2020 15:34:05 -0700 Subject: [PATCH] Use README.md as index.html when use_directory_urls is false --- docs/about/release-notes.md | 1 + mkdocs/structure/files.py | 18 +++++++----------- mkdocs/tests/structure/file_tests.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/docs/about/release-notes.md b/docs/about/release-notes.md index c9ab8bff..4555e99c 100644 --- a/docs/about/release-notes.md +++ b/docs/about/release-notes.md @@ -23,6 +23,7 @@ The current and past members of the MkDocs team. ## Version 1.1.1 (in development) +* Bugfix: Use README.md as index.html even if use_directory_urls is false (#2081). * Bugfix: Ignore links which start with a backslash (#1680). * Bugfix: Pass `builder` to the `on_serve` event so that it can be passed to `server.watch` by plugins (#1952). diff --git a/mkdocs/structure/files.py b/mkdocs/structure/files.py index a693fadb..b0a71208 100644 --- a/mkdocs/structure/files.py +++ b/mkdocs/structure/files.py @@ -143,18 +143,14 @@ class File: def _get_dest_path(self, use_directory_urls): """ Return destination path based on source path. """ if self.is_documentation_page(): - if use_directory_urls: - parent, filename = os.path.split(self.src_path) - if self.name == 'index': - # index.md or README.md => index.html - return os.path.join(parent, 'index.html') - else: - # foo.md => foo/index.html - return os.path.join(parent, self.name, 'index.html') - else: + parent, filename = os.path.split(self.src_path) + if not use_directory_urls or self.name == 'index': + # index.md or README.md => index.html # foo.md => foo.html - root, ext = os.path.splitext(self.src_path) - return root + '.html' + return os.path.join(parent, self.name + '.html') + else: + # foo.md => foo/index.html + return os.path.join(parent, self.name, 'index.html') return self.src_path def _get_url(self, use_directory_urls): diff --git a/mkdocs/tests/structure/file_tests.py b/mkdocs/tests/structure/file_tests.py index 5712ed66..8aa2e206 100644 --- a/mkdocs/tests/structure/file_tests.py +++ b/mkdocs/tests/structure/file_tests.py @@ -122,6 +122,20 @@ class TestFiles(PathAssertionMixin, unittest.TestCase): self.assertFalse(f.is_javascript()) self.assertFalse(f.is_css()) + def test_md_readme_index_file(self): + f = File('README.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) + self.assertPathsEqual(f.src_path, 'README.md') + self.assertPathsEqual(f.abs_src_path, '/path/to/docs/README.md') + self.assertPathsEqual(f.dest_path, 'index.html') + self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html') + self.assertEqual(f.url, 'index.html') + self.assertEqual(f.name, 'index') + self.assertTrue(f.is_documentation_page()) + self.assertFalse(f.is_static_page()) + self.assertFalse(f.is_media_file()) + self.assertFalse(f.is_javascript()) + self.assertFalse(f.is_css()) + def test_md_index_file_use_directory_urls(self): f = File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) self.assertPathsEqual(f.src_path, 'index.md') @@ -136,6 +150,20 @@ class TestFiles(PathAssertionMixin, unittest.TestCase): self.assertFalse(f.is_javascript()) self.assertFalse(f.is_css()) + def test_md_readme_index_file_use_directory_urls(self): + f = File('README.md', '/path/to/docs', '/path/to/site', use_directory_urls=True) + self.assertPathsEqual(f.src_path, 'README.md') + self.assertPathsEqual(f.abs_src_path, '/path/to/docs/README.md') + self.assertPathsEqual(f.dest_path, 'index.html') + self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html') + self.assertEqual(f.url, '.') + self.assertEqual(f.name, 'index') + self.assertTrue(f.is_documentation_page()) + self.assertFalse(f.is_static_page()) + self.assertFalse(f.is_media_file()) + self.assertFalse(f.is_javascript()) + self.assertFalse(f.is_css()) + def test_md_index_file_nested(self): f = File('foo/index.md', '/path/to/docs', '/path/to/site', use_directory_urls=False) self.assertPathsEqual(f.src_path, 'foo/index.md')