Create slugs in python


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

A slug function that support acentuation :)


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 = re.sub("\W", " ", ret)
  14. ret = re.sub(" +", separator, ret)
  15.  
  16. return ret.strip()
  17.  
  18. # usage @ pt_BR
  19. print slugfy("Teste de acentuação python", "-")

URL: http://www.lsouza.pro.br

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.