8
8
import re
9
9
import itertools
10
10
11
- import logging as log
12
-
13
11
from .variables import EXTENDED_NAMED_COLORS , CSS_PROPS_TEXT
14
12
15
13
@@ -78,7 +76,6 @@ def sort_properties(css_unsorted_string):
78
76
sort it by defined rule, and return sorted buffer if it's CSS property.
79
77
This function depends on '_prioritify' function.
80
78
"""
81
- log .debug ("Alphabetically Sorting all CSS / SCSS Properties." )
82
79
css_pgs = _compile_props (CSS_PROPS_TEXT , grouped = False ) # Do Not Group.
83
80
pattern = re .compile (r'(.*?{\r?\n?)(.*?)(}.*?)|(.*)' ,
84
81
re .DOTALL + re .MULTILINE )
@@ -102,7 +99,6 @@ def sort_properties(css_unsorted_string):
102
99
103
100
def remove_comments (css ):
104
101
"""Remove all CSS comment blocks."""
105
- log .debug ("Removing all Comments." )
106
102
iemac , preserve = False , False
107
103
comment_start = css .find ("/*" )
108
104
while comment_start >= 0 : # Preserve comments that look like `/*!...*/`.
@@ -132,7 +128,6 @@ def remove_comments(css):
132
128
133
129
def remove_unnecessary_whitespace (css ):
134
130
"""Remove unnecessary whitespace characters."""
135
- log .debug ("Removing all unnecessary white spaces." )
136
131
137
132
def pseudoclasscolon (css ):
138
133
"""Prevent 'p :link' from becoming 'p:link'.
@@ -168,19 +163,16 @@ def pseudoclasscolon(css):
168
163
169
164
def remove_unnecessary_semicolons (css ):
170
165
"""Remove unnecessary semicolons."""
171
- log .debug ("Removing all unnecessary semicolons." )
172
166
return re .sub (r";+\}" , "}" , css )
173
167
174
168
175
169
def remove_empty_rules (css ):
176
170
"""Remove empty rules."""
177
- log .debug ("Removing all unnecessary empty rules." )
178
171
return re .sub (r"[^\}\{]+\{\}" , "" , css )
179
172
180
173
181
174
def normalize_rgb_colors_to_hex (css ):
182
175
"""Convert `rgb(51,102,153)` to `#336699`."""
183
- log .debug ("Converting all rgba to hexadecimal color values." )
184
176
regex = re .compile (r"rgb\s*\(\s*([0-9,\s]+)\s*\)" )
185
177
match = regex .search (css )
186
178
while match :
@@ -193,14 +185,12 @@ def normalize_rgb_colors_to_hex(css):
193
185
194
186
def condense_zero_units (css ):
195
187
"""Replace `0(px, em, %, etc)` with `0`."""
196
- log .debug ("Condensing all zeroes on values." )
197
188
return re .sub (r"([\s:])(0)(px|em|%|in|q|ch|cm|mm|pc|pt|ex|rem|s|ms|"
198
189
r"deg|grad|rad|turn|vw|vh|vmin|vmax|fr)" , r"\1\2" , css )
199
190
200
191
201
192
def condense_multidimensional_zeros (css ):
202
193
"""Replace `:0 0 0 0;`, `:0 0 0;` etc. with `:0;`."""
203
- log .debug ("Condensing all multidimensional zeroes on values." )
204
194
return css .replace (":0 0 0 0;" , ":0;" ).replace (
205
195
":0 0 0;" , ":0;" ).replace (":0 0;" , ":0;" ).replace (
206
196
"background-position:0;" , "background-position:0 0;" ).replace (
@@ -209,13 +199,11 @@ def condense_multidimensional_zeros(css):
209
199
210
200
def condense_floating_points (css ):
211
201
"""Replace `0.6` with `.6` where possible."""
212
- log .debug ("Condensing all floating point values." )
213
202
return re .sub (r"(:|\s)0+\.(\d+)" , r"\1.\2" , css )
214
203
215
204
216
205
def condense_hex_colors (css ):
217
206
"""Shorten colors from #AABBCC to #ABC where possible."""
218
- log .debug ("Condensing all hexadecimal color values." )
219
207
regex = re .compile (
220
208
r"""([^\"'=\s])(\s*)#([0-9a-f])([0-9a-f])([0-9a-f])"""
221
209
r"""([0-9a-f])([0-9a-f])([0-9a-f])""" , re .I | re .S )
@@ -234,19 +222,16 @@ def condense_hex_colors(css):
234
222
235
223
def condense_whitespace (css ):
236
224
"""Condense multiple adjacent whitespace characters into one."""
237
- log .debug ("Condensing all unnecessary white spaces." )
238
225
return re .sub (r"\s+" , " " , css )
239
226
240
227
241
228
def condense_semicolons (css ):
242
229
"""Condense multiple adjacent semicolon characters into one."""
243
- log .debug ("Condensing all unnecessary multiple adjacent semicolons." )
244
230
return re .sub (r";;+" , ";" , css )
245
231
246
232
247
233
def wrap_css_lines (css , line_length = 80 ):
248
234
"""Wrap the lines of the given CSS to an approximate length."""
249
- log .debug ("Wrapping lines to ~{0} max line lenght." .format (line_length ))
250
235
lines , line_start = [], 0
251
236
for i , char in enumerate (css ):
252
237
# Its safe to break after } characters.
@@ -260,14 +245,12 @@ def wrap_css_lines(css, line_length=80):
260
245
261
246
def condense_font_weight (css ):
262
247
"""Condense multiple font weights into shorter integer equals."""
263
- log .debug ("Condensing font weights on values." )
264
248
return css .replace ('font-weight:normal;' , 'font-weight:400;' ).replace (
265
249
'font-weight:bold;' , 'font-weight:700;' )
266
250
267
251
268
252
def condense_std_named_colors (css ):
269
253
"""Condense named color values to shorter replacement using HEX."""
270
- log .debug ("Condensing standard named color values." )
271
254
for color_name , color_hexa in iter (tuple ({
272
255
':aqua;' : ':#0ff;' , ':blue;' : ':#00f;' ,
273
256
':fuchsia;' : ':#f0f;' , ':yellow;' : ':#ff0;' }.items ())):
@@ -277,7 +260,6 @@ def condense_std_named_colors(css):
277
260
278
261
def condense_xtra_named_colors (css ):
279
262
"""Condense named color values to shorter replacement using HEX."""
280
- log .debug ("Condensing extended named color values." )
281
263
for k , v in iter (tuple (EXTENDED_NAMED_COLORS .items ())):
282
264
same_color_but_rgb = 'rgb({0},{1},{2})' .format (v [0 ], v [1 ], v [2 ])
283
265
if len (k ) > len (same_color_but_rgb ):
@@ -287,19 +269,16 @@ def condense_xtra_named_colors(css):
287
269
288
270
def remove_url_quotes (css ):
289
271
"""Fix for url() does not need quotes."""
290
- log .debug ("Removing quotes from url." )
291
272
return re .sub (r'url\((["\'])([^)]*)\1\)' , r'url(\2)' , css )
292
273
293
274
294
275
def condense_border_none (css ):
295
276
"""Condense border:none; to border:0;."""
296
- log .debug ("Condense borders values." )
297
277
return css .replace ("border:none;" , "border:0;" )
298
278
299
279
300
280
def add_encoding (css ):
301
281
"""Add @charset 'UTF-8'; if missing."""
302
- log .debug ("Adding encoding declaration if needed." )
303
282
return '@charset "utf-8";' + css if "@charset" not in css .lower () else css
304
283
305
284
@@ -312,13 +291,11 @@ def restore_needed_space(css):
312
291
313
292
def unquote_selectors (css ):
314
293
"""Fix CSS for some specific selectors where Quotes is not needed."""
315
- log .debug ("Removing unnecessary Quotes on selectors of CSS classes." )
316
294
return re .compile ('([a-zA-Z]+)="([a-zA-Z0-9-_\.]+)"]' ).sub (r'\1=\2]' , css )
317
295
318
296
319
297
def css_minify (css , wrap = False , comments = False , sort = False , noprefix = False ):
320
298
"""Minify CSS main function."""
321
- log .info ("Compressing CSS..." )
322
299
css = remove_comments (css ) if not comments else css
323
300
css = sort_properties (css ) if sort else css
324
301
css = unquote_selectors (css )
@@ -339,5 +316,4 @@ def css_minify(css, wrap=False, comments=False, sort=False, noprefix=False):
339
316
css = condense_semicolons (css )
340
317
css = add_encoding (css ) if not noprefix else css
341
318
css = restore_needed_space (css )
342
- log .info ("Finished compressing CSS !." )
343
319
return css .strip ()
0 commit comments