python压缩HTML文件,python压缩javascript文件代码

本文介绍了一个使用正则表达式实现的JavaScript代码压缩工具,通过不同级别设置删除注释、空行和多余空白,提升代码密度。压缩过程包括临时替换字符串和正则表达式,以及精简不必要的空白。示例代码展示了如何在不同压缩级别下操作并测量压缩效果。
摘要由CSDN通过智能技术生成

通过正规表达式实现

'''

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

((?

''', re.VERBOSE)

# literals are temporarily replaced by numbered placeholders

literalMarker = '@_@%d@_@' # temporary replacement

backSubst = re.compile('@_@(\d+)@_@') # put the string literals back in

mlc1 = re.compile(r'(\/\*.*?\*\/)') # /* ... */ comments on single line

mlc = re.compile(r'(\/\*.*?\*\/)', re.DOTALL) # real multiline comments

slc = re.compile('\/\/.*') # remove single line comments

collapseWs = re.compile('(?<=\S)[ \t]+') # collapse successive non-leading white space characters into one

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)

# first, substitute string literals by placeholders to prevent the regexes messing with them

literals = []

def insertMarker(mo):

l = mo.group()

literals.append(l)

return self.literalMarker % (len(literals) - 1)

script = self.findLiterals.sub(insertMarker, script)

# now, to the literal-stripped carcass, apply some kludgy regexes for deflation...

script = self.slc.sub('', script) # strip single line comments

script = self.mlc1.sub(' ', script) # replace /* .. */ comments on single lines by space

script = self.mlc.sub('\n', script) # replace real multiline comments by newlines

# remove empty lines and trailing whitespace

script = '\n'.join([l.rstrip() for l in script.splitlines() if l.strip()])

if self.compressionLevel == 2: # squeeze out any dispensible whitespace

script = self.squeeze.sub('', script)

elif self.compressionLevel == 1: # only collapse multiple whitespace characters

script = self.collapseWs.sub(' ', script)

# now back-substitute the string and regex literals

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)

标签:

isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值