comparison piecrust/routing.py @ 262:61145dcd56e0

routing: Better generate URLs according to the site configuration. This fixes problems now that even the chef server generates URLs that are appropriate given the `pretty_urls` and `trailing_slash` settings. Add some tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 24 Feb 2015 08:06:25 -0800
parents da5e6e00fb41
children 422052d2e978
comparison
equal deleted inserted replaced
261:b51ddb0c260b 262:61145dcd56e0
112 112
113 uri = self.uri_format % source_metadata 113 uri = self.uri_format % source_metadata
114 suffix = None 114 suffix = None
115 if sub_num > 1: 115 if sub_num > 1:
116 # Note that we know the pagination suffix starts with a slash. 116 # Note that we know the pagination suffix starts with a slash.
117 suffix = self.pagination_suffix_format % sub_num 117 suffix = self.pagination_suffix_format % {'num': sub_num}
118 118
119 if self.pretty_urls: 119 if self.pretty_urls:
120 # Output will be: 120 # Output will be:
121 # - `subdir/name` 121 # - `subdir/name`
122 # - `subdir/name/2` 122 # - `subdir/name/2`
123 # - `subdir/name.ext` 123 # - `subdir/name.ext`
124 # - `subdir/name.ext/2` 124 # - `subdir/name.ext/2`
125 if suffix: 125 if suffix:
126 uri = uri.rstrip('/') + suffix 126 if uri == '':
127 uri = suffix.lstrip('/')
128 else:
129 uri = uri.rstrip('/') + suffix
127 if self.trailing_slash: 130 if self.trailing_slash:
128 uri = uri.rstrip('/') + '/' 131 uri = uri.rstrip('/') + '/'
129 else: 132 else:
130 # Output will be: 133 # Output will be:
131 # - `subdir/name.html` 134 # - `subdir/name.html`
132 # - `subdir/name/2.html` 135 # - `subdir/name/2.html`
133 # - `subdir/name.ext` 136 # - `subdir/name.ext`
134 # - `subdir/name/2.ext` 137 # - `subdir/name/2.ext`
135 base_uri, ext = os.path.splitext(uri) 138 if uri == '':
136 if not ext: 139 if suffix:
137 ext = '.html' 140 uri = suffix.lstrip('/') + '.html'
138 if suffix:
139 uri = base_uri + suffix + ext
140 else: 141 else:
141 uri = base_uri + ext 142 base_uri, ext = os.path.splitext(uri)
143 if not ext:
144 ext = '.html'
145 if suffix:
146 uri = base_uri + suffix + ext
147 else:
148 uri = base_uri + ext
142 149
143 if include_site_root: 150 if include_site_root:
144 uri = self.uri_root + uri 151 uri = self.uri_root + uri
145 152
146 return uri 153 return uri