Repository: mkdocs/mkdocs

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



Discussion:

Resolving patch:
diff --git a/mkdocs/config.py b/mkdocs/config.py
--- 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
--- 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
--- 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
--- 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 @@ def test_get_themes(self):
             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')]}
+            ]
+        )