Minify on backend
This commit is contained in:
Executable
+4
@@ -0,0 +1,4 @@
|
||||
from csscombine import csscombine
|
||||
__all__ = ["csscapture", "csscombine", "cssparse"]
|
||||
|
||||
|
||||
Executable
+69
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python
|
||||
"""Retrieve all CSS stylesheets including embedded for a given URL.
|
||||
Retrieve as StyleSheetList or save to disk - raw, parsed or minified version.
|
||||
|
||||
TODO:
|
||||
- maybe use DOM 3 load/save?
|
||||
- logger class which handles all cases when no log is given...
|
||||
- saveto: why does urllib2 hang?
|
||||
"""
|
||||
__all__ = ['CSSCapture']
|
||||
__docformat__ = 'restructuredtext'
|
||||
__version__ = '$Id$'
|
||||
|
||||
from cssutils.script import CSSCapture
|
||||
import logging
|
||||
import optparse
|
||||
import sys
|
||||
|
||||
def main(args=None):
|
||||
usage = "usage: %prog [options] URL"
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option('-d', '--debug', action='store_true', dest='debug',
|
||||
help='show debug messages during capturing')
|
||||
parser.add_option('-m', '--minified', action='store_true', dest='minified',
|
||||
help='saves minified version of captured files')
|
||||
parser.add_option('-n', '--notsave', action='store_true', dest='notsave',
|
||||
help='if given files are NOT saved, only log is written')
|
||||
# parser.add_option('-r', '--saveraw', action='store_true', dest='saveraw',
|
||||
# help='if given saves raw css otherwise cssutils\' parsed files')
|
||||
parser.add_option('-s', '--saveto', action='store', dest='saveto',
|
||||
help='saving retrieved files to "saveto", defaults to "_CSSCapture_SAVED"')
|
||||
parser.add_option('-u', '--useragent', action='store', dest='ua',
|
||||
help='useragent to use for request of URL, default is urllib2s default')
|
||||
options, url = parser.parse_args()
|
||||
|
||||
# TODO:
|
||||
options.saveraw = False
|
||||
|
||||
if not url:
|
||||
parser.error('no URL given')
|
||||
else:
|
||||
url = url[0]
|
||||
|
||||
if options.debug:
|
||||
level = logging.DEBUG
|
||||
else:
|
||||
level = logging.INFO
|
||||
|
||||
# START
|
||||
c = CSSCapture(ua=options.ua, defaultloglevel=level)
|
||||
|
||||
stylesheetlist = c.capture(url)
|
||||
|
||||
if options.notsave is None or not options.notsave:
|
||||
if options.saveto:
|
||||
saveto = options.saveto
|
||||
else:
|
||||
saveto = u'_CSSCapture_SAVED'
|
||||
c.saveto(saveto, saveraw=options.saveraw, minified=options.minified)
|
||||
else:
|
||||
for i, s in enumerate(stylesheetlist):
|
||||
print u'''%s.
|
||||
encoding: %r
|
||||
title: %r
|
||||
href: %r''' % (i + 1, s.encoding, s.title, s.href)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Executable
+94
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env python
|
||||
"""Combine all sheets referred to a given CSS *proxy* sheet
|
||||
into a single new sheet.
|
||||
|
||||
- no ``url()`` values are adjusted so currently when using relative references
|
||||
for e.g. images it is best to have all sheets in a single folder
|
||||
- in @import rules only relative paths do work for now but should be used
|
||||
anyway
|
||||
- messages are send to stderr
|
||||
- output to stdout.
|
||||
|
||||
Example::
|
||||
|
||||
csscombine sheets\csscombine-proxy.css -m -t ascii -s utf-8
|
||||
1>combined.css 2>log.txt
|
||||
|
||||
results in log.txt::
|
||||
|
||||
COMBINING sheets/csscombine-proxy.css
|
||||
USING SOURCE ENCODING: css
|
||||
* PROCESSING @import sheets\csscombine-1.css
|
||||
* PROCESSING @import sheets\csscombine-2.css
|
||||
INFO Nested @imports are not combined: @import "1.css";
|
||||
SETTING TARGET ENCODING: ascii
|
||||
|
||||
and combined.css::
|
||||
|
||||
@charset "ascii";@import"1.css";@namespaces2"uri";s2|sheet-1{top:1px}s2|sheet-2{top:2px}proxy{top:3px}
|
||||
|
||||
or without option -m::
|
||||
|
||||
@charset "ascii";
|
||||
@import "1.css";
|
||||
@namespace s2 "uri";
|
||||
@namespace other "other";
|
||||
/* proxy sheet were imported sheets should be combined */
|
||||
/* non-ascii chars: \F6 \E4 \FC */
|
||||
/* @import "csscombine-1.css"; */
|
||||
/* combined sheet 1 */
|
||||
s2|sheet-1 {
|
||||
top: 1px
|
||||
}
|
||||
/* @import url(csscombine-2.css); */
|
||||
/* combined sheet 2 */
|
||||
s2|sheet-2 {
|
||||
top: 2px
|
||||
}
|
||||
proxy {
|
||||
top: 3px
|
||||
}
|
||||
|
||||
"""
|
||||
__all__ = ['csscombine']
|
||||
__docformat__ = 'restructuredtext'
|
||||
__version__ = '$Id$'
|
||||
|
||||
from cssutils.script import csscombine
|
||||
import optparse
|
||||
import sys
|
||||
|
||||
def main(args=None):
|
||||
usage = "usage: %prog [options] [path]"
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option('-u', '--url', action='store',
|
||||
dest='url',
|
||||
help='URL to parse (path is ignored if URL given)')
|
||||
parser.add_option('-s', '--sourceencoding', action='store',
|
||||
dest='sourceencoding',
|
||||
help='encoding of input, defaulting to "css". If given overwrites other encoding information like @charset declarations')
|
||||
parser.add_option('-t', '--targetencoding', action='store',
|
||||
dest='targetencoding',
|
||||
help='encoding of output, defaulting to "UTF-8"', default='utf-8')
|
||||
parser.add_option('-m', '--minify', action='store_true', dest='minify',
|
||||
default=False,
|
||||
help='saves minified version of combined files, defaults to False')
|
||||
options, path = parser.parse_args()
|
||||
|
||||
if options.url:
|
||||
print csscombine(url=options.url,
|
||||
sourceencoding=options.sourceencoding,
|
||||
targetencoding=options.targetencoding,
|
||||
minify=options.minify)
|
||||
elif path:
|
||||
print csscombine(path=path[0],
|
||||
sourceencoding=options.sourceencoding,
|
||||
targetencoding=options.targetencoding,
|
||||
minify=options.minify)
|
||||
else:
|
||||
parser.error('no path or URL (-u) given')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python
|
||||
"""utility script to parse given filenames or string
|
||||
"""
|
||||
__docformat__ = 'restructuredtext'
|
||||
__version__ = '$Id$'
|
||||
|
||||
import cssutils
|
||||
import logging
|
||||
import optparse
|
||||
import sys
|
||||
|
||||
def main(args=None):
|
||||
"""
|
||||
Parses given filename(s) or string or URL (using optional encoding) and
|
||||
prints the parsed style sheet to stdout.
|
||||
|
||||
Redirect stdout to save CSS. Redirect stderr to save parser log infos.
|
||||
"""
|
||||
usage = """usage: %prog [options] filename1.css [filename2.css ...]
|
||||
[>filename_combined.css] [2>parserinfo.log] """
|
||||
p = optparse.OptionParser(usage=usage)
|
||||
p.add_option('-s', '--string', action='store_true', dest='string',
|
||||
help='parse given string')
|
||||
p.add_option('-u', '--url', action='store', dest='url',
|
||||
help='parse given url')
|
||||
p.add_option('-e', '--encoding', action='store', dest='encoding',
|
||||
help='encoding of the file or override encoding found')
|
||||
p.add_option('-m', '--minify', action='store_true', dest='minify',
|
||||
help='minify parsed CSS', default=False)
|
||||
p.add_option('-d', '--debug', action='store_true', dest='debug',
|
||||
help='activate debugging output')
|
||||
|
||||
(options, params) = p.parse_args(args)
|
||||
|
||||
if not params and not options.url:
|
||||
p.error("no filename given")
|
||||
|
||||
if options.debug:
|
||||
p = cssutils.CSSParser(loglevel=logging.DEBUG)
|
||||
else:
|
||||
p = cssutils.CSSParser()
|
||||
|
||||
if options.minify:
|
||||
cssutils.ser.prefs.useMinified()
|
||||
|
||||
if options.string:
|
||||
sheet = p.parseString(u''.join(params), encoding=options.encoding)
|
||||
print sheet.cssText
|
||||
elif options.url:
|
||||
sheet = p.parseUrl(options.url, encoding=options.encoding)
|
||||
print sheet.cssText
|
||||
else:
|
||||
for filename in params:
|
||||
sys.stderr.write('=== CSS FILE: "%s" ===\n' % filename)
|
||||
sheet = p.parseFile(filename, encoding=options.encoding)
|
||||
print sheet.cssText
|
||||
print
|
||||
sys.stderr.write('\n')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user