Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
raliste committed Jul 25, 2011
0 parents commit 6c75ce6
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.pyc
build
dist
*.egg-info
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2010, Mozilla Foundation
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of jingo-minify nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include LICENSE
include django_minify/bin/yuicompressor-2.4.4.jar
Empty file added django_minify/__init__.py
Empty file.
Binary file added django_minify/bin/yuicompressor-2.4.4.jar
Binary file not shown.
Empty file.
Empty file.
63 changes: 63 additions & 0 deletions django_minify/management/commands/compress_assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from optparse import make_option
import os
import time
import hashlib
import base64
import json
from subprocess import call, PIPE

from django.conf import settings
from django.core.management.base import BaseCommand

path = lambda *a: os.path.join(settings.STATIC_ROOT, *a)

class Command(BaseCommand):
help = ("Compresses css and js assets defined in settings.MINIFY_BUNDLES")

def generate_static_name(self, name, ftype, base=None, shorthash=None):
sha = hashlib.sha1(open(path('%s-min.%s' % (name, ftype))).read()).digest()
shorthash = base64.urlsafe_b64encode(sha[0:8]).rstrip('=')
name, ext = os.path.splitext('%s.%s' % (name, ftype))

return name + '.' + shorthash + ext

def handle(self, **options):
jar_path = (os.path.dirname(__file__), '..', '..', 'bin',
'yuicompressor-2.4.4.jar')
path_to_jar = os.path.realpath(os.path.join(*jar_path))

v = ''
if 'verbosity' in options and options['verbosity'] == '2':
v = '-v'

names = {}

for ftype, bundle in settings.MINIFY_BUNDLES.iteritems():
names[ftype] = {}

for name, files in bundle.iteritems():
files_all = []
for fn in files:
files_all.append(fn)

concatted_file = path('%s-all.%s' % (name, ftype,))
compressed_file = path('%s-min.%s' % (name, ftype,))
real_files = [path(f.lstrip('/')) for f in files_all]

# Concats the files.
call("cat %s > %s" % (' '.join(real_files), concatted_file),
shell=True)

# Compresses the concatenation.
call("%s -jar %s %s %s -o %s" % (settings.JAVA_BIN,
path_to_jar, v, concatted_file, compressed_file),
shell=True, stdout=PIPE)

names[ftype][name] = self.generate_static_name(name, ftype)

staticname = path(names[ftype][name])
if not os.path.exists(staticname):
os.symlink(compressed_file, staticname)

json_enc = json.JSONEncoder(indent=4, sort_keys=True)
open(os.path.join(settings.ROOT_PATH, 'names.json'), 'w').write(json_enc.encode(names))
Empty file.
29 changes: 29 additions & 0 deletions django_minify/templatetags/assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import time

from django import template
from django.conf import settings

register = template.Library()

def _build_html(items, wrapping):
if settings.DEBUG:
wrapping = wrapping.replace('%s', '%s?t=' + str(time.time()))

return '\n'.join((wrapping % (settings.STATIC_URL + item)) for item in items)

@register.simple_tag
def css(bundle):
if settings.DEBUG:
items = settings.MINIFY_BUNDLES['css'][bundle]
else:
items = (settings.STATIC_NAMES['css'][bundle],)
return _build_html(items, '<link rel="stylesheet" type="text/css" href="%s" />')

@register.simple_tag
def js(bundle):
if settings.DEBUG:
items = settings.MINIFY_BUNDLES['js'][bundle]
else:
items = (settings.STATIC_NAMES['js'][bundle],)
return _build_html(items, '<script type="text/javascript" src="%s"></script>')

26 changes: 26 additions & 0 deletions django_minify/templatetags/static.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django import template
from django.conf import settings

register = template.Library()

print 'dsadsa'

def _build_html(items, wrapping):
return '\n'.join((wrapping % (settings.STATIC_URL + item)) for item in items)

@register.simple_tag
def css(bundle):
if settings.DEBUG:
items = settings.MINIFY_BUNDLES['css'][bundle]
else:
items = (settings.STATIC_NAMES['css'][bundle])
return _build_html(items, '<link rel="stylesheet" type="text/css" href="%s" />')

@register.simple_tag
def js(bundle):
if settings.DEBUG:
items = settings.MINIFY_BUNDLES['js'][bundle]
else:
items = (settings.STATIC_NAMES['js'][bundle])
return _build_html(items, '<script type="text/javascript" src="%s"></script>')

24 changes: 24 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from setuptools import setup, find_packages

setup(
name='django_minify',
version='0.1',
description=('A Django app that will contact and minify JS and CSS.' +
'Based on Jingo Minify by James Socol.'),
author='Rodrigo Aliste P.',
author_email='[email protected]',
url='http://github.com/raliste/django-minify',
license='BSD',
packages=find_packages(),
include_package_data=True,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
'Intented Audience :: Developers',
'Framework :: Django',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
]
)

0 comments on commit 6c75ce6

Please sign in to comment.