Repository: mkdocs/mkdocs

Issue:
Regression with automatic pages config
Caused by: 7849e71b8401608f86e81b48e7b29d18e8408663

Discussion:
landscape-bot: [![Code Health](https://landscape.io/badge/159489/landscape.svg?style=flat)](https://landscape.io/diff/146595)
Repository health decreased by 0.24% when pulling **[f42c054](https://github.com/d0ugal/mkdocs/commit/f42c054a9b0fc86f7ed40de43fec8ebb53ab4191) on d0ugal:auto-pages** into **[7cc4764](https://github.com/mkdocs/mkdocs/commit/7cc47645d357fa96bf7197e30b519f331e77a3d5) on mkdocs:master**.

 * [1 new problem was found](https://landscape.io/diff/146595) (including 0 errors and 1 code smell).
 * No problems were fixed.
landscape-bot: [![Code Health](https://landscape.io/badge/159492/landscape.svg?style=flat)](https://landscape.io/diff/146599)
Code quality remained the same when pulling **[7fc3de2](https://github.com/d0ugal/mkdocs/commit/7fc3de2959a6c94556be4f0e9c5a0cf27d451318) on d0ugal:auto-pages** into **[7cc4764](https://github.com/mkdocs/mkdocs/commit/7cc47645d357fa96bf7197e30b519f331e77a3d5) on mkdocs:master**.
landscape-bot: [![Code Health](https://landscape.io/badge/159492/landscape.svg?style=flat)](https://landscape.io/diff/146599)
Code quality remained the same when pulling **[7fc3de2](https://github.com/d0ugal/mkdocs/commit/7fc3de2959a6c94556be4f0e9c5a0cf27d451318) on d0ugal:auto-pages** into **[7cc4764](https://github.com/mkdocs/mkdocs/commit/7cc47645d357fa96bf7197e30b519f331e77a3d5) on mkdocs:master**.
landscape-bot: [![Code Health](https://landscape.io/badge/159508/landscape.svg?style=flat)](https://landscape.io/diff/146619)
Code quality remained the same when pulling **[d49f3a0](https://github.com/d0ugal/mkdocs/commit/d49f3a09fb5431351d5bd975e71f40a3cd69cdd4) on d0ugal:auto-pages** into **[7cc4764](https://github.com/mkdocs/mkdocs/commit/7cc47645d357fa96bf7197e30b519f331e77a3d5) on mkdocs:master**.

Patch:
diff --git a/mkdocs.yml b/mkdocs.yml
index ad7ce968..930ccd95 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -4,19 +4,6 @@ site_description: Project documentation with Markdown.
 
 repo_url: https://github.com/mkdocs/mkdocs/
 
-pages:
-- Home: index.md
-- User Guide:
-    - Writing Your Docs: user-guide/writing-your-docs.md
-    - Styling Your Docs: user-guide/styling-your-docs.md
-    - Configuration: user-guide/configuration.md
-- About:
-    - License: about/license.md
-    - Release Notes: about/release-notes.md
-    - Contributing: about/contributing.md
-
-use_new_pages_structure: true
-
 markdown_extensions:
     toc:
         permalink: ""
diff --git a/mkdocs/config.py b/mkdocs/config.py
index 408e3393..71451244 100644
--- a/mkdocs/config.py
+++ b/mkdocs/config.py
@@ -145,6 +145,8 @@ def validate_config(user_config):
             elif utils.is_template_file(filename):
                 extra_templates.append(filename)
 
+    pages = utils.nest_paths(pages)
+
     if config['pages'] is None:
         config['pages'] = pages
     else:
diff --git a/mkdocs/nav.py b/mkdocs/nav.py
index 58b5bc14..d70628d3 100644
--- a/mkdocs/nav.py
+++ b/mkdocs/nav.py
@@ -22,13 +22,7 @@ def filename_to_title(filename):
     if utils.is_homepage(filename):
         return 'Home'
 
-    title = os.path.splitext(filename)[0]
-    title = title.replace('-', ' ').replace('_', ' ')
-    # Capitalize if the filename was all lowercase, otherwise leave it as-is.
-    if title.lower() == title:
-        title = title.capitalize()
-
-    return title
+    return utils.filename_to_title(filename)
 
 
 class SiteNavigation(object):
diff --git a/mkdocs/utils.py b/mkdocs/utils.py
index 971ce480..685e90aa 100644
--- a/mkdocs/utils.py
+++ b/mkdocs/utils.py
@@ -307,3 +307,60 @@ def get_theme_names():
     """Return a list containing all the names of all the builtin themes."""
 
     return os.listdir(os.path.join(os.path.dirname(__file__), 'themes'))
+
+
+def filename_to_title(filename):
+
+    title = os.path.splitext(filename)[0]
+    title = title.replace('-', ' ').replace('_', ' ')
+    # Capitalize if the filename was all lowercase, otherwise leave it as-is.
+    if title.lower() == title:
+        title = title.capitalize()
+
+    return title
+
+
+def find_or_create_node(branch, key):
+    """
+    Given a list, look for dictionary with a key matching key and return it's
+    value. If it doesn't exist, create it with the value of an empty list and
+    return that.
+    """
+
+    for node in branch:
+        if not isinstance(node, dict):
+            continue
+
+        if key in node:
+            return node[key]
+
+    new_branch = []
+    node = {key: new_branch}
+    branch.append(node)
+    return new_branch
+
+
+def nest_paths(paths):
+    """
+    Given a list of paths, convert them into a nested structure that will match
+    the pages config.
+    """
+    nested = []
+
+    for path in paths:
+
+        if os.path.sep not in path:
+            nested.append(path)
+            continue
+
+        directory, _ = os.path.split(path)
+        parts = directory.split(os.path.sep)
+
+        branch = nested
+        for part in parts:
+            part = filename_to_title(part)
+            branch = find_or_create_node(branch, part)
+
+        branch.append(path)
+
+    return nested


Test patch:
diff --git a/mkdocs/tests/utils_tests.py b/mkdocs/tests/utils_tests.py
index e2854187..41d066e6 100644
--- a/mkdocs/tests/utils_tests.py
+++ b/mkdocs/tests/utils_tests.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # coding: utf-8
 
+import os
 import unittest
 
 from mkdocs import nav, utils
@@ -108,3 +109,32 @@ class UtilsTests(unittest.TestCase):
             sorted(['flatly', 'cerulean', 'slate', 'bootstrap', 'yeti',
                     'spacelab', 'united', 'readable', 'simplex', 'mkdocs',
                     'cosmo', 'journal', 'cyborg', 'readthedocs', 'amelia']))
+
+    def test_nest_paths(self):
+
+        j = os.path.join
+
+        result = utils.nest_paths([
+            'index.md',
+            j('user-guide', 'configuration.md'),
+            j('user-guide', 'styling-your-docs.md'),
+            j('user-guide', 'writing-your-docs.md'),
+            j('about', 'contributing.md'),
+            j('about', 'license.md'),
+            j('about', 'release-notes.md'),
+        ])
+
+        self.assertEqual(
+            result,
+            [
+                'index.md',
+                {'User guide': [
+                    j('user-guide', 'configuration.md'),
+                    j('user-guide', 'styling-your-docs.md'),
+                    j('user-guide', 'writing-your-docs.md')]},
+                {'About': [
+                    j('about', 'contributing.md'),
+                    j('about', 'license.md'),
+                    j('about', 'release-notes.md')]}
+            ]
+        )