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