代码之家  ›  专栏  ›  技术社区  ›  Will Moffat

用于压缩CSS的Python脚本?[关闭]

  •  49
  • Will Moffat  · 技术社区  · 17 年前

    我正在寻找一个简单的Python脚本,可以在网站部署过程中压缩CSS。(Python是服务器上唯一支持的脚本语言,像 CSS Utils 对这个项目来说太过分了)。

    基本上我想 jsmin.py CSS。一个没有依赖关系的单一脚本。

    有什么想法吗?

    6 回复  |  直到 17 年前
        1
  •  71
  •   vallentin Remi    7 年前

    这对我来说似乎是一个很好的学习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黑客攻击;/**/黑客!如果你有很多黑客正在进行(或将它们放在一个单独的文件中),请不要使用minifier。

    任何关于我的python的提示都很感激。请温柔一点,这是我第一次。 :-)

        2
  •  12
  •   Gregor Müllegger    16 年前

    YUI的CSS压缩器有一个端口可用于python。

    以下是PyPi上的项目页面: http://pypi.python.org/pypi/cssmin/0.1.1

        3
  •  2
  •   Yahya Yahyaoui    11 年前

    有一个不错的在线工具 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()
    
        4
  •  2
  •   Community Mohan Dere    5 年前

    如果有人遇到这个问题并正在使用Django,有一个常用的包叫做 Django Compressor :

    将链接和内联的JavaScript或CSS压缩到一个缓存文件中。

    • JS/CSS属于模板

    • 灵活性

    • 它不会碍事的

    • 完整的测试套件

        5
  •  1
  •   Jeffrey Martinez    17 年前

    我不知道有什么现成的python css压缩程序,但就像你说的css-utils有这个选项。在检查并验证许可证允许后,您可以浏览源代码并剪掉自己进行缩小的部分。然后把这个放在一个脚本里,瞧!好了。

    首先,中的csscombine函数。../trunk/src/csstuils/script.py似乎在第361行附近的某个地方进行了压缩(我查看了1499版)。请注意名为“minify”的布尔函数参数。

        6
  •  1
  •   alexandrul    10 年前

    在the webassets 您可以在文档中找到多个压缩器和编译器的链接。从我选择的名单中 pyScss ,这也会缩小生成的CSS。

    如果你只需要一个CSS压缩器,你可以试试 csscompressor :

    几乎是YUI CSS压缩器的精确端口。通过所有原始单元测试。

    一个更通用的工具是 css-html-prettify :

    StandAlone异步单文件跨平台Unicode就绪的Python3 Web美化器。

    推荐文章