comparison piecrust/routing.py @ 515:16e705c58cae

internal: Improve handling of taxonomy term slugification. This paves the way to bring slugification options like transliteration to PieCrust. This change mostly makes sure we use one-way slugification, which means, for serving/previewing, we need to slugify each page's terms to compare to the stuff captured from the URL. It also simplifies things a bit in the code.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 26 Jul 2015 23:16:15 -0700
parents 64e1cd71b30b
children bab91fcef741
comparison
equal deleted inserted replaced
514:c9c305645e5f 515:16e705c58cae
40 def __init__(self, app, cfg): 40 def __init__(self, app, cfg):
41 self.app = app 41 self.app = app
42 42
43 self.source_name = cfg['source'] 43 self.source_name = cfg['source']
44 self.taxonomy_name = cfg.get('taxonomy') 44 self.taxonomy_name = cfg.get('taxonomy')
45 self.taxonomy_term_sep = cfg.get('term_separator', '/')
45 46
46 self.pretty_urls = app.config.get('site/pretty_urls') 47 self.pretty_urls = app.config.get('site/pretty_urls')
47 self.trailing_slash = app.config.get('site/trailing_slash') 48 self.trailing_slash = app.config.get('site/trailing_slash')
48 self.show_debug_info = app.config.get('site/show_debug_info') 49 self.show_debug_info = app.config.get('site/show_debug_info')
49 self.pagination_suffix_format = app.config.get( 50 self.pagination_suffix_format = app.config.get(
189 if self.show_debug_info: 190 if self.show_debug_info:
190 uri += '?!debug' 191 uri += '?!debug'
191 192
192 return uri 193 return uri
193 194
195 def getTaxonomyTerms(self, route_metadata):
196 if not self.is_taxonomy_route:
197 raise Exception("This route isn't a taxonomy route.")
198
199 tax = self.app.getTaxonomy(self.taxonomy_name)
200 all_values = route_metadata.get(tax.term_name)
201 if all_values is None:
202 raise Exception("'%s' values couldn't be found in route metadata" %
203 tax.term_name)
204
205 if self.taxonomy_term_sep in all_values:
206 return tuple(all_values.split(self.taxonomy_term_sep))
207 return all_values
208
194 def slugifyTaxonomyTerm(self, term): 209 def slugifyTaxonomyTerm(self, term):
195 #TODO: add options for transliterating and combining terms. 210 #TODO: add options for transliterating and combining terms.
196 if isinstance(term, tuple): 211 if isinstance(term, tuple):
197 return '/'.join(term) 212 return '/'.join(term)
198 return term 213 return term
199
200 def unslugifyTaxonomyTerm(self, term):
201 #TODO: same as above.
202 split_terms = term.split('/')
203 if len(split_terms) == 1:
204 return term
205 return tuple(split_terms)
206 214
207 def _uriFormatRepl(self, m): 215 def _uriFormatRepl(self, m):
208 name = m.group('name') 216 name = m.group('name')
209 #TODO: fix this hard-coded shit 217 #TODO: fix this hard-coded shit
210 if name == 'year': 218 if name == 'year':