def decode_htmlentities(text):
 def substitute_entity(match):
  try:
   ent = match.group(3)
   if (match.group(1) == '#'):
    if (match.group(2) == ''):
     return safe_unichr(int(ent))
    elif (match.group(2) in ['x', 'X']):
     return safe_unichr(int(ent, 16))
   else:
    cp = n2cp.get(ent)
    if cp:
     return safe_unichr(cp)
    else:
     return match.group()
  except:
   return match.group()
 return RE_HTML_ENTITY.sub(substitute_entity, text)

def decode_htmlentities ( text ) : def substitute_entity ( match ) : try : ent = match . group ( 3 ) if ( match . group ( 1 ) == '#' ) : if ( match . group ( 2 ) == '' ) : return safe_unichr ( int ( ent ) ) elif ( match . group ( 2 ) in [ 'x' , 'X' ] ) : return safe_unichr ( int ( ent , 16 ) ) else : cp = n2cp . get ( ent ) if cp : return safe_unichr ( cp ) else : return match . group ( ) except : return match . group ( ) return RE_HTML_ENTITY . sub ( substitute_entity , text )
decode html entities in text .