6 # Constants for use in compression level setting.
14 SIMPLE
: ((r
'\/\*.{4,}?\*\/', ''), # comment
15 (r
'\n\s*\n', r
"\n"), # empty new lines
16 (r
'(^\s*\n)|(\s*\n$)', "")), # new lines at start or end
17 NORMAL
: ((r
'/\*.{4,}?\*/', ''), # comments
18 (r
"\n", ""), # delete new lines
19 ('[\t ]+', " "), # change spaces and tabs to one space
20 (r
'\s?([;:{},+>])\s?', r
"\1"), # delete space where it is not needed, change ;} to }
21 (r
';}', "}"), # because semicolon is not needed there
22 (r
'}', r
"}\n")), # add new line after each rule
23 FULL
: ((r
'\/\*.*?\*\/', ''), # comments
24 (r
"\n", ""), # delete new lines
25 (r
'[\t ]+', " "), # change spaces and tabs to one space
26 (r
'\s?([;:{},+>])\s?', r
"\1"), # delete space where it is not needed, change ;} to }
27 (r
';}', "}")), # because semicolon is not needed there
31 def __init__(self
, level
=NORMAL
):
34 def compress(self
, css
):
35 """Tries to minimize the length of CSS code passed as parameter. Returns string."""
36 css
= css
.replace("\r\n", "\n") # get rid of Windows line endings, if they exist
37 for rule
in _REPLACERS
[self
.level
]:
38 css
= re
.compile(rule
[0], re
.MULTILINE|re
.UNICODE|re
.DOTALL
).sub(rule
[1], css
)
41 def minimalize(css
, level
=NORMAL
):
42 """Compress css using level method and return new css as a string."""
43 return CssMin(level
).compress(css
)
45 if __name__
== '__main__':
47 if len(sys
.argv
) <> 3:
48 print "Usage: %s <input-file> <output-file(can be the same)>" % sys
.argv
[0]
54 open(sys
.argv
[2], 'w').write(minimalize(inputcss
))