comparison 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
comparison
equal deleted inserted replaced
483:64e1cd71b30b 484:d4321317beae
87 root = app.config.get('site/root') 87 root = app.config.get('site/root')
88 if not uri.startswith(root): 88 if not uri.startswith(root):
89 raise Exception("URI '%s' is not a full URI." % uri) 89 raise Exception("URI '%s' is not a full URI." % uri)
90 90
91 pretty_urls = app.config.get('site/pretty_urls') 91 pretty_urls = app.config.get('site/pretty_urls')
92 trailing_slash = app.config.get('site/trailing_slash')
92 if not pretty_urls: 93 if not pretty_urls:
93 uri, ext = os.path.splitext(uri) 94 uri, ext = os.path.splitext(uri)
95 elif trailing_slash:
96 uri = uri.rstrip('/')
94 97
95 page_num = 1 98 page_num = 1
96 pgn_suffix_re = app.config.get('__cache/pagination_suffix_re') 99 pgn_suffix_re = app.config.get('__cache/pagination_suffix_re')
97 m = re.search(pgn_suffix_re, uri) 100 m = re.search(pgn_suffix_re, uri)
98 if m: 101 if m:
99 uri = uri[:m.start()] 102 uri = uri[:m.start()]
100 if uri == '':
101 uri = '/'
102 page_num = int(m.group('num')) 103 page_num = int(m.group('num'))
103 104
104 if not pretty_urls: 105 if len(uri) < len(root):
105 uri += ext 106 # The only reasons the URI could have gotten shorter are:
107 # - if the regexp "ate" the trailing slash of the root.
108 # - if we stripped the trailing slash on a root URL.
109 uri += '/'
110
111 if len(uri) > len(root):
112 # Now if we don't have a root URI, make it conform to the rules
113 # (re-add the extension, or re-add the trailing slash).
114 if not pretty_urls:
115 uri += ext
116 elif trailing_slash:
117 uri += '/'
106 118
107 return uri, page_num 119 return uri, page_num
108 120