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