comparison piecrust/uriutil.py @ 329:422052d2e978

internal: Try handling URLs in a consistent way. * Now URLs passed to, and returned from, routes will always be absolute URLs, i.e. URLs including the site root. * Validate the site root at config loading time to make sure it starts and ends with a slash. * Get rid of unused stuff. * Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 31 Mar 2015 23:03:28 -0700
parents b7ab1b503510
children d4321317beae
comparison
equal deleted inserted replaced
328:2a5996e0d3ec 329:422052d2e978
72 reps = dict((re.escape(k), v) for k, v in replacements.items()) 72 reps = dict((re.escape(k), v) for k, v in replacements.items())
73 pattern = re.compile("|".join(list(reps.keys()))) 73 pattern = re.compile("|".join(list(reps.keys())))
74 return pattern.sub(lambda m: reps[re.escape(m.group(0))], text) 74 return pattern.sub(lambda m: reps[re.escape(m.group(0))], text)
75 75
76 76
77 def get_slug(app, uri): 77 def split_uri(app, uri):
78 site_root = app.config.get('site/root') 78 root = app.config.get('site/root')
79 uri = uri[len(site_root):] 79 uri_root = uri[:len(root)]
80 return uri.lstrip('/') 80 if uri_root != root:
81 raise Exception("URI '%s' is not a full URI." % uri)
82 uri = uri[len(root):]
83 return uri_root, uri
81 84
82 85
83 def split_sub_uri(app, uri): 86 def split_sub_uri(app, uri):
87 root = app.config.get('site/root')
88 if not uri.startswith(root):
89 raise Exception("URI '%s' is not a full URI." % uri)
90
84 pretty_urls = app.config.get('site/pretty_urls') 91 pretty_urls = app.config.get('site/pretty_urls')
85 if not pretty_urls: 92 if not pretty_urls:
86 uri, ext = os.path.splitext(uri) 93 uri, ext = os.path.splitext(uri)
87 94
88 page_num = 1 95 page_num = 1
95 page_num = int(m.group('num')) 102 page_num = int(m.group('num'))
96 103
97 if not pretty_urls: 104 if not pretty_urls:
98 uri += ext 105 uri += ext
99 106
100 return (uri, page_num) 107 return uri, page_num
101 108