Ejemplo n.º 1
0
    def render(self, context):
        root = utils.get_root(settings)
        input_files, temp_files = utils.resolve_patterns(settings.JSMIN_INPUT, root)
        
        def urlize(f):
            path = os.path.relpath(f, settings.MEDIA_ROOT)
            try:
                path += "?_=%d" % os.stat(f).st_mtime
            except OSError:
                pass
            
            return path
        
        input_files = map(urlize, input_files)
        temp_files = map(urlize, temp_files)

        html = ""
        for f in input_files + temp_files:
            html += """<script src="%s%s" type="text/javascript" charset="utf-8"></script>\n""" % (settings.MEDIA_URL, f)
        
        return html
Ejemplo n.º 2
0
def jsmin(args):
    """Minify the configured JavaScript libraries."""
    
    if not hasattr(args.settings, 'JSMIN_INPUT'):
        raise ImproperlyConfigured("Must provide a JSMIN_INPUT setting")
    elif not hasattr(args.settings, 'JSMIN_OUTPUT'):
        raise ImproperlyConfigured("Must provide a JSMIN_OUTPUT setting")
    
    root = utils.get_root(args.settings)
    
    # Set up development mode. If nothing is specified, this will default to the
    # value of `settings.DEBUG`. The `-d` and `-p` options override this value.
    if args.development_mode is None:
        development_mode = args.settings.DEBUG
    else:
        development_mode = args.development_mode
    
    # `temp_files` have to be deleted after processing, whether minification was
    # successful or not.
    input_files, temp_files = utils.resolve_patterns(args.settings.JSMIN_INPUT, root)
    
    try:
        # Get an absolute output filename.
        output_file = utils.make_abs(args.settings.JSMIN_OUTPUT, root)
        
        if output_file in input_files:
            # This can happen if you output a '.js' file to the same directory
            # you're reading from. Remove it from the input files.
            input_files.remove(output_file)
        
        input_io = StringIO()
        try:
            # Populate the input StringIO.
            for filename in input_files:
                if filename in temp_files:
                    LOG.info("Reading %s" % p.basename(filename))
                else:
                    LOG.info("Reading %s" % p.relpath(filename))
                
                # The additional whitespace/comments will be filtered out by the
                # minifier later on, unless we are in development mode, in which
                # case we want the whitespace and comments.
                input_io.write("/* FILE: %s */" % filename + os.linesep)
                input_io.write(utils.read_from(filename))
                input_io.write(os.linesep * 2)
            input_io.seek(0)
            
            output_io = open(output_file, 'w')
            try:
                output_io.write(utils.get_prolog(args.settings, root))
                
                if development_mode:
                    LOG.info("Writing to %s" % p.relpath(output_file))
                    output_io.write(input_io.getvalue())
                else:
                    # Minify and write the output.
                    LOG.info("Minifying and writing to %s" % p.relpath(output_file))
                    libjsmin.JavascriptMinify(input_io, output_io).minify()
            finally:
                output_io.close() # Clean up.
        finally:
            input_io.close() # Clean up.
    
    finally:
        # Clean up.
        for temp_filename in temp_files:
            LOG.info("Cleaning temporary file %s" % p.basename(temp_filename))
            os.remove(temp_filename)