PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/lib/JSDOC/JsPlate.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 100 lines | 93 code | 4 blank | 3 comment | 1 complexity | c22cdf5c6ca1af460a458fcc7b22c824 MD5 | raw file
  1/**
  2	@constructor
  3*/
  4JSDOC.JsPlate = function(templateFile) {
  5	if (templateFile) this.template = IO.readFile(templateFile);
  6	
  7	this.templateFile = templateFile;
  8	this.code = "";
  9	this.parse();
 10}
 11
 12JSDOC.JsPlate.prototype.parse = function() {
 13	this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
 14	this.code = "var output=``"+this.template;
 15
 16	this.code = this.code.replace(
 17		/<for +each="(.+?)" +in="(.+?)" *>/gi, 
 18		function (match, eachName, inName) {
 19			return "``;\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+=``";
 20		}
 21	);	
 22	this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { output+=``");
 23	this.code = this.code.replace(/<elseif test="(.+?)"\s*\/>/g, "``;}\relse if ($1) { output+=``");
 24	this.code = this.code.replace(/<else\s*\/>/g, "``;}\relse { output+=``");
 25	this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
 26	this.code = this.code.replace(
 27		/\{\+\s*([\s\S]+?)\s*\+\}/gi,
 28		function (match, code) {
 29			code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
 30			code = code.replace(/(\r?\n)/g, " ");
 31			return "``+ ("+code+") +``";
 32		}
 33	);
 34	this.code = this.code.replace(
 35		/\{!\s*([\s\S]+?)\s*!\}/gi,
 36		function (match, code) {
 37			code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
 38			code = code.replace(/(\n)/g, " ");
 39			return "``; "+code+";\routput+=``";
 40		}
 41	);
 42	this.code = this.code+"``;";
 43
 44	this.code = this.code.replace(/(\r?\n)/g, "\\n");
 45	this.code = this.code.replace(/"/g, "\\\"");
 46	this.code = this.code.replace(/``/g, "\"");
 47}
 48
 49JSDOC.JsPlate.prototype.toCode = function() {
 50	return this.code;
 51}
 52
 53JSDOC.JsPlate.keys = function(obj) {
 54	var keys = [];
 55	if (obj.constructor.toString().indexOf("Array") > -1) {
 56		for (var i = 0; i < obj.length; i++) {
 57			keys.push(i);
 58		}
 59	}
 60	else {
 61		for (var i in obj) {
 62			keys.push(i);
 63		}
 64	}
 65	return keys;
 66};
 67
 68JSDOC.JsPlate.values = function(obj) {
 69	var values = [];
 70	if (obj.constructor.toString().indexOf("Array") > -1) {
 71		for (var i = 0; i < obj.length; i++) {
 72			values.push(obj[i]);
 73		}
 74	}
 75	else {
 76		for (var i in obj) {
 77			values.push(obj[i]);
 78		}
 79	}
 80	return values;
 81};
 82
 83JSDOC.JsPlate.prototype.process = function(data) {
 84	var keys = JSDOC.JsPlate.keys;
 85	var values = JSDOC.JsPlate.values;
 86	
 87	try {
 88		eval(this.code);
 89	}
 90	catch (e) {
 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]);
 96		print("");
 97	}
 98	/*debug*///print(this.code);
 99	return output;
100}