Replace XML character references with the referenced characters
(text)
| 68 | |
| 69 | |
| 70 | def unescape(text): |
| 71 | """Replace XML character references with the referenced characters""" |
| 72 | |
| 73 | def fixup(m): |
| 74 | text = m.group(0) |
| 75 | if text[1] == "#": |
| 76 | # Character reference |
| 77 | if text[2] == "x": |
| 78 | code = int(text[3:-1], 16) |
| 79 | else: |
| 80 | code = int(text[2:-1]) |
| 81 | else: |
| 82 | # Named entity |
| 83 | try: |
| 84 | code = htmlentitydefs.name2codepoint[text[1:-1]] |
| 85 | except KeyError: |
| 86 | return text # leave unchanged |
| 87 | try: |
| 88 | return chr(code) |
| 89 | except (ValueError, OverflowError): |
| 90 | return text # leave unchanged |
| 91 | |
| 92 | return re.sub("&(?:[0-9A-Za-z]+|#(?:[0-9]+|x[0-9A-Fa-f]+));", fixup, text) |
| 93 | |
| 94 | |
| 95 | def literal_destringizer(rep): |