ProjectWeb/Html5Prettifier.py

Mon, 26 Apr 2021 17:47:56 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 26 Apr 2021 17:47:56 +0200
changeset 35
a3f1dcf94fe4
parent 34
a6d8718f37b5
child 38
6a12561fc0b5
permissions
-rw-r--r--

Implemented some code simplifications.

# -*- 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().__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()

eric ide

mercurial