यह मेरे लिए पाइथन में जाने के लिए एक अच्छा काम लग रहा था, जो थोड़ी देर के लिए लंबित है। मैं इसके द्वारा मेरा पहला कभी अजगर स्क्रिप्ट मौजूद:
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])
मेरा मानना है कि यह काम करने के लिए, और उत्पादन इस पर हाल ही में सफारी, ओपेरा, और Firefox ठीक परीक्षण करती है। यह अंडरस्कोर &/**/हैक्स के अलावा सीएसएस हैक्स तोड़ देगा! यदि आपके पास बहुत सारे हैक चल रहे हैं (या उन्हें एक अलग फ़ाइल में रखें) तो एक मिनीफायर का उपयोग न करें।
मेरी पायथन पर किसी भी सुझाव की सराहना की। कृपया नम्र हो हालांकि, यह मेरा पहला समय है। :-)
आप एक क्रम में पिछले तत्व का उल्लेख करने के सूचकांक -1 उपयोग कर सकते हैं। तो आप .insert() के बजाय .append() का उपयोग कर सकते हैं, और .reverse() से बचें। इसके अलावा, यदि लेन (एलएसटी)> 0: आमतौर पर किया जाता है जैसे कि lst: – recursive
युक्तियों के लिए धन्यवाद। मैंने इन और कुछ अन्य चीजों को ठीक कर दिया है। पायथन वास्तव में एक अच्छी भाषा है। :-) – Borgar
बस कमाल है। सबसे अच्छा हिस्सा यह है कि यह अब एक तैनाती स्क्रिप्ट का हिस्सा बनने जा रहा है! – Soviut