Posted By

hisamu on 01/11/10


Tagged

python slug


Versions (?)

Create slugs in python


 / Published in: Python
 

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

A slug function that support acentuation :)

  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", "-")

Report this snippet  

You need to login to post a comment.