| 824 | """ |
| 825 | # http://snippets.dzone.com/posts/show/4569 |
| 826 | def replace_entity(match): |
| 827 | hash, hex, name = match.group(1), match.group(2), match.group(3) |
| 828 | if hash == "#" or name.isdigit(): |
| 829 | if hex == '' : |
| 830 | return unichr(int(name)) # "&" => "&" |
| 831 | if hex in ("x","X"): |
| 832 | return unichr(int('0x'+name, 16)) # "&" = > "&" |
| 833 | else: |
| 834 | cp = htmlentitydefs.name2codepoint.get(name) # "&" => "&" |
| 835 | return cp and unichr(cp) or match.group() # "&foo;" => "&foo;" |
| 836 | if isinstance(string, (str, unicode)): |
| 837 | return RE_UNICODE.subn(replace_entity, string)[0] |
| 838 | return string |