-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlutils.py
63 lines (55 loc) · 2.15 KB
/
htmlutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File: htmlutils.py
# Python 2.5
# HTML utility functions.
# Copyright (C) 2009,2010,2011
# Pierre Hauweele <pierre.hauweele@clubinfo-umons.be>, GPLv3
# This file is part of UMonsTimesheetGrabber.
#
# UMonsTimesheetGrabber is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UMonsTimesheetGrabber is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with UMonsTimesheetGrabber.
# If not, see <http://www.gnu.org/licenses/>.
import sys
from re import sub
from htmlentitydefs import name2codepoint as ent_name2codepoint
from urllib import quote_plus
import tidy
TIDY_OPTIONS = {"doctype": "omit", "numeric_entities": "yes", "output_xml": 1,\
"add_xml_decl": 0, "tidy_mark": 0, "wrap": 0,\
"input_encoding": "latin1", "output_encoding": "utf8"}
def repair(html):
tdoc = tidy.parseString(html, **TIDY_OPTIONS)
str(tdoc) # Workaround for a buffer size bug in the C tidylib.
return str(tdoc).decode("utf8")
def htmlent_to_uni(m):
ent = m.group(0)
if ent[1] == "#":
try:
if ent[2] == "x":
return unichr(int(ent[3:-1], 16))
else:
return unichr(int(ent[2:-1]))
except ValueError, err:
print >> sys.stderr, "Warning: invalid numeric entity", err
else:
try:
return unichr(ent_name2codepoint[ent[1:-1]])
except KeyError, err:
print >> sys.stderr, "Warning: invalid entity", err
return ent
def unescape_html(html):
return sub(u"&#?\w+?;", htmlent_to_uni, html)
def post_quote(x):
# if the user gives a string, let's hope it's correctly encoded
if type(x) == unicode:
x = x.encode("latin1")
return quote_plus(x)