2024-09-26 17:49:17 发布
网友
我正在寻找一个简单的Python脚本,它可以作为web站点部署过程的一部分缩小CSS。(Python是服务器上唯一支持的脚本语言,而像CSS Utils这样成熟的语法分析器对于这个项目来说是过分的)。
基本上我想用jsmin.py来表示CSS。一个没有依赖关系的脚本。
有什么想法吗?
如果有人在这个问题上登陆并且正在使用Django,那么有一个用于这个问题的常用包叫做Django Compressor:
Compresses linked and inline JavaScript or CSS into a single cached file.JS/CSS belong in the templatesFlexibilityIt doesn’t get in the wayFull test suite
Compresses linked and inline JavaScript or CSS into a single cached file.
JS/CSS belong in the templates
Flexibility
It doesn’t get in the way
Full test suite
这对我来说是一个很好的进入python的任务,它已经被搁置了一段时间。我在此展示我的第一个python脚本:
import sys, re with open( sys.argv[1] , 'r' ) as f: css = f.read() # remove comments - this will break a lot of hacks :-P css = re.sub( r'\s*/\*\s*\*/', "$$HACK1$$", css ) # preserve IE<6 comment hack css = re.sub( r'/\*[\s\S]*?\*/', "", css ) css = css.replace( "$$HACK1$$", '/**/' ) # preserve IE<6 comment hack # url() doesn't need quotes css = re.sub( r'url\((["\'])([^)]*)\1\)', r'url(\2)', css ) # spaces may be safely collapsed as generated content will collapse them anyway css = re.sub( r'\s+', ' ', css ) # shorten collapsable colors: #aabbcc to #abc css = re.sub( r'#([0-9a-f])\1([0-9a-f])\2([0-9a-f])\3(\s|;)', r'#\1\2\3\4', css ) # fragment values can loose zeros css = re.sub( r':\s*0(\.\d+([cm]m|e[mx]|in|p[ctx]))\s*;', r':\1;', css ) for rule in re.findall( r'([^{]+){([^}]*)}', css ): # we don't need spaces around operators selectors = [re.sub( r'(?<=[\[\(>+=])\s+|\s+(?=[=~^$*|>+\]\)])', r'', selector.strip() ) for selector in rule[0].split( ',' )] # order is important, but we still want to discard repetitions properties = {} porder = [] for prop in re.findall( '(.*?):(.*?)(;|$)', rule[1] ): key = prop[0].strip().lower() if key not in porder: porder.append( key ) properties[ key ] = prop[1].strip() # output rule if it contains any declarations if properties: print "%s{%s}" % ( ','.join( selectors ), ''.join(['%s:%s;' % (key, properties[key]) for key in porder])[:-1] )
我相信这是可行的,并且在最近的Safari、Opera和Firefox上输出它的测试结果很好。它将打破除下划线之外的CSS黑客攻击&;/**/hacks!如果你有很多黑客的话,不要使用小型化程序(或者把它们放在一个单独的文件中)。
关于我的Python的任何提示都很感激。不过,请温柔一点,这是我第一次。:-)
YUI的CSS压缩器有一个端口可用于python。
这是它在PyPi上的项目页面: http://pypi.python.org/pypi/cssmin/0.1.1
如果有人在这个问题上登陆并且正在使用Django,那么有一个用于这个问题的常用包叫做Django Compressor:
这对我来说是一个很好的进入python的任务,它已经被搁置了一段时间。我在此展示我的第一个python脚本:
我相信这是可行的,并且在最近的Safari、Opera和Firefox上输出它的测试结果很好。它将打破除下划线之外的CSS黑客攻击&;/**/hacks!如果你有很多黑客的话,不要使用小型化程序(或者把它们放在一个单独的文件中)。
关于我的Python的任何提示都很感激。不过,请温柔一点,这是我第一次。:-)
YUI的CSS压缩器有一个端口可用于python。
这是它在PyPi上的项目页面: http://pypi.python.org/pypi/cssmin/0.1.1
相关问题 更多 >
编程相关推荐