removed main.cfg, not used anywhere
[limo.git] / cssmin.py
blob64d8ffe1a789d22846294d40c03a6690aa43beeb
1 #!/usr/bin/env python
2 # encoding: utf-8
4 import re
6 # Constants for use in compression level setting.
7 NONE = 0
8 SIMPLE = 1
9 NORMAL = 2
10 FULL = 3
12 _REPLACERS = {
13 NONE: (None), # dummy
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
30 class CssMin:
31 def __init__(self, level=NORMAL):
32 self.level = level
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)
39 return 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__':
46 import sys
47 if len(sys.argv) <> 3:
48 print "Usage: %s <input-file> <output-file(can be the same)>" % sys.argv[0]
49 sys.exit(1)
51 f = open(sys.argv[1])
52 inputcss = f.read()
53 f.close()
54 open(sys.argv[2], 'w').write(minimalize(inputcss))