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() |