Added the HTML prettifier.

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
parent 4
6438afaad632
child 6
a1600fa29542

Added the HTML prettifier.

PluginProjectWeb.py file | annotate | diff | comparison | revisions
PluginWeb.e4p file | annotate | diff | comparison | revisions
ProjectWeb/Html5Prettifier.py file | annotate | diff | comparison | revisions
ProjectWeb/Html5ToCss3Converter.py file | annotate | diff | comparison | revisions
ProjectWeb/Html5ToCss3ConverterParameterDialog.py file | annotate | diff | comparison | revisions
ProjectWeb/Html5ToJsConverterParameterDialog.py file | annotate | diff | comparison | revisions
--- a/PluginProjectWeb.py	Thu Jan 01 13:27:03 2015 +0100
+++ b/PluginProjectWeb.py	Thu Jan 01 17:14:39 2015 +0100
@@ -180,6 +180,9 @@
             "HTML5 to CSS3"), self.__htm5ToCss3)
         self.__html5ToJsAct = self.__menu.addAction(self.tr(
             "HTML5 to JavaScript"), self.__htm5ToJs)
+        self.__menu.addSeparator()
+        self.__html5PrettifyAct = self.__menu.addAction(self.tr(
+            "Prettify HTML"), self.__html5Prettify)
         
         self.__menu.aboutToShow.connect(self.__menuAboutToShow)
     
@@ -189,11 +192,14 @@
         """
         editor = e5App().getObject("ViewManager").activeWindow()
         selectionAvailable = bool(editor and editor.selectedText() != "")
+        isHtml = editor.getLanguage().lower().startswith("html")
         
         self.__html5ToCss3Act.setEnabled(
-            selectionAvailable and BeautifulSoupAvailable)
+            selectionAvailable and BeautifulSoupAvailable and isHtml)
         self.__html5ToJsAct.setEnabled(
-            selectionAvailable and BeautifulSoupAvailable)
+            selectionAvailable and BeautifulSoupAvailable and isHtml)
+        self.__html5PrettifyAct.setEnabled(
+            BeautifulSoupAvailable and isHtml)
     
     def __populateMenu(self, name, menu):
         """
@@ -264,7 +270,6 @@
         html = editor.selectedText()
         
         converter = Html5ToCss3Converter(html)
-        
         css3 = converter.getCss3()
         
         if css3:
@@ -283,7 +288,6 @@
         html = editor.selectedText()
         
         converter = Html5ToJsConverter(html)
-        
         js = converter.getJavaScript()
         
         if js:
@@ -291,3 +295,24 @@
             newEditor = vm.activeWindow()
             newEditor.setText(js)
             newEditor.setLanguage("dummy.js")
+    
+    def __html5Prettify(self):
+        """
+        Private slot handling the Prettify HTML action.
+        """
+        from ProjectWeb.Html5Prettifier import Html5Prettifier
+        editor = e5App().getObject("ViewManager").activeWindow()
+        html = editor.text()
+        
+        prettifier = Html5Prettifier(html)
+        newHtml = prettifier.getPrettifiedHtml()
+        
+        if newHtml and newHtml != html:
+            cursorLine, cursorIndex = editor.getCursorPosition()
+            
+            editor.beginUndoAction()
+            editor.selectAll()
+            editor.replaceSelectedText(newHtml)
+            editor.endUndoAction()
+            
+            editor.setCursorPosition(cursorLine, cursorIndex)
--- a/PluginWeb.e4p	Thu Jan 01 13:27:03 2015 +0100
+++ b/PluginWeb.e4p	Thu Jan 01 17:14:39 2015 +0100
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE Project SYSTEM "Project-5.1.dtd">
 <!-- eric project file for project PluginWeb -->
-<!-- Copyright (C) 2014 Detlev Offenbach, detlev@die-offenbachs.de -->
+<!-- Copyright (C) 2015 Detlev Offenbach, detlev@die-offenbachs.de -->
 <Project version="5.1">
   <Language>en_US</Language>
   <Hash>b8ec8ccc0a087e55bdb7879c78b018b43ce2c6ed</Hash>
@@ -21,6 +21,7 @@
     <Source>ProjectWeb/Html5ToCss3ConverterParameterDialog.py</Source>
     <Source>ProjectWeb/Html5ToJsConverter.py</Source>
     <Source>ProjectWeb/Html5ToJsConverterParameterDialog.py</Source>
+    <Source>ProjectWeb/Html5Prettifier.py</Source>
   </Sources>
   <Forms>
     <Form>ProjectWeb/Html5ToCss3ConverterParameterDialog.ui</Form>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ProjectWeb/Html5Prettifier.py	Thu Jan 01 17:14:39 2015 +0100
@@ -0,0 +1,77 @@
+# -*- 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
--- a/ProjectWeb/Html5ToCss3Converter.py	Thu Jan 01 13:27:03 2015 +0100
+++ b/ProjectWeb/Html5ToCss3Converter.py	Thu Jan 01 17:14:39 2015 +0100
@@ -57,7 +57,6 @@
         if dlg.exec_() == QDialog.Accepted:
             indentation, placeholders = dlg.getData()
             
-            # TODO: implement this
             self.__createSoup()
             
             alreadyDone = list(self.TagsToIgnore)
--- a/ProjectWeb/Html5ToCss3ConverterParameterDialog.py	Thu Jan 01 13:27:03 2015 +0100
+++ b/ProjectWeb/Html5ToCss3ConverterParameterDialog.py	Thu Jan 01 17:14:39 2015 +0100
@@ -7,6 +7,8 @@
 Module implementing a dialog to enter the CSS conversion parameters.
 """
 
+from __future__ import unicode_literals
+
 from PyQt5.QtWidgets import QDialog
 
 from .Ui_Html5ToCss3ConverterParameterDialog import \
--- a/ProjectWeb/Html5ToJsConverterParameterDialog.py	Thu Jan 01 13:27:03 2015 +0100
+++ b/ProjectWeb/Html5ToJsConverterParameterDialog.py	Thu Jan 01 17:14:39 2015 +0100
@@ -7,6 +7,8 @@
 Module implementing a dialog to enter the JavaScript conversion parameters.
 """
 
+from __future__ import unicode_literals
+
 from PyQt5.QtWidgets import QDialog
 
 from .Ui_Html5ToJsConverterParameterDialog import \

eric ide

mercurial