-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpl.js
executable file
·168 lines (147 loc) · 6.25 KB
/
tmpl.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* RequireJS tmpl plugin 1.0.0
* Original creator: Adam Alexander (https://github.com/adamalex)
* Available via the MIT or new BSD license.
* see: https://github.com/kevinparkerson/requirejs-tmpl for details
*
*
*
* tmpl is a modified CoffeeScript plugin:
*
* @license cs 0.3.1 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/require-cs for details
*/
/* Yes, deliciously evil. */
/*jslint evil: true, strict: false, plusplus: false, regexp: false */
/*global require: false, XMLHttpRequest: false, ActiveXObject: false,
define: false, process: false, window: false */
define(['handlebars'], function (Handlebars) {
//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild)
var fs, getXhr,
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
fetchText = function (url, callback) {
throw new Error('Environment unsupported.');
},
buildMap = [];
if (typeof window !== "undefined" && window.navigator && window.document) {
// Browser action
getXhr = function () {
//Would love to dump the ActiveX crap in here. Need IE 6 to die first.
var xhr, i, progId;
if (typeof XMLHttpRequest !== "undefined") {
return new XMLHttpRequest();
} else {
for (i = 0; i < 3; i++) {
progId = progIds[i];
try {
xhr = new ActiveXObject(progId);
} catch (e) {}
if (xhr) {
progIds = [progId]; // so faster next time
break;
}
}
}
if (!xhr) {
throw new Error("getXhr(): XMLHttpRequest not available");
}
return xhr;
};
fetchText = function (url, callback) {
var xhr = getXhr();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (evt) {
//Do not explicitly handle errors, those should be
//visible via console output in the browser.
if (xhr.readyState === 4) {
callback(xhr.responseText);
}
};
xhr.send(null);
};
} else if (typeof process !== "undefined" &&
process.versions &&
!!process.versions.node) {
//Using special require.nodeRequire, something added by r.js.
fs = require.nodeRequire('fs');
fetchText = function (path, callback) {
callback(fs.readFileSync(path, 'utf8'));
};
} else if (typeof Packages !== 'undefined') {
//Why Java, why is this so awkward?
fetchText = function (path, callback) {
var encoding = "utf-8",
file = new java.io.File(path),
lineSeparator = java.lang.System.getProperty("line.separator"),
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
stringBuffer, line,
content = '';
try {
stringBuffer = new java.lang.StringBuffer();
line = input.readLine();
// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
// http://www.unicode.org/faq/utf_bom.html
// Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
if (line && line.length() && line.charAt(0) === 0xfeff) {
// Eat the BOM, since we've already found the encoding on this file,
// and we plan to concatenating this buffer with others; the BOM should
// only appear at the top of a file.
line = line.substring(1);
}
stringBuffer.append(line);
while ((line = input.readLine()) !== null) {
stringBuffer.append(lineSeparator);
stringBuffer.append(line);
}
//Make sure we return a JavaScript string and not a Java string.
content = String(stringBuffer.toString()); //String
} finally {
input.close();
}
callback(content);
};
}
//>>excludeEnd('excludeAfterBuild')
return {
//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild)
write: function (pluginName, name, write) {
if (name in buildMap) {
var text = buildMap[name];
write.asModule(pluginName + "!" + name, text);
}
},
//>>excludeEnd('excludeAfterBuild')
load: function (name, parentRequire, load, config) {
//>>excludeStart('excludeAfterBuild', pragmas.excludeAfterBuild)
var path = parentRequire.toUrl(name);
fetchText(path, function (text) {
//Do Handlebars transform.
text = Handlebars.precompile(text);
text = "define(['handlebars'], function (Handlebars) { return Handlebars.template(" + text + "); });";
//Hold on to the transformed text if a build.
if (config.isBuild) {
buildMap[name] = text;
}
/*@cc_on
/*@if (@_jscript)
//IE with conditional comments on cannot handle the sourceURL trick, so skip it if enabled.
@else @*/
if(!config.isBuild){
text += "\r\n//@ sourceURL=" + path;
}
/*@end
@*/
load.fromText(name, text);
//Give result to load. Need to wait until the module
//is fully parse, which will happen after this
//execution.
parentRequire([name], function (value) {
load(value);
});
});
//>>excludeEnd('excludeAfterBuild')
}
};
});