UNPKG

1.42 kBtext/coffeescriptView Raw
1Node = require('./node')
2
3# Code node that represent lines of CoffeeScript code
4# in the Haml template.
5#
6# @example inline code
7# - for project in projects
8#
9# @example Escaped code assignment
10# = user.get('email')
11#
12# @example Unescaped code assignment
13# != user.get('email')
14#
15module.exports = class Code extends Node
16
17 # Evaluate the Haml inline code
18 #
19 evaluate: ->
20 codeBlock = @expression.match(/(-|!=|\&=|=|~)\s?(.*)?/)
21 identifier = codeBlock[1]
22 code = codeBlock[2]
23
24 # Code block without output
25 if identifier is '-'
26 @opener = @markRunningCode(code)
27
28 # Suppress return value from function with Haml tags
29 @closer = @markRunningCode(" ''") if @children.length isnt 0 and @opener.code.match(/(->|=>)/)
30
31 # Code block that preserves whitespace
32 else if identifier is '~'
33 if @escapeHtml
34 @opener = @markInsertingCode(code, true, false, true)
35 else
36 @opener = @markInsertingCode(code, false, false, true)
37
38 # Code block with output
39 else
40 escape = identifier is '&=' or (identifier is '=' and @escapeHtml)
41
42 if @children.length isnt 0 and code.match(/(->|=>)$/)
43 @opener = @markInsertingCode(code, escape, false, false)
44 @opener.block = 'start'
45
46 @closer = @markRunningCode(" $buffer.join \"\\n\"")
47 @closer.block = 'end'
48
49 else
50 @opener = @markInsertingCode(code, escape)