ProjectWeb/Html5Prettifier.py

Tue, 10 Dec 2024 15:49:01 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 10 Dec 2024 15:49:01 +0100
branch
eric7
changeset 56
d91d613bba96
parent 52
815847f3d404
permissions
-rw-r--r--

Updated copyright for 2025.

# -*- coding: utf-8 -*-

# Copyright (c) 2015 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a class to prettify HTML code.
"""

import re

from bs4 import BeautifulSoup
from PyQt6.QtCore import QObject

from eric7 import Preferences


class Html5Prettifier(QObject):
    """
    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
        """
        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
        @type str
        @return prettified tag
        @rtype str
        """
        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
        @rtype str
        """
        if re.search(r"\n", matchobj.group()):
            return self.tagPrettify(matchobj.group())
        else:
            return matchobj.group()

eric ide

mercurial