Return to Snippet

Revision: 22380
at January 11, 2010 12:02 by hisamu


Updated Code
# -*- coding: utf-8 -*-
import htmlentitydefs, re

def slugfy(text, separator):
  ret = ""
  for c in text.lower():
    try:
      ret += htmlentitydefs.codepoint2name[ord(c)]
    except:
      ret += c

  ret = re.sub("([a-zA-Z])(uml|acute|grave|circ|tilde|cedil)", r"\1", ret)
  ret = re.sub("\W", " ", ret)
  ret = re.sub(" +", separator, ret)

  return ret.strip()

# usage @ pt_BR
print slugfy("Teste de acentuação python", "-")

Revision: 22379
at January 11, 2010 11:49 by hisamu


Initial Code
import htmlentitydefs, re

def slugfy(text):
  ret = ""
  for c in text:
    try:
      ret += htmlentitydefs.codepoint2name[ord(c)]
    except:
      ret += c

  return re.sub("([a-zA-Z])(uml|acute|grave|circ|tilde|cedil)", r"\1", ret)

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

Initial Description
A slug function that support acentuation :)

Initial Title
Create slugs in python

Initial Tags
python

Initial Language
Python