From 73bd963be9c5d0e6e856d6f4dd6890ce04f067e4 Mon Sep 17 00:00:00 2001 From: Dougal Matthews Date: Thu, 2 Apr 2015 20:25:54 +0100 Subject: [PATCH] Change the order of the Navigation processing to improve child_titles The current code was incorrectly processing titles for pages when an explicit title was provided but the path to the file was nested in a directory. Fixes #392 --- mkdocs/nav.py | 11 +++++++---- mkdocs/tests/nav_tests.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/mkdocs/nav.py b/mkdocs/nav.py index c8257e126b..932399b400 100644 --- a/mkdocs/nav.py +++ b/mkdocs/nav.py @@ -209,14 +209,17 @@ def _generate_site_navigation(pages_config, url_context, use_directory_urls=True ) raise exceptions.ConfigurationError(msg) + # If both the title and child_title are None, then we + # have just been given a path. If that path contains a / + # then lets automatically nest it. + if title is None and child_title is None and os.path.sep in path: + filename = path.split(os.path.sep)[-1] + child_title = filename_to_title(filename) + if title is None: filename = path.split(os.path.sep)[0] title = filename_to_title(filename) - if child_title is None and os.path.sep in path: - filename = path.split(os.path.sep)[-1] - child_title = filename_to_title(filename) - url = utils.get_url_path(path, use_directory_urls) if not child_title: diff --git a/mkdocs/tests/nav_tests.py b/mkdocs/tests/nav_tests.py index 7013a66e5a..b6876f3593 100644 --- a/mkdocs/tests/nav_tests.py +++ b/mkdocs/tests/nav_tests.py @@ -63,6 +63,39 @@ def test_indented_toc(self): self.assertEqual(len(site_navigation.nav_items), 3) self.assertEqual(len(site_navigation.pages), 6) + def test_nested_ungrouped(self): + pages = [ + ('index.md', 'Home'), + ('about/contact.md', 'Contact'), + ('about/sub/license.md', 'License Title') + ] + expected = dedent(""" + Home - / + Contact - /about/contact/ + License Title - /about/sub/license/ + """) + site_navigation = nav.SiteNavigation(pages) + self.assertEqual(str(site_navigation).strip(), expected) + self.assertEqual(len(site_navigation.nav_items), 3) + self.assertEqual(len(site_navigation.pages), 3) + + def test_nested_ungrouped_no_titles(self): + pages = [ + ('index.md',), + ('about/contact.md'), + ('about/sub/license.md') + ] + expected = dedent(""" + Home - / + About + Contact - /about/contact/ + License - /about/sub/license/ + """) + site_navigation = nav.SiteNavigation(pages) + self.assertEqual(str(site_navigation).strip(), expected) + self.assertEqual(len(site_navigation.nav_items), 2) + self.assertEqual(len(site_navigation.pages), 3) + def test_walk_simple_toc(self): pages = [ ('index.md', 'Home'),