Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Merge pull request #147 from dskecse/replace-sass-with-libsass
Browse files Browse the repository at this point in the history
Replace SASS with libsass
  • Loading branch information
Mike Cooper committed Dec 2, 2015
2 parents cd1e58f + 5ac46d2 commit d233a1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion test_site/hooks/__hooks__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from wok.contrib.hooks import compile_sass

hook_count = 0
def make_hook(name):
Expand All @@ -11,7 +12,7 @@ def logging_hook(*args):
hooks = {
'site.start': make_hook('site.start'),
'site.output.pre': make_hook('site.output.pre'),
'site.output.post': make_hook('site.output.post'),
'site.output.post': [compile_sass],
'site.content.gather.pre': make_hook('site.content.gather.pre'),
'site.content.gather.post': make_hook('site.content.gather.post'),
'page.meta.pre': make_hook('page.template.pre'),
Expand Down
File renamed without changes.
41 changes: 27 additions & 14 deletions wok/contrib/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Some hooks that might be useful."""

import os
import glob
import subprocess
from StringIO import StringIO
import logging
Expand All @@ -15,6 +16,10 @@
except ImportError:
etree = None

try:
import sass
except ImportError:
sass = None

class HeadingAnchors(object):
"""
Expand Down Expand Up @@ -59,7 +64,7 @@ def __call__(self, config, page):

sio_destination = StringIO()

# Use the extension of the template to determine the type of document
# Use the extension of the template to determine the type of document
if page.template.filename.endswith(".html") or page.filename.endswith(".htm"):
logging.debug('[HeadingAnchors] outputting {0} as HTML'.format(page))
tree.write(sio_destination, method='html')
Expand All @@ -84,26 +89,34 @@ def compile_sass(config, output_dir):
from wok.contrib.hooks import compile_sass
hooks = {
'site.output.post':[compile_sass]
'site.output.post': [compile_sass]
}
Dependencies:
- Ruby
- Sass (http://sass-lang.com)
- libsass
'''
logging.info('Running hook compile_sass on {0}.'.format(output_dir))
for root, dirs, files in os.walk(output_dir):
for f in files:
fname, fext = os.path.splitext(f)
if fext == ".scss" or fext == ".sass":
# Sass partials should not be compiled
if not fname.startswith('_') and fext == '.scss' or fext == '.sass':
abspath = os.path.abspath(root)
sass_src = "%s/%s"%(abspath, f)
sass_dest = "%s/%s.css"%(abspath, fname)
sass_arg = "%s:%s"%(sass_src, sass_dest)
logging.debug('[hook/sass] sass {0}'.format(sass_arg))
try:
subprocess.call(['sass', sass_arg])
except OSError:
logging.warning('[hook/compile_sass] Could not run SASS ' +
'hook. (Is SASS installed?)')
sass_src = '{0}/{1}'.format(abspath, f)
sass_dest = '{0}/{1}.css'.format(abspath, fname)

if sass is None:
logging.warning('To use compile_sass hook, you must install '
'libsass-python package.')
return

compiled_str = sass.compile(filename=sass_src, output_style='compressed')
with open(sass_dest, 'w') as f:
f.write(compiled_str)

# TODO: Get rid of extra housekeeping by compiling Sass files in
# "site.output.pre" hook
abspath = os.path.abspath(output_dir)
for f in glob.glob(os.path.join(abspath, '**', '*.s[a,c]ss')):
os.remove(f)

0 comments on commit d233a1c

Please sign in to comment.