-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathhamlc.coffee
118 lines (100 loc) · 3.72 KB
/
hamlc.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
fs = require 'fs'
Compiler = require './haml-coffee'
if process.browser
CoffeeScript = window.CoffeeScript
else
CoffeeScript = require 'coffee-script'
# Express 3 template Cache
__expressCache = {}
# Facade to Haml Coffee for easy template function
# compiling and JST template rendering.
#
module.exports =
# Render the Haml Coffee template into static HTML.
#
# @see {Compiler} for a complete list of the supported
# compiler options.
#
# @param [String] source the Haml Coffee source
# @param [Object] context context for the template
# @param [Object] options the compiler options
# @return [Function] the template
#
render: (source, context = {}, options = {}) ->
# Ensure placement is set to standalone for static rendering.
options.placement = 'standalone'
compiler = new Compiler(options)
compiler.parse source
template = new Function CoffeeScript.compile(compiler.precompile(), bare: true)
template.call context
# Compile the Haml Coffee template into
# a JavaScript function.
#
# @see {Compiler} for a complete list of the supported
# compiler options.
#
# @param [String] source the Haml Coffee source
# @param [Object] options the compiler options
# @return [Function] the template
#
compile: (source, options = {}) ->
compiler = new Compiler(options)
compiler.parse source
template = new Function CoffeeScript.compile(compiler.precompile(), bare: true)
(params) -> template.call params
# Creates the JavaScript Template.
#
# @see {Compiler} for a complete list of the supported
# compiler options.
#
# @param [String] source the Haml Coffee source
# @param [String] name the template name
# @param [String] namespace the template namespace
# @param [Object] options the compiler options
# @return [String] the template source code
#
template: (source, name, namespace, options = {}) ->
# Extend the options with the name and namespace so that the
# compiler has these configuration properties from the beginning
# and that the API for this method can stay the same.
options.namespace = namespace
options.name = name
compiler = new Compiler(options)
compiler.parse source
CoffeeScript.compile compiler.render()
# Express 3 templating interface with template function cache.
# When the template function cache is enabled by setting `cache`
# in the options to true, the compiled JavaScript template function
# is cached, which improves speed a lot, since it it only parses,
# generates and compiles to template once.
#
# @overload __express(filename, callback)
# Compiles and renders a template
# @param [String] filename the template file path
# @param [Function] the callback
#
# @overload __express(filename, options, callback)
# Compiles and renders a template
# @param [String] filename the template file path
# @param [Object] options the compiler options and template locals
# @option options [Boolean] cache whether to cache the template or not
# @param [Function] the callback
#
__express: (filename, options, callback) ->
# Parameter shift
if !!(options and options.constructor and options.call and options.apply)
callback = options
options = {}
try
if options.cache and __expressCache[filename]
callback null, __expressCache[filename](options)
else
options.filename = filename
source = fs.readFileSync(filename, 'utf8')
if options.cache
__expressCache[filename] = module.exports.compile(source, options)
callback null, __expressCache[filename](options)
else
callback null, module.exports.compile(source, options)(options)
catch err
callback err