From 65198dc74a90894cc860e2838150e13d643ed13d Mon Sep 17 00:00:00 2001 From: Friedrich Lindenberg Date: Fri, 15 Nov 2013 22:48:38 +0200 Subject: [PATCH] Use Python-slugify to generate Freezefile slugs. This lets me remove a lot of unneeded code from the utils, and the dependency improves the quality a lot. --- dataset/freeze/format/common.py | 5 ++-- dataset/util.py | 42 --------------------------------- 2 files changed, 3 insertions(+), 44 deletions(-) diff --git a/dataset/freeze/format/common.py b/dataset/freeze/format/common.py index 043025a..8b9859a 100644 --- a/dataset/freeze/format/common.py +++ b/dataset/freeze/format/common.py @@ -3,7 +3,8 @@ import logging import re import locale -from dataset.util import FreezeException, slug +from dataset.util import FreezeException +from slugify import slugify TMPL_KEY = re.compile("{{([^}]*)}}") @@ -11,7 +12,7 @@ TMPL_KEY = re.compile("{{([^}]*)}}") OPERATIONS = { 'identity': lambda x: x, 'lower': lambda x: unicode(x).lower(), - 'slug': slug + 'slug': slugify } diff --git a/dataset/util.py b/dataset/util.py index 339a5fa..1f8083c 100644 --- a/dataset/util.py +++ b/dataset/util.py @@ -10,45 +10,3 @@ class DatasetException(Exception): class FreezeException(DatasetException): pass - -def normalize(text): - """ Simplify a piece of text to generate a more canonical - representation. This involves lowercasing, stripping trailing - spaces, removing symbols, diacritical marks (umlauts) and - converting all newlines etc. to single spaces. - """ - if not isinstance(text, unicode): - text = unicode(text) - text = text.lower() - decomposed = ucnorm('NFKD', text) - filtered = [] - for char in decomposed: - cat = category(char) - if cat.startswith('C'): - filtered.append(' ') - elif cat.startswith('M'): - # marks, such as umlauts - continue - elif cat.startswith('Z'): - # newlines, non-breaking etc. - filtered.append(' ') - elif cat.startswith('S'): - # symbols, such as currency - continue - else: - filtered.append(char) - text = u''.join(filtered) - while ' ' in text: - text = text.replace(' ', ' ') - text = text.strip() - return ucnorm('NFKC', text) - -def slug(text): - """ Create a version of a string convenient for use in a URL - or file name. """ - text = normalize(text) - text = text.replace(u'ß', 'ss') - text = '-'.join(filter(lambda t: len(t), \ - SLUG_REMOVE.split(text))) - return text.lower() -