ProjectWeb/Html5Prettifier.py

Thu, 01 Jan 2015 17:14:39 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 01 Jan 2015 17:14:39 +0100
changeset 5
31bc1ef6f624
child 18
586a57c396b2
permissions
-rw-r--r--

Added the HTML prettifier.

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

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

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

from __future__ import unicode_literals

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)
        """
        result = re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag,
                        flags=re.MULTILINE)
        return result
    
    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()):
            result = self.tagPrettify(matchobj.group())
        else:
            result = matchobj.group()
        return result

eric ide

mercurial