Package cssutils :: Package scripts :: Module csscombine
[hide private]
[frames] | no frames]

Source Code for Module cssutils.scripts.csscombine

 1  """resolves imports in a given CSS proxy sheet 
 2   
 3  issues 
 4      - URL or file hrefs? URI should be default and therefor baseURI is needed 
 5      - no nested @imports are resolved yet 
 6      - namespace rules are not working yet! 
 7          - @namespace must be resolved (all should be moved to top of main sheet? 
 8            but how are different prefixes resolved???) 
 9  """ 
10  import os 
11  import sys 
12  import cssutils 
13   
14 -def combine(proxy, srcenc='css', tarenc='utf-8', minified=True):
15 """ 16 TODO: 17 - encoding 18 - read conf 19 """ 20 src = cssutils.parse(proxy, encoding=srcenc) 21 sys.stderr.write('COMBINING %s\n' % proxy) 22 srcpath = os.path.dirname(proxy) 23 r = cssutils.css.CSSStyleSheet() 24 for rule in src.cssRules: 25 if rule.type == rule.IMPORT_RULE: 26 fn = os.path.join(srcpath, rule.href) 27 sys.stderr.write('* PROCESSING @import %s\n' % fn) 28 importsheet = cssutils.parse(fn, encoding=srcenc) 29 importsheet.encoding = None # remove @charset 30 r.insertRule(cssutils.css.CSSComment(cssText=u'/* %s */' % 31 rule.cssText)) 32 for x in importsheet.cssRules: 33 if x.type == x.IMPORT_RULE: 34 sys.stderr.write('WARN\tNo nested @imports: %s\n' % x.cssText) 35 # TODO: too simple if prefixes different in sheets! 36 # elif x.type == x.NAMESPACE_RULE: 37 # print 'INFO\tMoved to begin of sheet', x.cssText 38 # r.insertRule(x, 0) 39 else: 40 r.insertRule(x) 41 #r.insertRule(importsheet.cssRules) 42 43 # elif rule.type == rule.NAMESPACE_RULE: 44 # print 'INFO\tMoved to begin of sheet', rule.cssText 45 # r.insertRule(rule, 0) 46 else: 47 r.insertRule(rule) 48 49 r.encoding = tarenc 50 if minified: 51 cssutils.ser.prefs.useMinified() 52 return r.cssText
53 54
55 -def main(args=None):
56 import optparse 57 58 usage = "usage: %prog [options] URL" 59 parser = optparse.OptionParser(usage=usage) 60 parser.add_option('-s', '--srcenc', action='store', dest='srcenc', 61 default='css', 62 help='encoding of input, defaulting to "css". If given overwrites other encoding information like @charset declarations') 63 parser.add_option('-t', '--tarenc', action='store', dest='tarenc', 64 help='encoding of output, defaulting to "UTF-8"', default='utf-8') 65 parser.add_option('-m', '--minified', action='store_true', dest='minified', 66 help='saves minified version of combined files, defaults to False') 67 options, url = parser.parse_args() 68 69 if not url: 70 parser.error('no URL given') 71 else: 72 url = url[0] 73 74 print combine(url, options.srcenc, options.tarenc, options.minified)
75 76 77 if __name__ == '__main__': 78 sys.exit(main()) 79