--- /dev/null
+"""
+ HTML Parser using html5.
+
+ Written by Broytman. Copyright (C) 2010 PhiloSoft Design
+"""
+
+from html5lib import HTMLParser as HTML5Parser
+from parse_html_util import HTMLParser
+
+
+def parse_html(filename, charset=None, log=None):
+ fp = open(filename)
+ html_tree = HTML5Parser().parse(fp, charset)
+ fp.close()
+
+ html = html_tree.childNodes[-1]
+ for node in html.childNodes:
+ if node.name == 'head':
+ head = node
+ break
+ else:
+ head = None
+
+ meta_charset = False
+ title = None
+ refresh = None
+ icon = None
+
+ if head:
+ for node in head.childNodes:
+ if node.name == 'meta' and \
+ ('http-equiv' in node.attributes) and \
+ (node.attributes['http-equiv'] == 'content-type'):
+ meta_content = node.attributes['content']
+ if meta_content:
+ try:
+ meta_charset = \
+ meta_content.lower().split('charset=')[1].split(';')[0]
+ except IndexError:
+ meta_charset = False
+ else:
+ break
+
+ for node in head.childNodes:
+ if node.name == 'title':
+ title = node.childNodes[0].value
+ break
+
+ if title and (charset or meta_charset):
+ title = title.encode(charset or meta_charset)
+
+ for node in head.childNodes:
+ if node.name == 'meta' and \
+ ('http-equiv' in node.attributes) and \
+ (node.attributes['http-equiv'] == 'refresh'):
+ refresh = node.attributes['content']
+ break
+
+ for node in head.childNodes:
+ if node.name == 'link' and \
+ ('rel' in node.attributes) and \
+ (node.attributes['rel'] in ('icon', 'shortcut icon')):
+ icon = node.attributes['href']
+ break
+
+ return HTMLParser(charset, meta_charset, title, refresh, icon)