]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_html5.py
Change parse_html to parse strings, not files
[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     for node in html_tree.childNodes:
23         if (node.name == 'html') and (node.type != 3): # Skip DocType element
24             html = node
25             break
26     else:
27         html = None
28
29     if not html:
30         return None
31
32     for node in html.childNodes:
33         if node.name == 'head':
34             head = node
35             break
36     else:
37         head = None
38
39     meta_charset = False
40     title = None
41     refresh = None
42     icon = None
43
44     if head:
45         for node in head.childNodes:
46             if node.name == 'title':
47                 if node.childNodes:
48                     title = node.childNodes[0].value
49                     break
50                 else:
51                     title = ''
52
53         for node in head.childNodes:
54             if (node.name == 'meta') and \
55                     ('http-equiv' in node.attributes) and \
56                     (node.attributes['http-equiv'] == 'content-type'):
57                 meta_content = node.attributes['content']
58                 if meta_content:
59                     try:
60                         meta_charset = \
61                             meta_content.lower().split('charset=')[1].split(';')[0]
62                     except IndexError:
63                         meta_charset = False
64                     else:
65                         break
66             elif (node.name == 'meta') and ('charset' in node.attributes):
67                 meta_charset = node.attributes['charset'].lower()
68                 break
69
70         if not charset:
71             charset = parser.tokenizer.stream.charEncoding[0]
72
73         if title and (charset or meta_charset):
74             title = title.encode(charset or meta_charset)
75
76         for node in head.childNodes:
77             if node.name == 'meta' and \
78                     ('http-equiv' in node.attributes) and \
79                     (node.attributes['http-equiv'] == 'refresh'):
80                 refresh = node.attributes['content']
81                 break
82
83         for node in head.childNodes:
84             if node.name == 'link' and \
85                     ('rel' in node.attributes) and \
86                     (node.attributes['rel'] in ('icon', 'shortcut icon')):
87                 icon = node.attributes['href']
88                 break
89
90     else:
91         for node in html.childNodes:
92             if node.name == 'title':
93                 if node.childNodes:
94                     title = node.childNodes[0].value
95                     break
96                 else:
97                     title = ''
98
99     if (title is None) and (refresh is None) and (icon is None):
100         return None
101     return HTMLParser(charset, meta_charset, title, refresh, icon)