16 |
16 |
17 class Html5Prettifier(QObject): |
17 class Html5Prettifier(QObject): |
18 """ |
18 """ |
19 Class implementing the HTML5 prettifier. |
19 Class implementing the HTML5 prettifier. |
20 """ |
20 """ |
|
21 |
21 def __init__(self, html, parent=None): |
22 def __init__(self, html, parent=None): |
22 """ |
23 """ |
23 Constructor |
24 Constructor |
24 |
25 |
25 @param html HTML text to be prettified |
26 @param html HTML text to be prettified |
26 @type str |
27 @type str |
27 @param parent reference to the parent object |
28 @param parent reference to the parent object |
28 @type QObject |
29 @type QObject |
29 """ |
30 """ |
30 super().__init__(parent) |
31 super().__init__(parent) |
31 |
32 |
32 self.__html = html |
33 self.__html = html |
33 self.__indentWidth = Preferences.getEditor("IndentWidth") |
34 self.__indentWidth = Preferences.getEditor("IndentWidth") |
34 |
35 |
35 def getPrettifiedHtml(self): |
36 def getPrettifiedHtml(self): |
36 """ |
37 """ |
37 Public method to prettify the HTML code. |
38 Public method to prettify the HTML code. |
38 |
39 |
39 @return prettified HTML code |
40 @return prettified HTML code |
40 @rtype str |
41 @rtype str |
41 """ |
42 """ |
42 from bs4 import BeautifulSoup |
43 from bs4 import BeautifulSoup |
|
44 |
43 soup = BeautifulSoup(self.__html, "html.parser") |
45 soup = BeautifulSoup(self.__html, "html.parser") |
44 prettyHtml = soup.prettify(formatter=self.tagPrettify) |
46 prettyHtml = soup.prettify(formatter=self.tagPrettify) |
45 # prettify comments |
47 # prettify comments |
46 prettyHtml = re.sub("<!--(.*?)-->", self.commentPrettify, prettyHtml, |
48 prettyHtml = re.sub( |
47 flags=re.DOTALL) |
49 "<!--(.*?)-->", self.commentPrettify, prettyHtml, flags=re.DOTALL |
|
50 ) |
48 # indent all HTML |
51 # indent all HTML |
49 prettyHtml = re.sub("^( *)(.*)$", |
52 prettyHtml = re.sub( |
50 r"{0}\2".format(r"\1" * self.__indentWidth), |
53 "^( *)(.*)$", |
51 prettyHtml, flags=re.MULTILINE) |
54 r"{0}\2".format(r"\1" * self.__indentWidth), |
52 |
55 prettyHtml, |
|
56 flags=re.MULTILINE, |
|
57 ) |
|
58 |
53 return prettyHtml |
59 return prettyHtml |
54 |
60 |
55 def tagPrettify(self, tag): |
61 def tagPrettify(self, tag): |
56 """ |
62 """ |
57 Public method to prettify HTML tags. |
63 Public method to prettify HTML tags. |
58 |
64 |
59 @param tag tag to be prettified |
65 @param tag tag to be prettified |
60 @type str |
66 @type str |
61 @return prettified tag |
67 @return prettified tag |
62 @rtype str |
68 @rtype str |
63 """ |
69 """ |
64 return re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag, |
70 return re.sub( |
65 flags=re.MULTILINE) |
71 " {{1,{0}}}".format(self.__indentWidth), " ", tag, flags=re.MULTILINE |
66 |
72 ) |
|
73 |
67 def commentPrettify(self, matchobj): |
74 def commentPrettify(self, matchobj): |
68 """ |
75 """ |
69 Public method to prettify HTML comments. |
76 Public method to prettify HTML comments. |
70 |
77 |
71 @param matchobj reference to the match object |
78 @param matchobj reference to the match object |
72 @type re.MatchObject |
79 @type re.MatchObject |
73 @return prettified comment |
80 @return prettified comment |
74 @rtype str |
81 @rtype str |
75 """ |
82 """ |