diff piecrust/uriutil.py @ 484:d4321317beae

internal: Correctly split sub URIs. Add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 22 Jul 2015 00:07:25 -0700
parents 422052d2e978
children 4284c673bb91
line wrap: on
line diff
--- a/piecrust/uriutil.py	Tue Jul 21 22:42:47 2015 -0700
+++ b/piecrust/uriutil.py	Wed Jul 22 00:07:25 2015 -0700
@@ -89,20 +89,32 @@
         raise Exception("URI '%s' is not a full URI." % uri)
 
     pretty_urls = app.config.get('site/pretty_urls')
+    trailing_slash = app.config.get('site/trailing_slash')
     if not pretty_urls:
         uri, ext = os.path.splitext(uri)
+    elif trailing_slash:
+        uri = uri.rstrip('/')
 
     page_num = 1
     pgn_suffix_re = app.config.get('__cache/pagination_suffix_re')
     m = re.search(pgn_suffix_re, uri)
     if m:
         uri = uri[:m.start()]
-        if uri == '':
-            uri = '/'
         page_num = int(m.group('num'))
 
-    if not pretty_urls:
-        uri += ext
+    if len(uri) < len(root):
+        # The only reasons the URI could have gotten shorter are:
+        # - if the regexp "ate" the trailing slash of the root.
+        # - if we stripped the trailing slash on a root URL.
+        uri += '/'
+
+    if len(uri) > len(root):
+        # Now if we don't have a root URI, make it conform to the rules
+        # (re-add the extension, or re-add the trailing slash).
+        if not pretty_urls:
+            uri += ext
+        elif trailing_slash:
+            uri += '/'
 
     return uri, page_num