Skip to content

ksuhaster/jccompressor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.


Optional ``post_compile`` of backend receive compiled css content and may be handled
as your wish.

About

Simple compressor for javascript and css files for django and jinja2 templates for django projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%