Mercurial > piecrust2
comparison piecrust/processing/compressors.py @ 315:d1490028e211
cleancss: Add option to specify an output extension, like `.min.css`.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 27 Mar 2015 20:47:31 -0700 |
parents | cba781477bd0 |
children | 0ab712eab0fb |
comparison
equal
deleted
inserted
replaced
314:1ddd18ad5e76 | 315:d1490028e211 |
---|---|
1 import os | 1 import os |
2 import os.path | 2 import os.path |
3 import logging | 3 import logging |
4 import platform | 4 import platform |
5 import subprocess | 5 import subprocess |
6 from piecrust.processing.base import SimpleFileProcessor | 6 from piecrust.processing.base import Processor, SimpleFileProcessor |
7 | 7 |
8 | 8 |
9 logger = logging.getLogger(__name__) | 9 logger = logging.getLogger(__name__) |
10 | 10 |
11 | 11 |
12 class CleanCssProcessor(SimpleFileProcessor): | 12 class CleanCssProcessor(Processor): |
13 PROCESSOR_NAME = 'cleancss' | 13 PROCESSOR_NAME = 'cleancss' |
14 | 14 |
15 def __init__(self): | 15 def __init__(self): |
16 super(CleanCssProcessor, self).__init__({'css': 'css'}) | 16 super(CleanCssProcessor, self).__init__() |
17 self._conf = None | 17 self._conf = None |
18 | 18 |
19 def _doProcess(self, in_path, out_path): | 19 def matches(self, path): |
20 return path.endswith('.css') | |
21 | |
22 def getOutputFilenames(self, filename): | |
20 self._ensureInitialized() | 23 self._ensureInitialized() |
24 basename, _ = os.path.splitext(filename) | |
25 return ['%s%s' % (basename, self._conf['out_ext'])] | |
26 | |
27 def process(self, path, out_dir): | |
28 self._ensureInitialized() | |
29 | |
30 _, in_name = os.path.split(path) | |
31 out_name = self.getOutputFilenames(in_name)[0] | |
32 out_path = os.path.join(out_dir, out_name) | |
21 | 33 |
22 args = [self._conf['bin'], '-o', out_path] | 34 args = [self._conf['bin'], '-o', out_path] |
23 args += self._conf['options'] | 35 args += self._conf['options'] |
24 args.append(in_path) | 36 args.append(in_path) |
25 logger.debug("Cleaning CSS file: %s" % args) | 37 logger.debug("Cleaning CSS file: %s" % args) |
44 return | 56 return |
45 | 57 |
46 self._conf = self.app.config.get('cleancss') or {} | 58 self._conf = self.app.config.get('cleancss') or {} |
47 self._conf.setdefault('bin', 'cleancss') | 59 self._conf.setdefault('bin', 'cleancss') |
48 self._conf.setdefault('options', ['--skip-rebase']) | 60 self._conf.setdefault('options', ['--skip-rebase']) |
61 self._conf.setdefault('out_ext', '.css') | |
62 if len(self._conf['out_ext']) > 0 and self._conf['out_ext'][0] != '.': | |
63 self._conf['out_ext'] = '.' + self._conf['out_ext'] | |
49 if not isinstance(self._conf['options'], list): | 64 if not isinstance(self._conf['options'], list): |
50 raise Exception("The `cleancss/options` configuration setting " | 65 raise Exception("The `cleancss/options` configuration setting " |
51 "must be an array of arguments.") | 66 "must be an array of arguments.") |
52 | 67 |
53 | 68 |