2
2
"""Some hooks that might be useful."""
3
3
4
4
import os
5
+ import glob
5
6
import subprocess
6
7
from StringIO import StringIO
7
8
import logging
15
16
except ImportError :
16
17
etree = None
17
18
19
+ try :
20
+ import sass
21
+ except ImportError :
22
+ sass = None
18
23
19
24
class HeadingAnchors (object ):
20
25
"""
@@ -59,7 +64,7 @@ def __call__(self, config, page):
59
64
60
65
sio_destination = StringIO ()
61
66
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
63
68
if page .template .filename .endswith (".html" ) or page .filename .endswith (".htm" ):
64
69
logging .debug ('[HeadingAnchors] outputting {0} as HTML' .format (page ))
65
70
tree .write (sio_destination , method = 'html' )
@@ -84,26 +89,34 @@ def compile_sass(config, output_dir):
84
89
from wok.contrib.hooks import compile_sass
85
90
86
91
hooks = {
87
- 'site.output.post':[compile_sass]
92
+ 'site.output.post': [compile_sass]
88
93
}
89
94
90
95
Dependencies:
91
96
92
- - Ruby
93
- - Sass (http://sass-lang.com)
97
+ - libsass
94
98
'''
95
99
logging .info ('Running hook compile_sass on {0}.' .format (output_dir ))
96
100
for root , dirs , files in os .walk (output_dir ):
97
101
for f in files :
98
102
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' :
100
105
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