|
Description:
A regex-based JavaScript compression kludge.
The current version has been tested against mochikit and json.js, which indeed tripped up the previous version (see comments).
Source: Text Source
'''
a regex-based JavaScript code compression kludge
'''
import re
class JSCompressor(object):
def __init__(self, compressionLevel=2, measureCompression=False):
'''
compressionLevel:
0 - no compression, script returned unchanged. For debugging only -
try if you suspect that compression compromises your script
1 - Strip comments and empty lines, don't change line breaks and indentation (code remains readable)
2 - Additionally strip insignificant whitespace (code will become quite unreadable)
measureCompression: append a comment stating the extent of compression
'''
self.compressionLevel = compressionLevel
self.measureCompression = measureCompression
findLiterals = re.compile(r'''
(\'.*?(?<=[^\\])\') | # single-quoted strings
(\".*?(?<=[^\\])\") | # double-quoted strings
((?<![\*\/])\/(?![\/\*]).*?(?<![\\])\/) # JS regexes, trying hard not to be tripped up by comments
''', re.VERBOSE)
literalMarker = '@_@%d@_@'
backSubst = re.compile('@_@(\d+)@_@')
mlc1 = re.compile(r'(\/\*.*?\*\/)')
mlc = re.compile(r'(\/\*.*?\*\/)', re.DOTALL)
slc = re.compile('\/\/.*')
collapseWs = re.compile('(?<=\S)[ \t]+')
squeeze = re.compile('''
\s+(?=[\}\]\)\:\&\|\=\;\,\.\+]) | # remove whitespace preceding control characters
(?<=[\{\[\(\:\&\|\=\;\,\.\+])\s+ | # ... or following such
[ \t]+(?=\W) | # remove spaces or tabs preceding non-word characters
(?<=\W)[ \t]+ # ... or following such
'''
, re.VERBOSE | re.DOTALL)
def compress(self, script):
'''
perform compression and return compressed script
'''
if self.compressionLevel == 0:
return script
lengthBefore = len(script)
literals = []
def insertMarker(mo):
l = mo.group()
literals.append(l)
return self.literalMarker % (len(literals) - 1)
script = self.findLiterals.sub(insertMarker, script)
script = self.slc.sub('', script)
script = self.mlc1.sub(' ', script)
script = self.mlc.sub('\n', script)
script = '\n'.join([l.rstrip() for l in script.splitlines() if l.strip()])
if self.compressionLevel == 2:
script = self.squeeze.sub('', script)
elif self.compressionLevel == 1:
script = self.collapseWs.sub(' ', script)
def backsub(mo):
return literals[int(mo.group(1))]
script = self.backSubst.sub(backsub, script)
if self.measureCompression:
lengthAfter = float(len(script))
squeezedBy = int(100*(1-lengthAfter/lengthBefore))
script += '\n// squeezed out %s%%\n' % squeezedBy
return script
if __name__ == '__main__':
script = '''
/* this is a totally useless multiline comment, containing a silly "quoted string",
surrounded by several superfluous line breaks
*/
// and this is an equally important single line comment
sth = "this string contains 'quotes', a /regex/ and a // comment yet it will survive compression";
function wurst(){ // this is a great function
var hans = 33;
}
sthelse = 'and another useless string';
function hans(){ // another function
var bill = 66; // successive spaces will be collapsed into one;
var bob = 77 // this line break will be preserved b/c of lacking semicolon
var george = 88;
}
'''
for x in range(1,3):
print '\ncompression level', x, ':\n--------------'
c = JSCompressor(compressionLevel=x, measureCompression=True)
cpr = c.compress(script)
print cpr
print 'length', len(cpr)
Discussion:
|
|
Add comment
|
|
Number of comments: 5
Problem compressing MochiKit, Mike Webb, 2007/06/30
It is great to have a compress capability in python. I was trying this and ran into a problem compressing my scripts. I seem to hit a problem in how it compresses MochiKit.Base. I will see if I can track the issue down when I can.
Add comment
Buggy, alas, Andreas Gustafsson, 2007/11/06
This would be quite useful if it actually worked, but unfortunately
it doesn't. For example, compressing http://www.json.org/json2.js
at level 2 yields a script with syntax errors.
Add comment
An alternative JavaScript compressor, Andreas Gustafsson, 2007/11/06
There's another JavaScript compressor at
http://www.crockford.com/javascript/jsmin.py.txt.
It's working fine for me.
Add comment
json.js and mochikit now work, Michael Palmer, 2008/05/03
The problem was caused by "quoted phrases" in comments. This is now resolved; json.js and all example pages in the standard download have been tested and work.
Add comment
oops, Michael Palmer, 2008/05/03
'standard download' is 'mochikit standard download'.
Add comment
|
|
|