Skip to content

Commit 8924172

Browse files
committed
Merge branch 'master' of github.com:mythmon/wok
2 parents 7cf34e2 + d233a1c commit 8924172

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
'pytest==2.5.2',
3535
],
3636
packages=['wok'],
37+
package_data={'wok':['contrib/*']},
3738
scripts=['scripts/wok'],
3839
)

test_site/hooks/__hooks__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from wok.contrib.hooks import compile_sass
23

34
hook_count = 0
45
def make_hook(name):
@@ -11,7 +12,7 @@ def logging_hook(*args):
1112
hooks = {
1213
'site.start': make_hook('site.start'),
1314
'site.output.pre': make_hook('site.output.pre'),
14-
'site.output.post': make_hook('site.output.post'),
15+
'site.output.post': [compile_sass],
1516
'site.content.gather.pre': make_hook('site.content.gather.pre'),
1617
'site.content.gather.post': make_hook('site.content.gather.post'),
1718
'page.meta.pre': make_hook('page.template.pre'),
File renamed without changes.

wok/contrib/hooks.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Some hooks that might be useful."""
33

44
import os
5+
import glob
56
import subprocess
67
from StringIO import StringIO
78
import logging
@@ -15,6 +16,10 @@
1516
except ImportError:
1617
etree = None
1718

19+
try:
20+
import sass
21+
except ImportError:
22+
sass = None
1823

1924
class HeadingAnchors(object):
2025
"""
@@ -59,7 +64,7 @@ def __call__(self, config, page):
5964

6065
sio_destination = StringIO()
6166

62-
# Use the extension of the template to determine the type of document
67+
# Use the extension of the template to determine the type of document
6368
if page.template.filename.endswith(".html") or page.filename.endswith(".htm"):
6469
logging.debug('[HeadingAnchors] outputting {0} as HTML'.format(page))
6570
tree.write(sio_destination, method='html')
@@ -84,26 +89,34 @@ def compile_sass(config, output_dir):
8489
from wok.contrib.hooks import compile_sass
8590
8691
hooks = {
87-
'site.output.post':[compile_sass]
92+
'site.output.post': [compile_sass]
8893
}
8994
9095
Dependencies:
9196
92-
- Ruby
93-
- Sass (http://sass-lang.com)
97+
- libsass
9498
'''
9599
logging.info('Running hook compile_sass on {0}.'.format(output_dir))
96100
for root, dirs, files in os.walk(output_dir):
97101
for f in files:
98102
fname, fext = os.path.splitext(f)
99-
if fext == ".scss" or fext == ".sass":
103+
# Sass partials should not be compiled
104+
if not fname.startswith('_') and fext == '.scss' or fext == '.sass':
100105
abspath = os.path.abspath(root)
101-
sass_src = "%s/%s"%(abspath, f)
102-
sass_dest = "%s/%s.css"%(abspath, fname)
103-
sass_arg = "%s:%s"%(sass_src, sass_dest)
104-
logging.debug('[hook/sass] sass {0}'.format(sass_arg))
105-
try:
106-
subprocess.call(['sass', sass_arg])
107-
except OSError:
108-
logging.warning('[hook/compile_sass] Could not run SASS ' +
109-
'hook. (Is SASS installed?)')
106+
sass_src = '{0}/{1}'.format(abspath, f)
107+
sass_dest = '{0}/{1}.css'.format(abspath, fname)
108+
109+
if sass is None:
110+
logging.warning('To use compile_sass hook, you must install '
111+
'libsass-python package.')
112+
return
113+
114+
compiled_str = sass.compile(filename=sass_src, output_style='compressed')
115+
with open(sass_dest, 'w') as f:
116+
f.write(compiled_str)
117+
118+
# TODO: Get rid of extra housekeeping by compiling Sass files in
119+
# "site.output.pre" hook
120+
abspath = os.path.abspath(output_dir)
121+
for f in glob.glob(os.path.join(abspath, '**', '*.s[a,c]ss')):
122+
os.remove(f)

0 commit comments

Comments
 (0)