Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80713 views
1
import Exception from "../exception";
2
3
export function SourceLocation(source, locInfo) {
4
this.source = source;
5
this.start = {
6
line: locInfo.first_line,
7
column: locInfo.first_column
8
};
9
this.end = {
10
line: locInfo.last_line,
11
column: locInfo.last_column
12
};
13
}
14
15
export function stripFlags(open, close) {
16
return {
17
open: open.charAt(2) === '~',
18
close: close.charAt(close.length-3) === '~'
19
};
20
}
21
22
export function stripComment(comment) {
23
return comment.replace(/^\{\{~?\!-?-?/, '')
24
.replace(/-?-?~?\}\}$/, '');
25
}
26
27
export function preparePath(data, parts, locInfo) {
28
/*jshint -W040 */
29
locInfo = this.locInfo(locInfo);
30
31
var original = data ? '@' : '',
32
dig = [],
33
depth = 0,
34
depthString = '';
35
36
for(var i=0,l=parts.length; i<l; i++) {
37
var part = parts[i].part;
38
original += (parts[i].separator || '') + part;
39
40
if (part === '..' || part === '.' || part === 'this') {
41
if (dig.length > 0) {
42
throw new Exception('Invalid path: ' + original, {loc: locInfo});
43
} else if (part === '..') {
44
depth++;
45
depthString += '../';
46
}
47
} else {
48
dig.push(part);
49
}
50
}
51
52
return new this.PathExpression(data, depth, dig, original, locInfo);
53
}
54
55
export function prepareMustache(path, params, hash, open, strip, locInfo) {
56
/*jshint -W040 */
57
// Must use charAt to support IE pre-10
58
var escapeFlag = open.charAt(3) || open.charAt(2),
59
escaped = escapeFlag !== '{' && escapeFlag !== '&';
60
61
return new this.MustacheStatement(path, params, hash, escaped, strip, this.locInfo(locInfo));
62
}
63
64
export function prepareRawBlock(openRawBlock, content, close, locInfo) {
65
/*jshint -W040 */
66
if (openRawBlock.path.original !== close) {
67
var errorNode = {loc: openRawBlock.path.loc};
68
69
throw new Exception(openRawBlock.path.original + " doesn't match " + close, errorNode);
70
}
71
72
locInfo = this.locInfo(locInfo);
73
var program = new this.Program([content], null, {}, locInfo);
74
75
return new this.BlockStatement(
76
openRawBlock.path, openRawBlock.params, openRawBlock.hash,
77
program, undefined,
78
{}, {}, {},
79
locInfo);
80
}
81
82
export function prepareBlock(openBlock, program, inverseAndProgram, close, inverted, locInfo) {
83
/*jshint -W040 */
84
// When we are chaining inverse calls, we will not have a close path
85
if (close && close.path && openBlock.path.original !== close.path.original) {
86
var errorNode = {loc: openBlock.path.loc};
87
88
throw new Exception(openBlock.path.original + ' doesn\'t match ' + close.path.original, errorNode);
89
}
90
91
program.blockParams = openBlock.blockParams;
92
93
var inverse,
94
inverseStrip;
95
96
if (inverseAndProgram) {
97
if (inverseAndProgram.chain) {
98
inverseAndProgram.program.body[0].closeStrip = close.strip;
99
}
100
101
inverseStrip = inverseAndProgram.strip;
102
inverse = inverseAndProgram.program;
103
}
104
105
if (inverted) {
106
inverted = inverse;
107
inverse = program;
108
program = inverted;
109
}
110
111
return new this.BlockStatement(
112
openBlock.path, openBlock.params, openBlock.hash,
113
program, inverse,
114
openBlock.strip, inverseStrip, close && close.strip,
115
this.locInfo(locInfo));
116
}
117
118