forked from jnordberg/wintersmith-less
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin.coffee
56 lines (44 loc) · 1.47 KB
/
plugin.coffee
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
stylus = require 'stylus'
nib = require 'nib'
path = require 'path'
fs = require 'fs'
module.exports = (env, callback) ->
class StylusPlugin extends env.ContentPlugin
constructor: (@_filepath, @_text) ->
getFilename: ->
@_filepath.relative.replace /styl$/, 'css'
getView: ->
return (env, locals, contents, templates, callback) =>
try
options = env.config.stylus or {}
options.filename = @getFilename()
options.paths = [path.dirname(@_filepath.full)]
renderer = stylus(@_text, options)
# allowing to specify dependencies (including nib) via config file
#
# "stylus": {
# "dependencies": [
# "nib",
# "jeet"
# ]
# }
dependencies = options.dependencies
if dependencies?
for dependency in dependencies
currentLib = require(dependency)
renderer.use(currentLib()) if currentLib?
renderer.render (err, css) ->
if err
callback err
else
callback null, new Buffer css
catch error
callback error
StylusPlugin.fromFile = (filepath, callback) ->
fs.readFile filepath.full, (error, buffer) ->
if error
callback error
else
callback null, new StylusPlugin(filepath, buffer.toString())
env.registerContentPlugin 'styles', '**/*.styl', StylusPlugin
do callback