4 JSDOC
.JsPlate
= function(templateFile
) {
5 if (templateFile
) this.template
= IO
.readFile(templateFile
);
7 this.templateFile
= templateFile
;
12 JSDOC
.JsPlate
.prototype.parse
= function() {
13 this.template
= this.template
.replace(/\{#[\s\S]+?#\}/gi, "");
14 this.code
= "var output=\u001e"+this.template
;
16 this.code
= this.code
.replace(
17 /<for +each="(.+?)" +in="(.+?)" *>/gi,
18 function (match
, eachName
, inName
) {
19 return "\u001e;\rvar $"+eachName
+"_keys = keys("+inName
+");\rfor(var $"+eachName
+"_i = 0; $"+eachName
+"_i < $"+eachName
+"_keys.length; $"+eachName
+"_i++) {\rvar $"+eachName
+"_last = ($"+eachName
+"_i == $"+eachName
+"_keys.length-1);\rvar $"+eachName
+"_key = $"+eachName
+"_keys[$"+eachName
+"_i];\rvar "+eachName
+" = "+inName
+"[$"+eachName
+"_key];\routput+=\u001e";
22 this.code
= this.code
.replace(/<if test="(.+?)">/g, "\u001e;\rif ($1) { output+=\u001e");
23 this.code
= this.code
.replace(/<elseif test="(.+?)"\s*\/>/g
, "\u001e;}\relse if ($1) { output+=\u001e");
24 this.code
= this.code
.replace(/<else\s*\/>/g
, "\u001e;}\relse { output+=\u001e");
25 this.code
= this.code
.replace(/<\/(if|for)>/g
, "\u001e;\r};\routput+=\u001e");
26 this.code
= this.code
.replace(
27 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
28 function (match
, code
) {
29 code
= code
.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code
30 code
= code
.replace(/(\r?\n)/g, " ");
31 return "\u001e+ ("+code
+") +\u001e";
34 this.code
= this.code
.replace(
35 /\{!\s*([\s\S]+?)\s*!\}/gi,
36 function (match
, code
) {
37 code
= code
.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code
38 code
= code
.replace(/(\n)/g, " ");
39 return "\u001e; "+code
+";\routput+=\u001e";
42 this.code
= this.code
+"\u001e;";
44 this.code
= this.code
.replace(/(\r?\n)/g, "\\n");
45 this.code
= this.code
.replace(/"/g, "\\\"");
46 this.code
= this.code
.replace(/\u001e/g, "\"");
49 JSDOC
.JsPlate
.prototype.toCode
= function() {
53 JSDOC
.JsPlate
.keys
= function(obj
) {
55 if (obj
.constructor
.toString().indexOf("Array") > -1) {
56 for (var i
= 0; i
< obj
.length
; i
++) {
68 JSDOC
.JsPlate
.values
= function(obj
) {
70 if (obj
.constructor
.toString().indexOf("Array") > -1) {
71 for (var i
= 0; i
< obj
.length
; i
++) {
83 JSDOC
.JsPlate
.prototype.process
= function(data
, compact
) {
84 var keys
= JSDOC
.JsPlate
.keys
;
85 var values
= JSDOC
.JsPlate
.values
;
91 print(">> There was an error evaluating the compiled code from template: "+this.templateFile
);
92 print(" The error was on line "+e
.lineNumber
+" "+e
.name
+": "+e
.message
);
93 var lines
= this.code
.split("\r");
94 if (e
.lineNumber
-2 >= 0) print("line "+(e
.lineNumber
-1)+": "+lines
[e
.lineNumber
-2]);
95 print("line "+e
.lineNumber
+": "+lines
[e
.lineNumber
-1]);
99 if (compact
) { // patch by mcbain.asm
100 // Remove lines that contain only space-characters, usually left by lines in the template
101 // which originally only contained JSPlate tags or code. This makes it easier to write
102 // non-tricky templates which still put out nice code (not bloated with extra lines).
103 // Lines purposely left blank (just a line ending) are left alone.
104 output
= output
.replace(/\s+?(\r?)\n/g, "$1\n");
107 /*debug*///print(this.code
);