Mercurial > cpython-lru_cache-weakref
annotate Lib/html/__init__.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 7b9235852b3b |
| children |
| rev | line source |
|---|---|
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
1 """ |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
2 General functions for HTML manipulation. |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
3 """ |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
4 |
|
87275
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
5 import re as _re |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
6 from html.entities import html5 as _html5 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
7 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
8 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
9 __all__ = ['escape', 'unescape'] |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
10 |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
11 |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
12 def escape(s, quote=True): |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
13 """ |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
14 Replace special characters "&", "<" and ">" to HTML-safe sequences. |
|
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
15 If the optional flag quote is true (the default), the quotation mark |
|
72373
bc5b96c92770
Fix issue12938 - Update the docstring of html.escape. Include the information on single quote.
Senthil Kumaran <senthil@uthcode.com>
parents:
65362
diff
changeset
|
16 characters, both double quote (") and single quote (') characters are also |
|
bc5b96c92770
Fix issue12938 - Update the docstring of html.escape. Include the information on single quote.
Senthil Kumaran <senthil@uthcode.com>
parents:
65362
diff
changeset
|
17 translated. |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
18 """ |
|
84481
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
19 s = s.replace("&", "&") # Must be done first! |
|
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
20 s = s.replace("<", "<") |
|
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
21 s = s.replace(">", ">") |
|
65362
5633af590057
#2830: add html.escape() helper and move cgi.escape() uses in the standard library to it. It defaults to quote=True and also escapes single quotes, which makes casual use safer. The cgi.escape() interface is not touched, but emits a (silent) PendingDeprecationWarning.
Georg Brandl <georg@python.org>
parents:
47147
diff
changeset
|
22 if quote: |
|
84481
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
23 s = s.replace('"', """) |
|
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
24 s = s.replace('\'', "'") |
|
db5f2b74e369
#18020: improve html.escape speed by an order of magnitude. Patch by Matt Bryant.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
72373
diff
changeset
|
25 return s |
|
87275
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
26 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
27 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
28 # see http://www.w3.org/TR/html5/syntax.html#tokenizing-character-references |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
29 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
30 _invalid_charrefs = { |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
31 0x00: '\ufffd', # REPLACEMENT CHARACTER |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
32 0x0d: '\r', # CARRIAGE RETURN |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
33 0x80: '\u20ac', # EURO SIGN |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
34 0x81: '\x81', # <control> |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
35 0x82: '\u201a', # SINGLE LOW-9 QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
36 0x83: '\u0192', # LATIN SMALL LETTER F WITH HOOK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
37 0x84: '\u201e', # DOUBLE LOW-9 QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
38 0x85: '\u2026', # HORIZONTAL ELLIPSIS |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
39 0x86: '\u2020', # DAGGER |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
40 0x87: '\u2021', # DOUBLE DAGGER |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
41 0x88: '\u02c6', # MODIFIER LETTER CIRCUMFLEX ACCENT |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
42 0x89: '\u2030', # PER MILLE SIGN |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
43 0x8a: '\u0160', # LATIN CAPITAL LETTER S WITH CARON |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
44 0x8b: '\u2039', # SINGLE LEFT-POINTING ANGLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
45 0x8c: '\u0152', # LATIN CAPITAL LIGATURE OE |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
46 0x8d: '\x8d', # <control> |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
47 0x8e: '\u017d', # LATIN CAPITAL LETTER Z WITH CARON |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
48 0x8f: '\x8f', # <control> |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
49 0x90: '\x90', # <control> |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
50 0x91: '\u2018', # LEFT SINGLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
51 0x92: '\u2019', # RIGHT SINGLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
52 0x93: '\u201c', # LEFT DOUBLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
53 0x94: '\u201d', # RIGHT DOUBLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
54 0x95: '\u2022', # BULLET |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
55 0x96: '\u2013', # EN DASH |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
56 0x97: '\u2014', # EM DASH |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
57 0x98: '\u02dc', # SMALL TILDE |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
58 0x99: '\u2122', # TRADE MARK SIGN |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
59 0x9a: '\u0161', # LATIN SMALL LETTER S WITH CARON |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
60 0x9b: '\u203a', # SINGLE RIGHT-POINTING ANGLE QUOTATION MARK |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
61 0x9c: '\u0153', # LATIN SMALL LIGATURE OE |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
62 0x9d: '\x9d', # <control> |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
63 0x9e: '\u017e', # LATIN SMALL LETTER Z WITH CARON |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
64 0x9f: '\u0178', # LATIN CAPITAL LETTER Y WITH DIAERESIS |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
65 } |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
66 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
67 _invalid_codepoints = { |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
68 # 0x0001 to 0x0008 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
69 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
70 # 0x000E to 0x001F |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
71 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
72 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
73 # 0x007F to 0x009F |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
74 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
75 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
76 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
77 # 0xFDD0 to 0xFDEF |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
78 0xfdd0, 0xfdd1, 0xfdd2, 0xfdd3, 0xfdd4, 0xfdd5, 0xfdd6, 0xfdd7, 0xfdd8, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
79 0xfdd9, 0xfdda, 0xfddb, 0xfddc, 0xfddd, 0xfdde, 0xfddf, 0xfde0, 0xfde1, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
80 0xfde2, 0xfde3, 0xfde4, 0xfde5, 0xfde6, 0xfde7, 0xfde8, 0xfde9, 0xfdea, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
81 0xfdeb, 0xfdec, 0xfded, 0xfdee, 0xfdef, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
82 # others |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
83 0xb, 0xfffe, 0xffff, 0x1fffe, 0x1ffff, 0x2fffe, 0x2ffff, 0x3fffe, 0x3ffff, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
84 0x4fffe, 0x4ffff, 0x5fffe, 0x5ffff, 0x6fffe, 0x6ffff, 0x7fffe, 0x7ffff, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
85 0x8fffe, 0x8ffff, 0x9fffe, 0x9ffff, 0xafffe, 0xaffff, 0xbfffe, 0xbffff, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
86 0xcfffe, 0xcffff, 0xdfffe, 0xdffff, 0xefffe, 0xeffff, 0xffffe, 0xfffff, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
87 0x10fffe, 0x10ffff |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
88 } |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
89 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
90 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
91 def _replace_charref(s): |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
92 s = s.group(1) |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
93 if s[0] == '#': |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
94 # numeric charref |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
95 if s[1] in 'xX': |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
96 num = int(s[2:].rstrip(';'), 16) |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
97 else: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
98 num = int(s[1:].rstrip(';')) |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
99 if num in _invalid_charrefs: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
100 return _invalid_charrefs[num] |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
101 if 0xD800 <= num <= 0xDFFF or num > 0x10FFFF: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
102 return '\uFFFD' |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
103 if num in _invalid_codepoints: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
104 return '' |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
105 return chr(num) |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
106 else: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
107 # named charref |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
108 if s in _html5: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
109 return _html5[s] |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
110 # find the longest matching name (as defined by the standard) |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
111 for x in range(len(s)-1, 1, -1): |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
112 if s[:x] in _html5: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
113 return _html5[s[:x]] + s[x:] |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
114 else: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
115 return '&' + s |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
116 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
117 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
118 _charref = _re.compile(r'&(#[0-9]+;?' |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
119 r'|#[xX][0-9a-fA-F]+;?' |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
120 r'|[^\t\n\f <&#;]{1,32};?)') |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
121 |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
122 def unescape(s): |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
123 """ |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
124 Convert all named and numeric character references (e.g. >, >, |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
125 &x3e;) in the string s to the corresponding unicode characters. |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
126 This function uses the rules defined by the HTML 5 standard |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
127 for both valid and invalid character references, and the list of |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
128 HTML 5 named character references defined in html.entities.html5. |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
129 """ |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
130 if '&' not in s: |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
131 return s |
|
7b9235852b3b
#2927: Added the unescape() function to the html module.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
84481
diff
changeset
|
132 return _charref.sub(_replace_charref, s) |
