Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # coding=UTF-8
- import re
- import unicodedata
- import string
- import random
- re_camel_case = re.compile(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))')
- def slugify(value):
- """
- Convert to ASCII convert spaces to hyphens. Remove characters that aren't
- alphanumerics, underscores, or hyphens. Convert to lowercase. Camel case
- to hyphen. Also strip leading and trailing whitespace.
- """
- value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
- value = re_camel_case.sub(r' \1', value)
- value = re.sub('[^\w\s-]', '', value).strip().lower()
- return re.sub('[-\s]+', '-', value)
- def id_generator(size=3, chars=string.ascii_lowercase + string.digits):
- return ''.join(random.choice(chars) for _ in range(size))
- def slug(value):
- return slugify(value)+"-"+id_generator()
- print(slug(u"We-🚔A.re/You/âé-ï_$#@%"))
- #gives:
- we-are-youae-i_-0j2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement