diff -r 27f43499da60 -r 2bed42620c99 ProjectWeb/Html5Prettifier.py --- a/ProjectWeb/Html5Prettifier.py Thu Dec 30 12:22:34 2021 +0100 +++ b/ProjectWeb/Html5Prettifier.py Wed Sep 21 15:36:01 2022 +0200 @@ -18,56 +18,63 @@ """ Class implementing the HTML5 prettifier. """ + def __init__(self, html, parent=None): """ Constructor - + @param html HTML text to be prettified @type str @param parent reference to the parent object @type QObject """ super().__init__(parent) - + self.__html = html self.__indentWidth = Preferences.getEditor("IndentWidth") - + def getPrettifiedHtml(self): """ Public method to prettify the HTML code. - + @return prettified HTML code @rtype str """ from bs4 import BeautifulSoup + soup = BeautifulSoup(self.__html, "html.parser") prettyHtml = soup.prettify(formatter=self.tagPrettify) # prettify comments - prettyHtml = re.sub("<!--(.*?)-->", self.commentPrettify, prettyHtml, - flags=re.DOTALL) + prettyHtml = re.sub( + "<!--(.*?)-->", self.commentPrettify, prettyHtml, flags=re.DOTALL + ) # indent all HTML - prettyHtml = re.sub("^( *)(.*)$", - r"{0}\2".format(r"\1" * self.__indentWidth), - prettyHtml, flags=re.MULTILINE) - + prettyHtml = re.sub( + "^( *)(.*)$", + r"{0}\2".format(r"\1" * self.__indentWidth), + prettyHtml, + flags=re.MULTILINE, + ) + return prettyHtml - + def tagPrettify(self, tag): """ Public method to prettify HTML tags. - + @param tag tag to be prettified @type str @return prettified tag @rtype str """ - return re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag, - flags=re.MULTILINE) - + return re.sub( + " {{1,{0}}}".format(self.__indentWidth), " ", tag, flags=re.MULTILINE + ) + def commentPrettify(self, matchobj): """ Public method to prettify HTML comments. - + @param matchobj reference to the match object @type re.MatchObject @return prettified comment