summaryrefslogtreecommitdiff
path: root/build_system/cmpcss
diff options
context:
space:
mode:
Diffstat (limited to 'build_system/cmpcss')
-rwxr-xr-xbuild_system/cmpcss125
1 files changed, 125 insertions, 0 deletions
diff --git a/build_system/cmpcss b/build_system/cmpcss
new file mode 100755
index 0000000..d347a63
--- /dev/null
+++ b/build_system/cmpcss
@@ -0,0 +1,125 @@
1#!/usr/bin/python
2###############
3# cmpcss
4# Compress a CSS file in prepration for production use.
5#
6# AUTHOR
7# Michael Crute (mcrute@gmail.com)
8# DATE
9# 16 June 2006
10# VERSION
11# 1.0
12# PURPOSE
13# An implementation of a CSS compressor in Python. Written to
14# prepare development CSS to production ready CSS. Does the
15# following:
16# * Removes spaces intelligently
17# * Removes charset declarations
18# * Removes extra semicolons
19# * Flattens the file (tabs and line breaks)
20# USAGE
21# cmpcss site_dev.css > site_prod.css
22# LICENSE
23# You may copy and redistribute this program as you see fit, with no
24# restrictions.
25# WARRANTY
26# This program comes with NO warranty, real or implied. If it eats
27# your CSS, it's not my problem, you should have kept a backup.
28###############
29
30import sys
31import re
32#import string
33
34# The chewy caramel center of the program
35# Don't bitch about the short variable names, it makes it easy to read
36def cleanline(theLine):
37 # Kills line breaks, tabs, and double spaces
38 p = re.compile('(\n|\r|\t|\f|\v)+')
39 m = p.sub('',theLine)
40
41 # Kills double spaces
42 p = re.compile('( )+')
43 m = p.sub(' ',m)
44
45 # Removes last semicolon before }
46 p = re.compile('(; }|;})+')
47 m = p.sub('}',m)
48
49 # Removes space before {
50 p = re.compile('({ )+')
51 m = p.sub('{',m)
52
53 # Removes all comments
54 p = re.compile('/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/')
55 m = p.sub('',m)
56
57 # Strip off the Charset
58 p = re.compile('@CHARSET .*;')
59 m = p.sub('',m)
60
61 # Strip spaces before the {
62 p = re.compile(' {')
63 m = p.sub('{',m)
64
65 # Strip space after :
66 p = re.compile(': ')
67 m = p.sub(':',m)
68
69 # Strip space after ,
70 p = re.compile(', ')
71 m = p.sub(',',m)
72
73 # Strip space after ;
74 p = re.compile('; ')
75 m = p.sub(';',m)
76
77# May return to add color code compression
78# not a huge space saver so also not high priority
79# p = re.compile('.*#[0-9A-F]{6}.*')
80# cc = p.match(m)
81
82# if cc:
83# cc = compcolor(m)
84
85 return m
86
87#def compcolor(m):
88
89# Boring usage info for people too dumb to use the program
90def printusage():
91 print ''
92 print 'SoftGroup CSS Compressor'
93 print ''
94 print 'This is a compressor for CSS files.'
95 print 'As far as I know it doesn\'t break the standards'
96 print 'compliance of the file. If you can find a way'
97 print 'to make it break a file please email me.'
98 print ''
99 print 'Usage: cmpcss <filename>'
100 sys.exit()
101
102# The main routine
103if ( __name__ == "__main__" ):
104 if (len(sys.argv) <= 1):
105 printusage()
106
107 # Open the file for reading
108 try:
109 theFile = file(sys.argv[1], "r")
110 except IOError:
111 print 'I couldn\'t open your file.'
112 sys.exit()
113
114 # Initialize temp so it's not scoped to the for loop
115 temp = ''
116
117 # Loop through the file line by line and clean it
118 for line in theFile:
119 temp = temp + cleanline(line)
120
121 # Once more, clean the entire file string
122 print cleanline(temp)
123
124 # Cleanup after ourselves
125 theFile.close()