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

# Source Code for Module cssutils.scripts.csscombine'

      1  """combines sheets referred to by @import rules in a given CSS proxy sheet
      2  into a single new sheet.
      3
      4  - proxy currently is a path (no URI)
      5  - in @import rules only relative paths do work for now but should be used
      6    anyway
      7  - currently no nested @imports are resolved
      8  - messages are send to stderr
      9  - output to stdout.
     10
     11  Example::
     12
     13      csscombine sheets\csscombine-proxy.css -m -t ascii -s utf-8
     14          1>combined.css 2>log.txt
     15
     16  results in log.txt::
     17
     18      COMBINING sheets\csscombine-proxy.css
     19      USING SOURCE ENCODING: utf-8
     20      * PROCESSING @import sheets\csscombine-1.css
     21      * PROCESSING @import sheets\csscombine-2.css
     22      SETTING TARGET ENCODING: ascii
     23
     24  and combined.css::
     25
     26      @charset "ascii";a{color:green}body{color:#fff;background:#000}
     27
     28  or without option -m::
     29
     30      @charset "ascii";
     31      /* proxy sheet which imports sheets which should be combined \F6 \E4 \FC  */
     32      /* @import "csscombine-1.css"; */
     33      /* combined sheet 1 */
     34      a {
     35          color: green
     36          }
     37      /* @import url(csscombine-2.css); */
     38      /* combined sheet 2 */
     39      body {
     40          color: #fff;
     41          background: #000
     42          }
     43
     44  issues
     45
     46  - URL or file hrefs? URI should be default and therefor baseURI is needed
     47  - no nested @imports are resolved yet
     48  - namespace rules are not working yet!
     49      - @namespace must be resolved (all should be moved to top of main sheet?
     50        but how are different prefixes resolved???)
     51
     52  - maybe add a config file which is used?
     53
     54  """
     55  __all__ = ['csscombine']
     56  __docformat__ = 'restructuredtext'
     57  __version__ = '$Id: csscombine.py 1116 2008-03-05 13:52:23Z cthedot $'
     58
     59  import os
     60  import sys
     61  import cssutils
     62  from cssutils.serialize import CSSSerializer
     63
     64 -def csscombine(proxypath, sourceencoding=None, targetencoding='utf-8',
     65                 minify=True):
     66      """
     67      :returns: combined cssText
     68      :Parameters:
     69          `proxypath`:
     70              url or path to a CSSStyleSheet which imports other sheets which
     71              are then combined into one sheet
     72          `sourceencoding`:
     73              encoding of the source sheets including the proxy sheet
     74
     75          `targetencoding` = 'utf-8':
     76              encoding of the combined stylesheet
     77          `minify` = True:
     78              defines if the combined sheet should be minified
     79      """
     80      sys.stderr.write('COMBINING %s\n' % proxypath)
     81      if sourceencoding is not None:
     82          sys.stderr.write('USING SOURCE ENCODING: %s\n' % sourceencoding)
     83      src = cssutils.parse(proxypath, encoding=sourceencoding)
     84      srcpath = os.path.dirname(proxypath)
     85      r = cssutils.css.CSSStyleSheet()
     86      for rule in src.cssRules:
     87          if rule.type == rule.IMPORT_RULE:
     88              fn = os.path.join(srcpath, rule.href)
     89              sys.stderr.write('* PROCESSING @import %s\n' % fn)
     90              importsheet = cssutils.parse(fn, encoding=sourceencoding)
     91              importsheet.encoding = None # remove @charset
     92              r.insertRule(cssutils.css.CSSComment(cssText=u'/* %s */' %
     93                                                   rule.cssText))
     94              for x in importsheet.cssRules:
     95                  if x.type == x.IMPORT_RULE:
     96                      sys.stderr.write('WARN\tNo nested @imports: %s\n' % x.cssText)
     97                  # TODO: too simple if prefixes different in sheets!
     98  #                elif x.type == x.NAMESPACE_RULE:
     99  #                    print 'INFO\tMoved to begin of sheet', x.cssText
    100  #                    r.insertRule(x, 0)
    101                  else:
    102                      r.insertRule(x)
    103              #r.insertRule(importsheet.cssRules)
    104
    105  #        elif rule.type == rule.NAMESPACE_RULE:
    106  #            print 'INFO\tMoved to begin of sheet', rule.cssText
    107  #            r.insertRule(rule, 0)
    108          else:
    109              r.insertRule(rule)
    110
    111      sys.stderr.write('SETTING TARGET ENCODING: %s\n' % targetencoding)
    112      r.encoding = targetencoding
    113      if minify:
    114          # save old setting and use own serializer
    115          oldser = cssutils.ser
    116          cssutils.setSerializer(CSSSerializer())
    117          cssutils.ser.prefs.useMinified()
    118          cssText = r.cssText
    119          cssutils.setSerializer(oldser)
    120      else:
    121          cssText = r.cssText
    122      return cssText
    123
    124 -def main(args=None):
    125      import optparse
    126
    127      usage = "usage: %prog [options] path"
    128      parser = optparse.OptionParser(usage=usage)
    129      parser.add_option('-s', '--sourceencoding', action='store',
    130                        dest='sourceencoding', default='css',
    131          help='encoding of input, defaulting to "css". If given overwrites other encoding information like @charset declarations')
    132      parser.add_option('-t', '--targetencoding', action='store',
    133                        dest='targetencoding',
    134          help='encoding of output, defaulting to "UTF-8"', default='utf-8')
    135      parser.add_option('-m', '--minify', action='store_true', dest='minify',
    136                        default=False,
    137          help='saves minified version of combined files, defaults to False')
    138      options, path = parser.parse_args()
    139
    140      if not path:
    141          parser.error('no path given')
    142      else:
    143          path = path[0]
    144
    145      print csscombine(path, options.sourceencoding, options.targetencoding,
    146                       options.minify)
    147
    148
    149  if __name__ == '__main__':
    150      sys.exit(main())
    151