global HTMLParser from html.parser import HTMLParser global html import html global browserDoc class browserDoc(QTextBrowser): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.setOpenLinks(False) style = "" if (config["accessibility"]["colorBackground"] != "default"): style += "background-color: " +config["accessibility"]["colorBackground"] + ";" if (config["accessibility"]["colorText"] != "default"): style += "color: " +config["accessibility"]["colorText"]+ ";" self.setStyleSheet("QTextBrowser { " +style+ " }") self.anchorClicked.connect(self.cAnchorClicked) class cHtmlParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.output = "" def handle_starttag(self,tag,attrs): if tag == "img": altText = False for attr in attrs: if attr[0] == "alt": altText = html.escape(attr[1]) break for attr in attrs: if attr[0] == "src": url = attr[1] if not altText: altText = url.rsplit("/")[-1] self.output += 'image: ' +html.escape(altText)+ '' return return self.output += "<" +tag for attr in attrs: if attr[1] != None: self.output += ' ' +html.escape(attr[0])+ '="' +html.escape(attr[1])+ '"' else: self.output += ' ' +html.escape(attr[0]) self.output += ">" def handle_endtag(self,tag): if tag in ["img"]: return self.output += "" def handle_data(self,data): self.output += data def cRenderHtml(self,htm): parser = self.cHtmlParser() parser.feed(htm) self.clear() self.insertHtml(parser.output) self.update() def cAnchorClicked(self,url): url = str(url.toEncoded(),"utf-8") browserWindow = self.parent() curUrl = browserWindow.cDocumentInfo["url"] curUrlParsed = parseUrl(curUrl) urlParsed = parseUrl(url) print("---") print("navigating from: " +curUrl) print("to: " +url) if urlParsed["protocol"] == "": # is relative if url[0] == "#": curUrlParsed["anchor"] = url[1:] url = unparseUrl(curUrlParsed) elif url[:2] == "//": url = curUrlParsed["protocol"] + ":" + url elif url[0] == "/": urlParsed["protocol"] = curUrlParsed["protocol"] urlParsed["domain"] = curUrlParsed["domain"] urlParsed["path"] = urlParsed["path"][1:] url = unparseUrl(urlParsed) else: urlParsed["protocol"] = curUrlParsed["protocol"] urlParsed["domain"] = curUrlParsed["domain"] while len(curUrlParsed["path"]) > 0 and curUrlParsed["path"][-1] == "/": curUrlParsed["path"] = curUrlParsed["path"][:-1] urlParsed["path"] = curUrlParsed["path"] + "/" + url url = unparseUrl(urlParsed) browserWindow.cUrlBar.setText(url) browserWindow.cNavigate()