changeset 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 64e696adb99e
children fd8bd08d1fa8
files piecrust/app.py piecrust/routing.py tests/bakes/test_unicode_tags.yaml tests/servings/test_unicode_tags.yaml
diffstat 4 files changed, 136 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/app.py	Fri Aug 14 22:44:25 2015 -0700
+++ b/piecrust/app.py	Fri Aug 14 22:46:06 2015 -0700
@@ -29,7 +29,7 @@
 logger = logging.getLogger(__name__)
 
 
-CACHE_VERSION = 21
+CACHE_VERSION = 22
 
 
 class VariantNotFoundError(Exception):
@@ -122,6 +122,7 @@
                     ('textile', 'textile')]),
                 'default_auto_format': 'md',
                 'pagination_suffix': '/%num%',
+                'slugify_mode': 'encode',
                 'plugins': None,
                 'themes_sources': [DEFAULT_THEME_SOURCE],
                 'cache_time': 28800,
--- a/piecrust/routing.py	Fri Aug 14 22:44:25 2015 -0700
+++ b/piecrust/routing.py	Fri Aug 14 22:46:06 2015 -0700
@@ -42,10 +42,12 @@
 SLUGIFY_TRANSLITERATE = 2
 SLUGIFY_LOWERCASE = 4
 SLUGIFY_DOT_TO_DASH = 8
+SLUGIFY_SPACE_TO_DASH = 16
 
 
 re_first_dot_to_dash = re.compile(r'^\.+')
 re_dot_to_dash = re.compile(r'\.+')
+re_space_to_dash = re.compile(r'\s+')
 
 
 def _parse_slugify_mode(value):
@@ -53,7 +55,8 @@
             'encode': SLUGIFY_ENCODE,
             'transliterate': SLUGIFY_TRANSLITERATE,
             'lowercase': SLUGIFY_LOWERCASE,
-            'dot_to_dash': SLUGIFY_DOT_TO_DASH}
+            'dot_to_dash': SLUGIFY_DOT_TO_DASH,
+            'space_to_dash': SLUGIFY_SPACE_TO_DASH}
     mode = 0
     for v in value.split(','):
         f = mapping.get(v.strip())
@@ -77,8 +80,11 @@
         self.source_name = cfg['source']
         self.taxonomy_name = cfg.get('taxonomy')
         self.taxonomy_term_sep = cfg.get('term_separator', '/')
-        self.slugify_mode = _parse_slugify_mode(
-                cfg.get('slugify_mode', 'encode,lowercase'))
+
+        sm = cfg.get('slugify_mode')
+        if not sm:
+            sm = app.config.get('site/slugify_mode', 'encode')
+        self.slugify_mode = _parse_slugify_mode(sm)
 
         self.pretty_urls = app.config.get('site/pretty_urls')
         self.trailing_slash = app.config.get('site/trailing_slash')
@@ -256,6 +262,8 @@
         if self.slugify_mode & SLUGIFY_DOT_TO_DASH:
             term = re_first_dot_to_dash.sub('', term)
             term = re_dot_to_dash.sub('-', term)
+        if self.slugify_mode & SLUGIFY_SPACE_TO_DASH:
+            term = re_space_to_dash.sub('-', term)
         return term
 
     def _uriFormatRepl(self, m):
--- a/tests/bakes/test_unicode_tags.yaml	Fri Aug 14 22:44:25 2015 -0700
+++ b/tests/bakes/test_unicode_tags.yaml	Fri Aug 14 22:46:06 2015 -0700
@@ -39,6 +39,55 @@
     pages/_index.md: ''
 outfiles:
     tag/это тэг.html: |
-      Pages in /tag/%D1%8D%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
+      Pages in /tag/%D0%AD%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
+      Post 01
+---
+config:
+    site:
+        slugify_mode: lowercase,transliterate
+in:
+    posts/2015-03-01_post01.md: |
+      ---
+      title: Post 01
+      tags: [étrange]
+      ---
+    posts/2015-03-02_post02.md: |
+      ---
+      title: Post 02
+      tags: [étrange, sévère]
+      ---
+    pages/_tag.md: |
+      Pages in {{pctagurl(tag)}}
+      {% for p in pagination.posts -%}
+      {{p.title}}
+      {% endfor %}
+    pages/_index.md: ''
+outfiles:
+    tag/etrange.html: |
+      Pages in /tag/etrange.html
+      Post 02
+      Post 01
+    tag/severe.html: |
+      Pages in /tag/severe.html
+      Post 02
+---
+config:
+    site:
+        slugify_mode: lowercase,transliterate,space_to_dash
+in:
+    posts/2015-03-01_post01.md: |
+      ---
+      title: Post 01
+      tags: [Это тэг]
+      ---
+    pages/_tag.md: |
+      Pages in {{pctagurl(tag)}}
+      {% for p in pagination.posts -%}
+      {{p.title}}
+      {% endfor %}
+    pages/_index.md: ''
+outfiles:
+    tag/eto-teg.html: |
+      Pages in /tag/eto-teg.html
       Post 01
 
--- a/tests/servings/test_unicode_tags.yaml	Fri Aug 14 22:44:25 2015 -0700
+++ b/tests/servings/test_unicode_tags.yaml	Fri Aug 14 22:46:06 2015 -0700
@@ -44,7 +44,7 @@
     Pages in /tag/s%C3%A9v%C3%A8re.html
     Post 02
 ---
-url: /tag/%D1%8D%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
+url: /tag/%D0%AD%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
 in:
     posts/2015-03-01_post01.md: |
       ---
@@ -58,6 +58,77 @@
       {% endfor %}
     pages/_index.md: ''
 out: |
-    Pages in /tag/%D1%8D%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
+    Pages in /tag/%D0%AD%D1%82%D0%BE%20%D1%82%D1%8D%D0%B3.html
+    Post 01
+---
+config:
+    site:
+        slugify_mode: lowercase,transliterate
+url: /tag/etrange.html
+in:
+    posts/2015-03-01_post01.md: |
+      ---
+      title: Post 01
+      tags: [étrange]
+      ---
+    posts/2015-03-02_post02.md: |
+      ---
+      title: Post 02
+      tags: [étrange, sévère]
+      ---
+    pages/_tag.md: |
+      Pages in {{pctagurl(tag)}}
+      {% for p in pagination.posts -%}
+      {{p.title}}
+      {% endfor %}
+    pages/_index.md: ''
+out: |
+    Pages in /tag/etrange.html
+    Post 02
+    Post 01
+---
+config:
+    site:
+        slugify_mode: lowercase,transliterate
+url: /tag/severe.html
+in:
+    posts/2015-03-01_post01.md: |
+      ---
+      title: Post 01
+      tags: [étrange]
+      ---
+    posts/2015-03-02_post02.md: |
+      ---
+      title: Post 02
+      tags: [étrange, sévère]
+      ---
+    pages/_tag.md: |
+      Pages in {{pctagurl(tag)}}
+      {% for p in pagination.posts -%}
+      {{p.title}}
+      {% endfor %}
+    pages/_index.md: ''
+out: |
+    Pages in /tag/severe.html
+    Post 02
+---
+config:
+    site:
+        slugify_mode: lowercase,transliterate,space_to_dash
+url: /tag/eto-teg.html
+in:
+    posts/2015-03-01_post01.md: |
+      ---
+      title: Post 01
+      tags: [Это тэг]
+      ---
+    pages/_tag.md: |
+      Pages in {{pctagurl(tag)}}
+      {% for p in pagination.posts -%}
+      {{p.title}}
+      {% endfor %}
+    pages/_index.md: ''
+out: |
+    Pages in /tag/eto-teg.html
     Post 01