栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用于缩小CSS的Python脚本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用于缩小CSS的Python脚本?

对于我来说,进入已经等待了一段时间的python似乎是一项艰巨的任务。我特此介绍我的第一个python脚本:

import sys, rewith open( sys.argv[1] , 'r' ) as f:    css = f.read()# remove comments - this will break a lot of hacks :-Pcss = re.sub( r's*', "$$HACK1$$", css ) # preserve IE<6 comment hackcss = re.sub( r'', "", css )css = css.replace( "$$HACK1$$", '' ) # preserve IE<6 comment hack# url() doesn't need quotescss = re.sub( r'url((["'])([^)]*)1)', r'url(2)', css )# spaces may be safely collapsed as generated content will collapse them anywaycss = re.sub( r's+', ' ', css )# shorten collapsable colors: #aabbcc to #abccss = re.sub( r'#([0-9a-f])1([0-9a-f])2([0-9a-f])3(s|;)', r'#1234', css )# fragment values can loose zeroscss = 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的任何提示表示赞赏。不过请温柔,这是我的第一次。:-)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号