Added function convert_entity from decobot.utils.text_extraction

(to complete and fix revision 124)

--HG--
extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40126
This commit is contained in:
olveyra 2008-07-27 14:54:11 +00:00
parent 1e2ddb47a3
commit b59f62dc91
1 changed files with 19 additions and 0 deletions

View File

@ -2,6 +2,7 @@
Auxiliary functions which doesn't fit anywhere else
"""
import re
import htmlentitydefs
from twisted.internet import defer, reactor
from twisted.python import failure
@ -154,6 +155,24 @@ def load_class(class_path):
return cls
def convert_entity(m, keep_reserved=False):
"""
Convert a HTML entity into unicode string
"""
if m.group(1)=='#':
try:
return unichr(int(m.group(2)))
except ValueError:
return '&#%s;' % m.group(2)
try:
if not (keep_reserved and m.group(2) in ['lt', 'amp']):
return unichr(htmlentitydefs.name2codepoint[m.group(2)])
else:
return '&%s;' % m.group(2)
except KeyError:
return '&%s;' % m.group(2)
def unquote_html(s, keep_reserved=False):
"""Convert a HTML quoted string into normal string (ISO-8859-1).