用于缩小CSS的Python脚本? [英] Python script for minifying CSS?

查看:135
本文介绍了用于缩小CSS的Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简单的Python脚本,可以将CSS缩小为网站部署过程的一部分。 (Python是服务器上唯一支持的脚本语言,而像 CSS Utils 这样的完整的解析器对此有些过分强调项目)。



基本上我想要 jsmin.py

解决方案

没有依赖关系的单一脚本



<这似乎是一个很好的任务,我进入python,这已经一段时间了。我现在介绍我的第一个python脚本:

  import sys,re 

css = open .argv [1],'r').read()

#删除注释 - 这将打破很多黑客:-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评论hack

#url()不需要引号
css = re.sub(r'url\(([\'])([^)] *) \ 1 \)',r'url(\2)',css)

#空格可以安全地折叠,因为生成的内容会折叠它们
css = re.sub (r'\s +','',css)

#缩短可折叠颜色:#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)

#片段值可以零散
css = re.sub(r':\s * 0(\.\d + ] m | e [mx] | in | p [ctx]))\s *;',r':\1;',css)

for re.findall '([^ {] +){([^}] *)}',css):

我们不需要运算符周围的空格
selectors = [re.sub r'(α<= [\ [\(> + =])\s + | \s +(?= [=〜^ $ * |> + \] \)

#顺序很重要,但我们仍然想要丢弃重复的
属性= {}
porder = []
for in re.findall('(。*?):(。*?)(; | $)',rule [1]):
key = prop [0] .strip()。lower()
如果key不在porder中:porder.append(key)
properties [key] = prop [1] .strip
$ b#输出规则如果它包含任何声明
如果属性:
print%s {%s}%(','。join(selectors),''。 [: - 1])$ ​​b $ b

$ b [%s:%s;'%(key,properties [key]
$ b

我相信这个工作,并输出它测试正常最近的Safari,Opera和Firefox。它会打破CSS黑客除了下划线& / ** / hacks!如果你有很多黑客(或把它们放在一个单独的文件),不要使用一个缩影。



我的python的任何提示赞赏。请温柔,虽然,这是我第一次。 : - )


I'm looking for a simple Python script that can minify CSS as part of a web-site deployment process. (Python is the only scripting language supported on the server and full-blown parsers like CSS Utils are overkill for this project).

Basically I'd like jsmin.py for CSS. A single script with no dependencies.

Any ideas?

解决方案

This seemed like a good task for me to get into python, which has been pending for a while. I hereby present my first ever python script:

import sys, re

css = open( sys.argv[1] , 'r' ).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] ) 

I believe this to work, and output it tests fine on recent Safari, Opera, and Firefox. It will break CSS hacks other than the underscore & /**/ hacks! Do not use a minifier if you have a lot of hacks going on (or put them in a separate file).

Any tips on my python appreciated. Please be gentle though, it's my first time. :-)

这篇关于用于缩小CSS的Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆