7
7
8
8
import re
9
9
10
- import logging as log
11
-
12
10
13
11
__all__ = ('html_minify' , )
14
12
@@ -19,7 +17,6 @@ def condense_html_whitespace(html):
19
17
>>> condense_html_whitespace('<i> <b> <a> test </a> </b> </i><br>')
20
18
'<i><b><a> test </a></b></i><br>'
21
19
""" # first space between tags, then empty new lines and in-between.
22
- log .debug ("Removing unnecessary HTML White Spaces and Empty New Lines." )
23
20
tagsStack = []
24
21
split = re .split ('(<\\ s*pre.*>|<\\ s*/\\ s*pre\\ s*>|<\\ s*textarea.*>|<\\ s*/\\ s*textarea\\ s*>)' , html , flags = re .IGNORECASE )
25
22
for i in range (0 , len (split )):
@@ -56,7 +53,6 @@ def condense_style(html):
56
53
>>> condense_style('<style type="text/css">*{border:0}</style><p>a b c')
57
54
'<style>*{border:0}</style><p>a b c'
58
55
""" # May look silly but Emmet does this and is wrong.
59
- log .debug ("Condensing HTML Style CSS tags." )
60
56
return html .replace ('<style type="text/css">' , '<style>' ).replace (
61
57
"<style type='text/css'>" , '<style>' ).replace (
62
58
"<style type=text/css>" , '<style>' )
@@ -68,7 +64,6 @@ def condense_script(html):
68
64
>>> condense_script('<script type="text/javascript"> </script><p>a b c')
69
65
'<script> </script><p>a b c'
70
66
""" # May look silly but Emmet does this and is wrong.
71
- log .debug ("Condensing HTML Script JS tags." )
72
67
return html .replace ('<script type="text/javascript">' , '<script>' ).replace (
73
68
"<style type='text/javascript'>" , '<script>' ).replace (
74
69
"<style type=text/javascript>" , '<script>' )
@@ -80,7 +75,6 @@ def clean_unneeded_html_tags(html):
80
75
>>> clean_unneeded_html_tags('a<body></img></td>b</th></tr></hr></br>c')
81
76
'abc'
82
77
"""
83
- log .debug ("Removing unnecessary optional HTML tags." )
84
78
for tag_to_remove in ("""</area> </base> <body> </body> </br> </col>
85
79
</colgroup> </dd> </dt> <head> </head> </hr> <html> </html> </img>
86
80
</input> </li> </link> </meta> </option> </param> <tbody> </tbody>
@@ -97,7 +91,6 @@ def remove_html_comments(html):
97
91
>>> _+= "<!-- kill me please -->keep" ; remove_html_comments(_)
98
92
'<!-- build:dev -->a<!-- endbuild -->b<!--[if IE 7]>c<![endif]--> keep'
99
93
""" # Grunt uses comments to as build arguments, bad practice but still.
100
- log .debug ("""Removing all unnecessary HTML comments.""" )
101
94
return re .compile (r'<!-- .*? -->' , re .I ).sub ('' , html )
102
95
103
96
@@ -107,7 +100,6 @@ def unquote_html_attributes(html):
107
100
>>> unquote_html_attributes('<img width="9" height="5" data-foo="0" >')
108
101
'<img width=9 height=5 data-foo=0 >'
109
102
""" # data-foo=0> might cause errors on IE, we leave 1 space data-foo=0 >
110
- log .debug ("Removing unnecessary Quotes on attributes of HTML tags." )
111
103
# cache all regular expressions on variables before we enter the for loop.
112
104
any_tag = re .compile (r"<\w.*?>" , re .I | re .MULTILINE | re .DOTALL )
113
105
space = re .compile (r' \s+|\s +' , re .MULTILINE )
@@ -144,12 +136,10 @@ def html_minify(html, comments=False):
144
136
>>> html_minify(' <p width="9" height="5" > <!-- a --> b </p> c <br> ')
145
137
'<p width=9 height=5 > b c <br>'
146
138
"""
147
- log .info ("Compressing HTML..." )
148
139
html = remove_html_comments (html ) if not comments else html
149
140
html = condense_style (html )
150
141
html = condense_script (html )
151
142
html = clean_unneeded_html_tags (html )
152
143
html = condense_html_whitespace (html )
153
144
html = unquote_html_attributes (html )
154
- log .info ("Finished compressing HTML !." )
155
145
return html .strip ()
0 commit comments