-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6c75ce6
Showing
12 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.pyc | ||
build | ||
dist | ||
*.egg-info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Binary file not shown.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
) |