PageRenderTime 33ms CodeModel.GetById 17ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/hyde/ext/plugins/uglify.py

http://github.com/hyde/hyde
Python | 75 lines | 48 code | 11 blank | 16 comment | 3 complexity | d42e2e958344356f73ef3e2741216da4 MD5 | raw file
 1# -*- coding: utf-8 -*-
 2"""
 3Uglify plugin
 4"""
 5
 6from hyde.plugin import CLTransformer
 7from hyde.fs import File
 8
 9class UglifyPlugin(CLTransformer):
10    """
11    The plugin class for Uglify JS
12    """
13
14    def __init__(self, site):
15        super(UglifyPlugin, self).__init__(site)
16
17    @property
18    def executable_name(self):
19        return "uglifyjs"
20
21    @property
22    def plugin_name(self):
23        """
24        The name of the plugin.
25        """
26        return "uglify"
27
28    def text_resource_complete(self, resource, text):
29        """
30        If the site is in development mode, just return.
31        Otherwise, save the file to a temporary place
32        and run the uglify app. Read the generated file
33        and return the text as output.
34        """
35
36        try:
37            mode = self.site.config.mode
38        except AttributeError:
39            mode = "production"
40
41        if not resource.source_file.kind == 'js':
42            return
43
44        if mode.startswith('dev'):
45            self.logger.debug("Skipping uglify in development mode.")
46            return
47
48        supported = [
49            ("beautify", "b"),
50            ("indent", "i"),
51            ("quote-keys", "q"),
52            ("mangle-toplevel", "mt"),
53            ("no-mangle", "nm"),
54            ("no-squeeze", "ns"),
55            "no-seqs",
56            "no-dead-code",
57            ("no-copyright", "nc"),
58            "overwrite",
59            "verbose",
60            "unsafe",
61            "max-line-len",
62            "reserved-names",
63            "ascii"
64        ]
65
66        uglify = self.app
67        source = File.make_temp(text)
68        target = File.make_temp('')
69        args = [unicode(uglify)]
70        args.extend(self.process_args(supported))
71        args.extend(["-o", unicode(target), unicode(source)])
72
73        self.call_app(args)
74        out = target.read_all()
75        return out