From 2d206767e556cb4791dc99575a2f5d103e21dfe7 Mon Sep 17 00:00:00 2001 From: Will Larson Date: Wed, 4 Jun 2008 11:54:50 +0900 Subject: [PATCH] Updated the slugify method to handle more invalid characters, be easier to extend, and to produce more appealing output. --- editor/views.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/editor/views.py b/editor/views.py index d93570a..9066790 100644 --- a/editor/views.py +++ b/editor/views.py @@ -28,11 +28,22 @@ from django.contrib.auth import views, authenticate - +CHARACTERS_TO_STRIP = """ .,!?'";:/\+=# """ def slugify(str): - # This could be substantially more robust, - # but for the moment it suffices. - return str.lower().replace(' ','-').replace('.','').replace(',','-').replace("'","").replace('"',"")[:95] + new_str = "" + replaced_previous = False + for char in str.lower(): + if char in CHARACTERS_TO_STRIP: + if replaced_previous: + pass + else: + new_str = new_str + "-" + replaced_previous = True + else: + replaced_previous = False + new_str = new_str + char + return new_str.strip("-") + def login(request): error_msg = u""