用于压缩CSS的Python脚本?

49 投票
6 回答
19642 浏览
提问于 2025-04-11 18:48

我在找一个简单的Python脚本,能在网站部署过程中把CSS文件压缩一下。(服务器上只支持Python这种脚本语言,而像CSS Utils这样的完整解析器对这个项目来说太复杂了)。

简单来说,我想要一个像jsmin.py那样的工具,用来处理CSS。希望是一个没有任何依赖的单一脚本。

有没有什么好主意?

6 个回答

2

有一个很不错的在线工具 cssminifier,它还有一个简单易用的API接口。我写了一个小的Python脚本,可以把CSS文件的内容发送到这个工具的API,然后返回压缩后的CSS,并把它保存到一个名为"style.min.css"的文件里。我喜欢这个工具,因为它的代码很简洁,可以很好地集成到自动化部署的脚本中:

import requests
f = open("style.css", "r")
css_text = f.read()
f.close()
r = requests.post("http://cssminifier.com/raw", data={"input":css_text})
css_minified = r.text
f2 = open("style.min.css", "w")
f2.write(css_minified)
f2.close()
13

有一个可以在Python中使用的YUI CSS压缩工具。

你可以在PyPi上找到它的项目页面: http://pypi.python.org/pypi/cssmin/0.1.1

73

这看起来是一个很适合我入门Python的任务,我一直想学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的小技巧,除了下划线和/**/这两种方法!如果你用了很多小技巧,最好不要使用压缩工具(或者把这些小技巧放在一个单独的文件里)。

如果你对我的Python代码有任何建议,我会很感激。不过请轻点,毕竟这是我第一次写代码。:-)

撰写回答