Wed, 30 Dec 2020 11:02:10 +0100
Updated copyright for 2021.
# -*- coding: utf-8 -*- # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a class to prettify HTML code. """ import re from PyQt5.QtCore import QObject import Preferences class Html5Prettifier(QObject): """ Class implementing the HTML5 prettifier. """ def __init__(self, html, parent=None): """ Constructor @param html HTML text to be prettified (string) @param parent reference to the parent object (QObject) """ super(Html5Prettifier, self).__init__(parent) self.__html = html self.__indentWidth = Preferences.getEditor("IndentWidth") def getPrettifiedHtml(self): """ Public method to prettify the HTML code. @return prettified HTML code (string) """ 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) # indent all HTML 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 (string) @return prettified tag (string) """ 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 (re.MatchObject) @return prettified comment (string) """ if re.search("\n", matchobj.group()): return self.tagPrettify(matchobj.group()) else: return matchobj.group()