This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/util/misc.py

43 lines
1 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
from HTMLParser import HTMLParser
2012-04-19 10:40:16 +02:00
import htmlentitydefs
2011-11-20 10:23:31 +01:00
import errno
import re
class HTMLStripper(HTMLParser):
def __init__(self, data):
HTMLParser.__init__(self)
self._stripped = []
self.feed(data)
def handle_starttag(self, tag, attrs):
if tag.lower() == 'br':
self._stripped.append('\n')
def handle_charref(self, name):
try:
if name.lower().startswith('x'):
char = int(name[1:], 16)
else:
char = int(name)
self._stripped.append(unichr(char))
except Exception, error:
2012-02-23 11:49:57 +01:00
return
2011-11-20 10:23:31 +01:00
def handle_entityref(self, name):
try:
2012-04-19 10:40:16 +02:00
char = unichr(htmlentitydefs.name2codepoint[name])
2011-11-20 10:23:31 +01:00
except Exception, error:
char = u'&%s;' % name
self._stripped.append(char)
def handle_data(self, data):
self._stripped.append(data)
@property
def stripped(self):
return ''.join(self._stripped)
def strip_html(data):
return HTMLStripper(data).stripped