| author | hamidouk |
| Fri, 23 Dec 2011 11:38:47 +0100 | |
| branch | jsdoc |
| changeset 520 | fe008e95a716 |
| permissions | -rw-r--r-- |
|
520
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
1 |
/** |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
2 |
@constructor |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
3 |
*/ |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
4 |
JSDOC.JsPlate = function(templateFile) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
5 |
if (templateFile) this.template = IO.readFile(templateFile); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
6 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
7 |
this.templateFile = templateFile; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
8 |
this.code = ""; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
9 |
this.parse(); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
10 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
11 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
12 |
JSDOC.JsPlate.prototype.parse = function() { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
13 |
this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, ""); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
14 |
this.code = "var output=\u001e"+this.template; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
15 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
16 |
this.code = this.code.replace( |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
17 |
/<for +each="(.+?)" +in="(.+?)" *>/gi, |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
18 |
function (match, eachName, inName) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
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"; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
20 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
21 |
); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
22 |
this.code = this.code.replace(/<if test="(.+?)">/g, "\u001e;\rif ($1) { output+=\u001e"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
23 |
this.code = this.code.replace(/<elseif test="(.+?)"\s*\/>/g, "\u001e;}\relse if ($1) { output+=\u001e"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
24 |
this.code = this.code.replace(/<else\s*\/>/g, "\u001e;}\relse { output+=\u001e"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
25 |
this.code = this.code.replace(/<\/(if|for)>/g, "\u001e;\r};\routput+=\u001e"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
26 |
this.code = this.code.replace( |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
27 |
/\{\+\s*([\s\S]+?)\s*\+\}/gi, |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
28 |
function (match, code) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
29 |
code = code.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
30 |
code = code.replace(/(\r?\n)/g, " "); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
31 |
return "\u001e+ ("+code+") +\u001e"; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
32 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
33 |
); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
34 |
this.code = this.code.replace( |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
35 |
/\{!\s*([\s\S]+?)\s*!\}/gi, |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
36 |
function (match, code) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
37 |
code = code.replace(/"/g, "\u001e"); // prevent qoute-escaping of inline code |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
38 |
code = code.replace(/(\n)/g, " "); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
39 |
return "\u001e; "+code+";\routput+=\u001e"; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
40 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
41 |
); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
42 |
this.code = this.code+"\u001e;"; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
43 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
44 |
this.code = this.code.replace(/(\r?\n)/g, "\\n"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
45 |
this.code = this.code.replace(/"/g, "\\\""); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
46 |
this.code = this.code.replace(/\u001e/g, "\""); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
47 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
48 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
49 |
JSDOC.JsPlate.prototype.toCode = function() { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
50 |
return this.code; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
51 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
52 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
53 |
JSDOC.JsPlate.keys = function(obj) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
54 |
var keys = []; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
55 |
if (obj.constructor.toString().indexOf("Array") > -1) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
56 |
for (var i = 0; i < obj.length; i++) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
57 |
keys.push(i); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
58 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
59 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
60 |
else { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
61 |
for (var i in obj) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
62 |
keys.push(i); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
63 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
64 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
65 |
return keys; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
66 |
}; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
67 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
68 |
JSDOC.JsPlate.values = function(obj) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
69 |
var values = []; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
70 |
if (obj.constructor.toString().indexOf("Array") > -1) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
71 |
for (var i = 0; i < obj.length; i++) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
72 |
values.push(obj[i]); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
73 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
74 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
75 |
else { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
76 |
for (var i in obj) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
77 |
values.push(obj[i]); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
78 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
79 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
80 |
return values; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
81 |
}; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
82 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
83 |
JSDOC.JsPlate.prototype.process = function(data, compact) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
84 |
var keys = JSDOC.JsPlate.keys; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
85 |
var values = JSDOC.JsPlate.values; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
86 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
87 |
try { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
88 |
eval(this.code); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
89 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
90 |
catch (e) { |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
91 |
print(">> There was an error evaluating the compiled code from template: "+this.templateFile); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
92 |
print(" The error was on line "+e.lineNumber+" "+e.name+": "+e.message); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
93 |
var lines = this.code.split("\r"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
94 |
if (e.lineNumber-2 >= 0) print("line "+(e.lineNumber-1)+": "+lines[e.lineNumber-2]); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
95 |
print("line "+e.lineNumber+": "+lines[e.lineNumber-1]); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
96 |
print(""); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
97 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
98 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
99 |
if (compact) { // patch by mcbain.asm |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
100 |
// Remove lines that contain only space-characters, usually left by lines in the template |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
101 |
// which originally only contained JSPlate tags or code. This makes it easier to write |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
102 |
// non-tricky templates which still put out nice code (not bloated with extra lines). |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
103 |
// Lines purposely left blank (just a line ending) are left alone. |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
104 |
output = output.replace(/\s+?(\r?)\n/g, "$1\n"); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
105 |
} |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
106 |
|
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
107 |
/*debug*///print(this.code); |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
108 |
return output; |
|
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff
changeset
|
109 |
} |