ProjectWeb/Html5Prettifier.py

branch
eric7
changeset 38
6a12561fc0b5
parent 35
a3f1dcf94fe4
child 40
a9b17341d181
equal deleted inserted replaced
37:01822a402c97 38:6a12561fc0b5
7 Module implementing a class to prettify HTML code. 7 Module implementing a class to prettify HTML code.
8 """ 8 """
9 9
10 import re 10 import re
11 11
12 from PyQt5.QtCore import QObject 12 from PyQt6.QtCore import QObject
13 13
14 import Preferences 14 import Preferences
15 15
16 16
17 class Html5Prettifier(QObject): 17 class Html5Prettifier(QObject):
20 """ 20 """
21 def __init__(self, html, parent=None): 21 def __init__(self, html, parent=None):
22 """ 22 """
23 Constructor 23 Constructor
24 24
25 @param html HTML text to be prettified (string) 25 @param html HTML text to be prettified
26 @param parent reference to the parent object (QObject) 26 @type str
27 @param parent reference to the parent object
28 @type QObject
27 """ 29 """
28 super().__init__(parent) 30 super().__init__(parent)
29 31
30 self.__html = html 32 self.__html = html
31 self.__indentWidth = Preferences.getEditor("IndentWidth") 33 self.__indentWidth = Preferences.getEditor("IndentWidth")
32 34
33 def getPrettifiedHtml(self): 35 def getPrettifiedHtml(self):
34 """ 36 """
35 Public method to prettify the HTML code. 37 Public method to prettify the HTML code.
36 38
37 @return prettified HTML code (string) 39 @return prettified HTML code
40 @rtype str
38 """ 41 """
39 from bs4 import BeautifulSoup 42 from bs4 import BeautifulSoup
40 soup = BeautifulSoup(self.__html, "html.parser") 43 soup = BeautifulSoup(self.__html, "html.parser")
41 prettyHtml = soup.prettify(formatter=self.tagPrettify) 44 prettyHtml = soup.prettify(formatter=self.tagPrettify)
42 # prettify comments 45 # prettify comments
51 54
52 def tagPrettify(self, tag): 55 def tagPrettify(self, tag):
53 """ 56 """
54 Public method to prettify HTML tags. 57 Public method to prettify HTML tags.
55 58
56 @param tag tag to be prettified (string) 59 @param tag tag to be prettified
57 @return prettified tag (string) 60 @type str
61 @return prettified tag
62 @rtype str
58 """ 63 """
59 return re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag, 64 return re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag,
60 flags=re.MULTILINE) 65 flags=re.MULTILINE)
61 66
62 def commentPrettify(self, matchobj): 67 def commentPrettify(self, matchobj):
63 """ 68 """
64 Public method to prettify HTML comments. 69 Public method to prettify HTML comments.
65 70
66 @param matchobj reference to the match object (re.MatchObject) 71 @param matchobj reference to the match object
67 @return prettified comment (string) 72 @type re.MatchObject
73 @return prettified comment
74 @rtype str
68 """ 75 """
69 if re.search("\n", matchobj.group()): 76 if re.search("\n", matchobj.group()):
70 return self.tagPrettify(matchobj.group()) 77 return self.tagPrettify(matchobj.group())
71 else: 78 else:
72 return matchobj.group() 79 return matchobj.group()

eric ide

mercurial