Reputation: 23675
Basically, my question is the same as this but in Python (and GAE), not C#.
Requirements:
Upvotes: 2
Views: 2767
Reputation: 7644
As alternative (and probably more tested version) I'm suggesting you use the (minimal modified) slugify code from Django:
import unicodedata
import re
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '-', value)
See: https://github.com/django/django/blob/master/django/utils/text.py#L435
Upvotes: 3
Reputation: 226181
def ToSeoFriendly(s, maxlen):
'''Join with dashes, eliminate punction, clip to maxlen, lowercase.
>>> ToSeoFriendly("The quick. brown4 fox jumped", 14)
'the-quick-brow'
'''
t = '-'.join(s.split()) # join words with dashes
u = ''.join([c for c in t if c.isalnum() or c=='-']) # remove punctation
return u[:maxlen].rstrip('-').lower() # clip to maxlen
if __name__ == '__main__':
import doctest
print doctest.testmod()
Upvotes: 6