aboutsummaryrefslogtreecommitdiffstats
path: root/syte/compress.py
blob: 99fec9305e60b3c30cb6756cbed5253d1352eb60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

import os
import sys
import subprocess
import shlex
import traceback

path_to_here = os.path.abspath(os.path.dirname(__file__))
path_before_site = path_to_here[0:path_to_here.rfind('syte')]
sys.path.append(path_before_site)

#os.environ['DJANGO_SETTINGS_MODULE'] = 'syte.settings'

#from django.conf import settings
import settings

def compress_statics():
    try:
        #This won't work on windows.
        subprocess.check_call(shlex.split('mkdir -p static/css static/js/min'))
    except Exception:
        print 'Make sure to create "syte > static > css" and "syte > static > js > min" before compressing statics.'

    compress_styles()
    compress_js()

def compress_styles():
    less_path = 'static/less/styles.less'
    css_path = 'static/css/'

    try:
        subprocess.check_call(shlex.split('lessc {0} {1}styles.min.css -yui-compress'.format(less_path, css_path)))
        print 'CSS Styles Generated: styles.min.css'
    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        stack_trace = traceback.format_exception(exc_type, exc_value, exc_traceback)
        print stack_trace

def compress_js():
    js_files = [
      'libs/jquery.url.js',
      'libs/require.js',
      'libs/handlebars.js',
      'libs/moment.min.js',
      'libs/bootstrap-modal.js',
      'libs/spin.min.js',
      'libs/prettify.js',

      'components/base.js',
      'components/mobile.js',
      'components/blog-posts.js',
      'components/links.js',
    ]

    if settings.TWITTER_INTEGRATION_ENABLED:
        js_files.append('components/twitter.js')

    if settings.GITHUB_INTEGRATION_ENABLED:
        js_files.append('components/github.js')

    if settings.DRIBBBLE_INTEGRATION_ENABLED:
        js_files.append('components/dribbble.js')

    if settings.INSTAGRAM_INTEGRATION_ENABLED:
        js_files.append('components/instagram.js')

    if settings.DISQUS_INTEGRATION_ENABLED:
        js_files.append('components/disqus.js')

    combined = ''
    for js in js_files:
        f = open('static/js/' + js, 'r')
        combined += f.read()
        f.close()

    f = open('static/js/combined.js', 'w')
    f.write(combined)
    f.close()

    try:
        subprocess.check_call(shlex.split('uglifyjs -o static/js/min/scripts.min.js static/js/combined.js'))
        subprocess.check_call(shlex.split('rm -f static/js/combined.js'))
        print 'JavaScript Combined and Minified: scripts.min.js'
    except Exception:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        stack_trace = traceback.format_exception(exc_type, exc_value, exc_traceback)
        print stack_trace

if __name__ == "__main__":
    compress_statics()
    sys.exit()