]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_html5.py
Fix(Py3): Stop encoding unicode to bytes
[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-2023 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(
21         html_text, encoding=charset, parseMeta=bool(charset))
22
23     html = None
24     if hasattr(html_tree, 'childNodes'):
25         for node in html_tree.childNodes:
26             # Skip DocType element
27             if (node.name == 'html') and (node.type != 3):
28                 html = node
29                 break
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].\
64                             split(';')[0]
65                     except IndexError:
66                         meta_charset = False
67                     else:
68                         break
69             elif (node.name == 'meta') and ('charset' in node.attributes):
70                 meta_charset = node.attributes['charset'].lower()
71                 break
72
73         if not charset:
74             charset = parser.tokenizer.stream.charEncoding[0]
75
76         #if title and (charset or meta_charset):
77         #    title = title.encode(charset or meta_charset)
78
79         for node in head.childNodes:
80             if node.name == 'meta' and \
81                     ('http-equiv' in node.attributes) and \
82                     (node.attributes['http-equiv'] == 'refresh'):
83                 refresh = node.attributes['content']
84                 break
85
86         for node in head.childNodes:
87             if node.name == 'link' and \
88                     ('rel' in node.attributes) and \
89                     (node.attributes['rel'] in ('icon', 'shortcut icon')):
90                 icon = node.attributes['href']
91                 break
92
93     else:
94         for node in html.childNodes:
95             if node.name == 'title':
96                 if node.childNodes:
97                     title = node.childNodes[0].value
98                     break
99                 else:
100                     title = ''
101
102     if (title is None) and (refresh is None) and (icon is None):
103         return None
104     return HTMLParser(charset, meta_charset, title, refresh, icon)