Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge https://github.com/glyphobet/clevercss

Conflicts:
	setup.py
  • Loading branch information...
commit ebdee649c8bab4f70d1678be5da7ee6e8dac8a52 2 parents 54cb5b3 + 9ccbf3a
Petr Viktorin authored

Showing 2 changed files with 143 additions and 1 deletion. Show diff stats Hide diff stats

  1. 141  bin/ccss
  2. 3  setup.py
141  bin/ccss
... ...
@@ -0,0 +1,141 @@
  1
+#!/usr/bin/env python
  2
+
  3
+from optparse import OptionParser
  4
+import re
  5
+import sys
  6
+
  7
+import clevercss
  8
+from clevercss.errors import *
  9
+
  10
+help_text = '''
  11
+usage: %prog <file 1> ... <file n>
  12
+  if called with some filenames it will read each file, cut of
  13
+  the extension and append a ".css" extension and save. If 
  14
+  the target file has the same name as the source file it will
  15
+  abort, but if it overrides a file during this process it will
  16
+  continue. This is a desired functionality. To avoid that you
  17
+  must not give your source file a .css extension.
  18
+
  19
+  if you call it without arguments it will read from stdin and
  20
+  write the converted css to stdout.
  21
+'''
  22
+
  23
+version_text = '''\
  24
+CleverCSS Version %s
  25
+Licensed under the BSD license.
  26
+(c) Copyright 2007 by Armin Ronacher and Georg Brandl
  27
+(c) Copyright 2010 by Jared Forsyth''' % clevercss.VERSION
  28
+
  29
+def main():
  30
+    parser = OptionParser(usage=help_text, version=version_text)
  31
+    parser.add_option('--eigen-test', action='store_true',
  32
+            help='evaluate the example from the docstring')
  33
+    parser.add_option('--list-colors', action='store_true',
  34
+            help='list all known coor names')
  35
+    parser.add_option('-n', '--no-overwrite', action='store_true', dest='no_overwrite',
  36
+            help='don\'t overwrite any files (default=false)')
  37
+    parser.add_option('--to-ccss', action='store_true',
  38
+            help='convert css files to ccss')
  39
+    parser.add_option('--minified', action='store_true',
  40
+            help='minify the resulting css')
  41
+
  42
+    (options, args) = parser.parse_args()
  43
+    if options.eigen_test:
  44
+        print do_test()
  45
+    elif options.list_colors:
  46
+        list_colors()
  47
+    elif options.to_ccss:
  48
+        for arg in args:
  49
+            print cleverfy(arg)
  50
+    elif len(args):
  51
+        convert_many(args, options)
  52
+    else:
  53
+        convert_stream()
  54
+
  55
+import cssutils
  56
+import logging
  57
+cssutils.log.setLevel(logging.FATAL)
  58
+
  59
+def parseCSS(text):
  60
+    parser = cssutils.CSSParser()
  61
+    css = parser.parseString(text)
  62
+    rules = {}
  63
+    for rule in css.cssRules:
  64
+        commas = rule.selectorText.split(',')
  65
+        for comma in commas:
  66
+            parts = comma.split()
  67
+            c = rules
  68
+            for i, part in enumerate(parts):
  69
+                if part in '>+':
  70
+                    parts[i+1] = '&' + part + parts[i+1]
  71
+                    continue
  72
+                c = c.setdefault(part, {})
  73
+            c.setdefault(':rules:', []).append(rule)
  74
+    return rules
  75
+
  76
+def rulesToCCSS(selector, rules):
  77
+    text = selector + ':\n  '
  78
+    if rules.get(':rules:'):
  79
+        text += '\n\n  '.join('\n  '.join(line.strip().rstrip(';') for line in rule.style.cssText.splitlines()) for rule in rules.get(':rules:', [])) + '\n'
  80
+    for other in rules:
  81
+        if other == ':rules:':
  82
+            continue
  83
+        text += '\n  ' + rulesToCCSS(other, rules[other]).replace('\n', '\n  ')
  84
+    return text
  85
+
  86
+def cleverfy(fname):
  87
+    rules = parseCSS(open(fname).read())
  88
+    text = ''
  89
+    for rule in rules:
  90
+        text += rulesToCCSS(rule, rules[rule]) + '\n\n'
  91
+    return text
  92
+
  93
+def do_test():
  94
+    rx = re.compile(r'Example::\n(.*?)__END__(?ms)')
  95
+    text = rx.search(clevercss.__doc__).group(1)
  96
+    ccss = '\n'.join(line[8:].rstrip() for line in text.splitlines())
  97
+    return clevercss.convert(ccss)
  98
+
  99
+def list_colors():
  100
+    print '%d known colors:' % len(clevercss.consts.COLORS)
  101
+    for color in sorted(clevercss.consts.COLORS.items()):
  102
+        print '  %-30s%s' % color
  103
+
  104
+def convert_stream():
  105
+    import sys
  106
+    try:
  107
+        print clevercss.convert(sys.stdin.read())
  108
+    except (ParserError, EvalException), e:
  109
+        sys.stderr.write('Error: %s\n' % e)
  110
+        sys.exit(1)
  111
+
  112
+def convert_many(files, options):
  113
+    for fname in files:
  114
+        target = fname.rsplit('.', 1)[0] + '.css'
  115
+        if fname == target:
  116
+            sys.stderr.write('Error: same name for '
  117
+                             'source and target file "%s".' % fname)
  118
+            sys.exit(2)
  119
+        elif options.no_overwrite and os.path.exists(target):
  120
+            sys.stderr.write('File exists (and --no-overwrite was used) "%s".' % target)
  121
+            sys.exit(3)
  122
+
  123
+        src = open(fname)
  124
+        try:
  125
+            try:
  126
+                converted = clevercss.convert(src.read(), fname=fname)
  127
+            except (ParserError, EvalException), e:
  128
+                sys.stderr.write('Error in file %s: %s\n' % (fname, e))
  129
+                sys.exit(1)
  130
+            dst = open(target, 'w')
  131
+            try:
  132
+                print 'Writing output to %s...' % target
  133
+                dst.write(converted)
  134
+            finally:
  135
+                dst.close()
  136
+        finally:
  137
+            src.close()
  138
+
  139
+if __name__ == '__main__':
  140
+    main()
  141
+
3  setup.py
@@ -19,9 +19,10 @@
19 19
     maintainer_email='david.ziegler@gmail.com',
20 20
     version='0.2.2.dev',
21 21
     url='http://sandbox.pocoo.org/clevercss/',
22  
-    download_url='https://github.com/guileen/clevercss3/tree',
23 22
     py_modules=['extract_sprites'],
  23
+    download_url='http://github.com/dziegler/clevercss/tree',
24 24
     packages=['clevercss'],
  25
+    scripts=['bin/ccss'],
25 26
     description='python inspired sass-like css preprocessor',
26 27
     long_description=readme_text,
27 28
     install_requires = req_modules,

0 notes on commit ebdee64

Please sign in to comment.
Something went wrong with that request. Please try again.