| 251 | """ |
| 252 | # http://snippets.dzone.com/posts/show/4569 |
| 253 | def replace_entity(match): |
| 254 | hash, hex, name = match.group(1), match.group(2), match.group(3) |
| 255 | if hash == "#" or name.isdigit(): |
| 256 | if hex == '' : |
| 257 | return unichr(int(name)) # "&" => "&" |
| 258 | if hex in ("x","X"): |
| 259 | return unichr(int('0x'+name, 16)) # "&" = > "&" |
| 260 | else: |
| 261 | cp = htmlentitydefs.name2codepoint.get(name) # "&" => "&" |
| 262 | return cp and unichr(cp) or match.group() # "&foo;" => "&foo;" |
| 263 | if isinstance(string, (str, unicode)): |
| 264 | return RE_UNICODE.subn(replace_entity, string)[0] |
| 265 | return string |