]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_html5.py
Fix a bug: html_tree may have no childNodes
[bookmarks_db.git] / parse_html / bkmk_ph_html5.py
1 """HTML Parser using html5
2
3 This file is a part of Bookmarks database and Internet robot.
4
5 """
6
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2010-2014 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['parse_html']
12
13
14 from html5lib import HTMLParser as HTML5Parser
15 from .bkmk_ph_util import HTMLParser
16
17
18 def parse_html(html_text, charset=None, log=None):
19     parser = HTML5Parser()
20     html_tree = parser.parse(html_text, encoding=charset, parseMeta=bool(charset))
21
22     html = None
23     if hasattr(html_tree, 'childNodes'):
24         for node in html_tree.childNodes:
25             # Skip DocType element
26             if (node.name == 'html') and (node.type != 3):
27                 html = node
28                 break
29
30     if not html:
31         return None
32
33     for node in html.childNodes:
34         if node.name == 'head':
35             head = node
36             break
37     else:
38         head = None
39
40     meta_charset = False
41     title = None
42     refresh = None
43     icon = None
44
45     if head:
46         for node in head.childNodes:
47             if node.name == 'title':
48                 if node.childNodes:
49                     title = node.childNodes[0].value
50                     break
51                 else:
52                     title = ''
53
54         for node in head.childNodes:
55             if (node.name == 'meta') and \
56                     ('http-equiv' in node.attributes) and \
57                     (node.attributes['http-equiv'] == 'content-type'):
58                 meta_content = node.attributes['content']
59                 if meta_content:
60                     try:
61                         meta_charset = \
62                             meta_content.lower().split('charset=')[1].split(';')[0]
63                     except IndexError:
64                         meta_charset = False
65                     else:
66                         break
67             elif (node.name == 'meta') and ('charset' in node.attributes):
68                 meta_charset = node.attributes['charset'].lower()
69                 break
70
71         if not charset:
72             charset = parser.tokenizer.stream.charEncoding[0]
73
74         if title and (charset or meta_charset):
75             title = title.encode(charset or meta_charset)
76
77         for node in head.childNodes:
78             if node.name == 'meta' and \
79                     ('http-equiv' in node.attributes) and \
80                     (node.attributes['http-equiv'] == 'refresh'):
81                 refresh = node.attributes['content']
82                 break
83
84         for node in head.childNodes:
85             if node.name == 'link' and \
86                     ('rel' in node.attributes) and \
87                     (node.attributes['rel'] in ('icon', 'shortcut icon')):
88                 icon = node.attributes['href']
89                 break
90
91     else:
92         for node in html.childNodes:
93             if node.name == 'title':
94                 if node.childNodes:
95                     title = node.childNodes[0].value
96                     break
97                 else:
98                     title = ''
99
100     if (title is None) and (refresh is None) and (icon is None):
101         return None
102     return HTMLParser(charset, meta_charset, title, refresh, icon)