-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
32 lines (22 loc) · 1.12 KB
/
render.js
File metadata and controls
32 lines (22 loc) · 1.12 KB
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
// var Scope = require('./scope');
function _renderTokens (con_Text, tokens_list, scope, statements) {
return tokens_list.reduce(function (result, token) {
if( typeof token === 'string' ) return result + token
if( !token.$$ ) throw new Error('Unrecognized token type')
if( !token.$ ) { // ${ expression } special case
return result + con_Text.eval(token.$$)(scope)
}
if( !statements[token.$] ) throw new Error('Unrecognized command')
return result + statements[token.$].call(con_Text, token.$$, scope, function renderContent (_scope) {
if( typeof token._ === 'string' ) return token._
return _renderTokens(con_Text, token._, _scope, statements)
}, function renderOtherwise (_scope) {
if( typeof token.__ === 'string' ) return token.__
return _renderTokens(con_Text, token.__ || [], _scope, statements)
})
}, '')
}
module.exports = function renderTokens (con_Text, tokens_list, data, statements) {
if( !(tokens_list instanceof Array) ) throw new TypeError('tokens list should be an Array')
return _renderTokens( con_Text, tokens_list, data || {}, statements || {} )
}