''' 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 # a bunch of regexes used in compression # first, exempt string and regex literals from compression by transient substitution findLiterals = re.compile(r''' (\'.*?(?<=[^\\])\') | # single-quoted strings (\".*?(?<=[^\\])\") | # double-quoted strings ((?