Simple compressor for javascript and css files for django and jinja2 templates for django projects.
Python
Pull request Compare This branch is 3 commits ahead, 17 commits behind vitalyvolkov:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/jccompressor
.gitignore
README
setup.py

README

NEW
===

I prefer to store static in ``MEDIA_URL/s/`` for *css* and ``MEDIA_URL/j/`` for *js*. So, added new settings

JC_JS_DIR = 'j'
JC_CSS_DIR = 's'

If you skip this, then all will remain as is: *css* dir for styles, *js* for javascript.

ORIGINAL
========

Simple compressor for javascript and css files. Purpose of this package is to
make compressing with easy ways. It means that you do not need run compressor
in single mode through shell or another application to get new files, but get 
them right after you open webpage.

It includes jinja2 filter to make it easy.

Jinja2 ways:

1. Append to your jinja2 filters.

jccompressor.ext.jinja2.compress.compress_css
jccompressor.ext.jinja2.compress.compress_js

2. We defined some settings for flexible setup of jccompressor.

# Should jccompressor compress scripts.
JC_MAKE_COMPRESS = True
# Should jccompressor compress scripts every time
# when you run application. It is usefull for debug mode.
JC_FORCE_BUILD = True
# Should jccompressor only combine scripts and do not make compiling.
JC_ONLY_COMBINING = True
# Version of built scripts.
JC_SCRIPTS_VERSION = '23'
# Path to built scripts to save.
COMPRESSED_MEDIA_URL = MEDIA_URL + 'build/'

3. Use filters in your jinja2 template.

Suppose you have three css files in your css folder with names 1, 2 and 3.css. 
So that's enough to call this global function and filter.

{% set css = ['1', '2', '3'] %}
{{ css|compress_css }}

That also suitable for JS files in /js/ folder.

{% set js = ['1', '2', '3'] %}
{{ js|compress_js }}

Note, stylesheet files should be placed in `css` folder of your `MEDIA_ROOT`,
and javascript files should be placed in `js` folder of `MEDIA_ROOT` folder.

4. Usage of backends.

I have added support of backends to manipulate content of your files.
Backend should be defined in settings files of django project.

JC_BACKEND="your.own.backend.Klass"

Your backend should define only one method that gets opened file descriptor
with content of script. You always can check what type currently handled
through member variable `filetype`. It should be "css" or "js".

Some example:

import os
import subprocess

from jccompressor.backends.base import BaseJCBackend


class StylusBackend(BaseJCBackend):

    def read(self, stream):
	if self.filetype != 'css':
            return stream.read()
        devnull = open(os.devnull, "w")
        try:
            p = subprocess.Popen(["stylus"], stdin=stream, stdout=devnull)
            return p.communicate()[0]
        except (OSError, subprocess.CalledProcessError):
            pass
        return ''


Another function in your backend is ``pre_open()``. It always applicable for all
of your scripts you apply to filter ``compress_css`` or ``compress_js`` when 
compiler tries to open file before reading content.