-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.coffee
89 lines (72 loc) · 2.9 KB
/
index.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
# expose the render function
{engine} = require('./lib/engine')
{view, getCommonHeaders, getCommonHeadersJs} = require('./lib/view')
exports.engine = engine
exports.view = view
exports.getCommonHeaders = getCommonHeaders
exports.getCommonHeadersJs = getCommonHeadersJs
exports.expressEngine = e = new engine { verbose: false, prettyPrintErrors: true }
exports.render = e.run
cacheless_engine = new engine { verbose: false, prettyPrintErrors: true, cache: false}
# given a template string, returns a function that can be called
# on an object to render it.
# --------------------------------------------
exports.compileStr = (template_str, options) ->
v = new view template_str, options
return (x) -> v.run x
# express 3.x support from a custom engine;
# this function takes a toffee engine
# and returns a function that matches the __express
# standard.
# --------------------------------------------
to_express = exports.toExpress = (eng) ->
return (filename, options, cb) ->
eng.run filename, options, (err, res) ->
if err
if typeof(err) is "string"
err = new Error err
cb err
else
cb null, res
# express 3.x support using the default engine
# --------------------------------------------
__express = exports.__express = to_express e
# consolidate.js support, which doesn't want caching on by default
# --------------------------------------------
exports.__consolidate_engine_render = (filename, options, cb) ->
eng = if options.cache then e else cacheless_engine
eng.run filename, options, (err, res) ->
cb err, res
# consolidate.js wants this, but it might generally be useful
# --------------------------------------------
exports.str_render = exports.strRender = (template_str, options, cb) ->
v = new view template_str, options
[err, res] = v.run options
cb err, res
# express 2.x support
# --------------------------------------------
exports.compile = require('./lib/view').expressCompile
# better support for string compiling
# --------------------------------------------
exports.configurable_compile = (source, opts) ->
opts = opts or {}
opts.headers = if opts.headers? then opts.headers else true
opts.filename = opts.filename or null
opts.to_coffee = opts.to_coffee or false
err = null
# this compiles an individual template that you've read while recursing:
v = new view source, {
filename: opts.filename
bundlePath: opts.filename
browserMode: true
}
if opts.to_coffee
output = v.toCoffee()
else
output = v.toJavaScript()
if v.error then throw v.error.e
if opts.headers
header = getCommonHeadersJs true, true, true
if opts.coffee then output = "`#{header}`\n\n#{output}"
else output = "#{header}\n;\n#{output}"
return output