Create better Slugs in Python


/ Published in: Python
Save to your folder(s)

I only did some improvements over another snippet.


Copy this code and paste it in your HTML
  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()

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.