|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class to prettify HTML code. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import re |
|
13 |
|
14 |
|
15 from PyQt5.QtCore import QObject |
|
16 |
|
17 import Preferences |
|
18 |
|
19 |
|
20 class Html5Prettifier(QObject): |
|
21 """ |
|
22 Class implementing the HTML5 prettifier. |
|
23 """ |
|
24 def __init__(self, html, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param html HTML text to be prettified (string) |
|
29 @param parent reference to the parent object (QObject) |
|
30 """ |
|
31 super(Html5Prettifier, self).__init__(parent) |
|
32 |
|
33 self.__html = html |
|
34 self.__indentWidth = Preferences.getEditor("IndentWidth") |
|
35 |
|
36 def getPrettifiedHtml(self): |
|
37 """ |
|
38 Public method to prettify the HTML code. |
|
39 |
|
40 @return prettified HTML code (string) |
|
41 """ |
|
42 from bs4 import BeautifulSoup |
|
43 soup = BeautifulSoup(self.__html, "html.parser") |
|
44 prettyHtml = soup.prettify(formatter=self.tagPrettify) |
|
45 # prettify comments |
|
46 prettyHtml = re.sub("<!--(.*?)-->", self.commentPrettify, prettyHtml, |
|
47 flags=re.DOTALL) |
|
48 # indent all HTML |
|
49 prettyHtml = re.sub("^( *)(.*)$", |
|
50 r"{0}\2".format(r"\1" * self.__indentWidth), |
|
51 prettyHtml, flags=re.MULTILINE) |
|
52 |
|
53 return prettyHtml |
|
54 |
|
55 def tagPrettify(self, tag): |
|
56 """ |
|
57 Public method to prettify HTML tags. |
|
58 |
|
59 @param tag tag to be prettified (string) |
|
60 @return prettified tag (string) |
|
61 """ |
|
62 result = re.sub(" {{1,{0}}}".format(self.__indentWidth), " ", tag, |
|
63 flags=re.MULTILINE) |
|
64 return result |
|
65 |
|
66 def commentPrettify(self, matchobj): |
|
67 """ |
|
68 Public method to prettify HTML comments. |
|
69 |
|
70 @param matchobj reference to the match object (re.MatchObject) |
|
71 @return prettified comment (string) |
|
72 """ |
|
73 if re.search("\n", matchobj.group()): |
|
74 result = self.tagPrettify(matchobj.group()) |
|
75 else: |
|
76 result = matchobj.group() |
|
77 return result |