Posted By

alvarohurtado84 on 05/19/11


Tagged

python slug django slugify slugfy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

guillermoj88gmailcom


Create better Slugs in Python


 / Published in: Python
 

URL: http://www.alvarohurtado.es/

I only did some improvements over another snippet.

  1. # -*- coding: utf-8 -*-
  2. import htmlentitydefs, re
  3.  
  4. def slugfy(text, separator="-"):
  5. ret = ""
  6. for c in text.lower():
  7. try:
  8. ret += htmlentitydefs.codepoint2name[ord(c)]
  9. except:
  10. ret += c
  11.  
  12. ret = re.sub("([a-zA-Z])(uml|acute|grave|circ|tilde|cedil)", r"\1", ret)
  13. ret = ret.strip()
  14. ret = re.sub(" ", "_", ret)
  15. ret = re.sub("\W", "", ret)
  16. ret = re.sub("[ _]+", separator, ret)
  17.  
  18. return ret.strip()

Report this snippet  

You need to login to post a comment.