-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwintersmith-handlebars.coffee
97 lines (86 loc) · 3.24 KB
/
wintersmith-handlebars.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
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
92
93
94
95
96
97
Handlebars = require 'handlebars'
path = require 'path'
fs = require 'fs'
layoutPattern = /{{!<\s+([A-Za-z0-9\._\-\/]+)\s*}}/
extend = (obj, mixin) ->
obj[name] = method for name, method of mixin
obj
module.exports = (env, callback) ->
# Default partial directory is `partials`, helper directory to `helpers`
defaults =
partialDir: "partials"
helperDir: "helpers"
# Extend options with ones defined in config.json
options = env.config.handlebars or {}
for key, value of defaults
options[key] ?= defaults[key]
# Support for Handlebars templates
class HandlebarsTemplate extends env.TemplatePlugin
constructor: (@tpl, @raw, @filepath) ->
render: (locals, callback) ->
try
layout = @raw.toString().match(layoutPattern)
if layout and layout.length
@layoutpath =
full: path.join(path.dirname(@filepath.full), layout[1])
relative: layout[1]
HandlebarsTemplate.fromFile @layoutpath, (error, layout, filepath) =>
if error then callback error
else
context = extend locals,
body: @raw.toString()
layout.render context, (error, contents) =>
if error then callback error
else
if contents
@tpl = Handlebars.compile contents.toString()
rendered = @tpl locals
callback null, new Buffer rendered
else
rendered = @tpl locals
callback null, new Buffer rendered
catch error
callback error
HandlebarsTemplate.fromFile = (filepath, callback) ->
fs.readFile filepath.full, (error, contents) ->
if error then callback error
else
try
tpl = Handlebars.compile contents.toString()
callback null, new HandlebarsTemplate tpl, contents, filepath
catch error
callback error
# Support for Handlebars partials
class HandlebarsPartial extends HandlebarsTemplate
HandlebarsPartial.fromFile = (filepath, callback) ->
fs.readFile filepath.full, (error, contents) ->
if error then callback error
else
try
ext = path.extname filepath.relative
basename = path.basename filepath.relative, ext
tpl = Handlebars.compile contents.toString()
Handlebars.registerPartial basename, tpl
callback null, new HandlebarsPartial tpl
catch error
callback error
# Support for Handlebars partials
class HandlebarsHelper extends HandlebarsTemplate
HandlebarsHelper.fromFile = (filepath, callback) ->
try
ext = path.extname filepath.relative
basename = path.basename filepath.relative, ext
fn = env.loadModule filepath.full
if fn
Handlebars.registerHelper basename, fn
callback null, null
else
error = new Error 'Could not load helper function'
callback error
catch error
callback error
# Registering the plugins
env.registerTemplatePlugin '**/*.*(html|hbs)', HandlebarsTemplate
env.registerTemplatePlugin "**/#{options.partialDir}/*.*(html|hbs)", HandlebarsPartial
env.registerTemplatePlugin "**/#{options.helperDir}/*.*(js|coffee)", HandlebarsHelper
callback() # Return callback