changeset 1100:1ce67d2fae0a

routing: Fix URL generation bug with ugly URLs and index pages in sub-folders.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 17 Feb 2018 11:54:00 -0800
parents 07c23be08029
children 31113d52e8be
files piecrust/routing.py tests/bakes/test_ordered_source.yaml
diffstat 2 files changed, 64 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/routing.py	Sat Feb 17 11:53:03 2018 -0800
+++ b/piecrust/routing.py	Sat Feb 17 11:54:00 2018 -0800
@@ -23,6 +23,10 @@
     pass
 
 
+class InvalidRouteParameterError(Exception):
+    pass
+
+
 class RouteParameter(object):
     TYPE_STRING = 0
     TYPE_PATH = 1
@@ -111,8 +115,9 @@
         for p in self.supported_params:
             if p.param_name == name:
                 return p
-        raise Exception("No such supported route parameter '%s' in: %s" %
-                        (name, self.uri_pattern))
+        raise InvalidRouteParameterError(
+            "No such supported route parameter '%s' in: %s" %
+            (name, self.uri_pattern))
 
     def getParameterType(self, name):
         return self.getParameter(name).param_type
@@ -200,7 +205,11 @@
                 if suffix:
                     uri = base_uri + suffix + ext
                 else:
-                    uri = base_uri + ext
+                    # If we just have the extension to add to the URL, we
+                    # strip any trailing slash to prevent, say, the index
+                    # page of a source from generating an URL like
+                    # `subdir/.html`. Instead, it does `subdir.html`.
+                    uri = base_uri.rstrip('/') + ext
 
         uri = self.uri_root + urllib.parse.quote(uri)
 
@@ -255,7 +264,7 @@
             elif param_type == RouteParameter.TYPE_INT2:
                 return '%%(%s)02d' % name
             return '%%(%s)s' % name
-        except:
+        except InvalidRouteParameterError:
             known = [p.name for p in self.supported_params]
             raise Exception("Unknown route parameter '%s' for route '%s'. "
                             "Must be one of: %s'" %
@@ -282,7 +291,7 @@
     def _coerceRouteParameter(self, name, val):
         try:
             param_type = self.getParameterType(name)
-        except:
+        except InvalidRouteParameterError:
             # Unknown parameter... just leave it.
             return val
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/bakes/test_ordered_source.yaml	Sat Feb 17 11:54:00 2018 -0800
@@ -0,0 +1,50 @@
+---
+in:
+    manual/00__index.html: "my index"
+    manual/01_zzz.html: "zzz"
+    manual/02_aaa.html: "aaa"
+    manual/02_aaa/01_aaa_zzz: "aaa with zzz"
+    manual/02_aaa/02_aaa_bbb: "aaa with bbb"
+    manual/03_yyy.html: "yyy"
+config:
+    site:
+        pretty_urls: true
+        sources:
+            manual:
+                type: ordered
+                default_layout: none
+        routes:
+            - url: '/man/%slug%'
+              source: manual
+outfiles:
+    man/index.html: "my index"
+    man/aaa/index.html: "aaa"
+    man/zzz/index.html: "zzz"
+    man/yyy/index.html: "yyy"
+    man/aaa/aaa_zzz/index.html: "aaa with zzz"
+    man/aaa/aaa_bbb/index.html: "aaa with bbb"
+---
+in:
+    manual/00__index.html: "my index"
+    manual/01_zzz.html: "zzz"
+    manual/02_aaa.html: "aaa"
+    manual/02_aaa/01_aaa_zzz: "aaa with zzz"
+    manual/02_aaa/02_aaa_bbb: "aaa with bbb"
+    manual/03_yyy.html: "yyy"
+config:
+    site:
+        sources:
+            manual:
+                type: ordered
+                default_layout: none
+        routes:
+            - url: '/man/%slug%'
+              source: manual
+outfiles:
+    man.html: "my index"
+    man/aaa.html: "aaa"
+    man/zzz.html: "zzz"
+    man/yyy.html: "yyy"
+    man/aaa/aaa_zzz.html: "aaa with zzz"
+    man/aaa/aaa_bbb.html: "aaa with bbb"
+