comparison piecrust/routing.py @ 561:624559e72d3b

routes: Add better support for taxonomy slugification. * The `site/slugify_mode` can change slugification mode for all sources in one go. * Add support for replacing spaces with dashes. * Add tests.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 14 Aug 2015 22:46:06 -0700
parents daf8df5ade7d
children 6b6c5442c790
comparison
equal deleted inserted replaced
560:64e696adb99e 561:624559e72d3b
40 40
41 SLUGIFY_ENCODE = 1 41 SLUGIFY_ENCODE = 1
42 SLUGIFY_TRANSLITERATE = 2 42 SLUGIFY_TRANSLITERATE = 2
43 SLUGIFY_LOWERCASE = 4 43 SLUGIFY_LOWERCASE = 4
44 SLUGIFY_DOT_TO_DASH = 8 44 SLUGIFY_DOT_TO_DASH = 8
45 SLUGIFY_SPACE_TO_DASH = 16
45 46
46 47
47 re_first_dot_to_dash = re.compile(r'^\.+') 48 re_first_dot_to_dash = re.compile(r'^\.+')
48 re_dot_to_dash = re.compile(r'\.+') 49 re_dot_to_dash = re.compile(r'\.+')
50 re_space_to_dash = re.compile(r'\s+')
49 51
50 52
51 def _parse_slugify_mode(value): 53 def _parse_slugify_mode(value):
52 mapping = { 54 mapping = {
53 'encode': SLUGIFY_ENCODE, 55 'encode': SLUGIFY_ENCODE,
54 'transliterate': SLUGIFY_TRANSLITERATE, 56 'transliterate': SLUGIFY_TRANSLITERATE,
55 'lowercase': SLUGIFY_LOWERCASE, 57 'lowercase': SLUGIFY_LOWERCASE,
56 'dot_to_dash': SLUGIFY_DOT_TO_DASH} 58 'dot_to_dash': SLUGIFY_DOT_TO_DASH,
59 'space_to_dash': SLUGIFY_SPACE_TO_DASH}
57 mode = 0 60 mode = 0
58 for v in value.split(','): 61 for v in value.split(','):
59 f = mapping.get(v.strip()) 62 f = mapping.get(v.strip())
60 if f is None: 63 if f is None:
61 if v == 'iconv': 64 if v == 'iconv':
75 self.app = app 78 self.app = app
76 79
77 self.source_name = cfg['source'] 80 self.source_name = cfg['source']
78 self.taxonomy_name = cfg.get('taxonomy') 81 self.taxonomy_name = cfg.get('taxonomy')
79 self.taxonomy_term_sep = cfg.get('term_separator', '/') 82 self.taxonomy_term_sep = cfg.get('term_separator', '/')
80 self.slugify_mode = _parse_slugify_mode( 83
81 cfg.get('slugify_mode', 'encode,lowercase')) 84 sm = cfg.get('slugify_mode')
85 if not sm:
86 sm = app.config.get('site/slugify_mode', 'encode')
87 self.slugify_mode = _parse_slugify_mode(sm)
82 88
83 self.pretty_urls = app.config.get('site/pretty_urls') 89 self.pretty_urls = app.config.get('site/pretty_urls')
84 self.trailing_slash = app.config.get('site/trailing_slash') 90 self.trailing_slash = app.config.get('site/trailing_slash')
85 self.show_debug_info = app.config.get('site/show_debug_info') 91 self.show_debug_info = app.config.get('site/show_debug_info')
86 self.pagination_suffix_format = app.config.get( 92 self.pagination_suffix_format = app.config.get(
254 if self.slugify_mode & SLUGIFY_LOWERCASE: 260 if self.slugify_mode & SLUGIFY_LOWERCASE:
255 term = term.lower() 261 term = term.lower()
256 if self.slugify_mode & SLUGIFY_DOT_TO_DASH: 262 if self.slugify_mode & SLUGIFY_DOT_TO_DASH:
257 term = re_first_dot_to_dash.sub('', term) 263 term = re_first_dot_to_dash.sub('', term)
258 term = re_dot_to_dash.sub('-', term) 264 term = re_dot_to_dash.sub('-', term)
265 if self.slugify_mode & SLUGIFY_SPACE_TO_DASH:
266 term = re_space_to_dash.sub('-', term)
259 return term 267 return term
260 268
261 def _uriFormatRepl(self, m): 269 def _uriFormatRepl(self, m):
262 name = m.group('name') 270 name = m.group('name')
263 #TODO: fix this hard-coded shit 271 #TODO: fix this hard-coded shit