thirdparty/jsdoctoolkit/app/lib/JSDOC/JsPlate.js
changeset 3041 c8f47f0b6697
equal deleted inserted replaced
3040:8f9580309846 3041:c8f47f0b6697
       
     1 /**
       
     2 	@constructor
       
     3 */
       
     4 JSDOC.JsPlate = function(templateFile) {
       
     5 	if (templateFile) this.template = IO.readFile(templateFile);
       
     6 	
       
     7 	this.templateFile = templateFile;
       
     8 	this.code = "";
       
     9 	this.parse();
       
    10 }
       
    11 
       
    12 JSDOC.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 
       
    49 JSDOC.JsPlate.prototype.toCode = function() {
       
    50 	return this.code;
       
    51 }
       
    52 
       
    53 JSDOC.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 
       
    68 JSDOC.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 
       
    83 JSDOC.JsPlate.prototype.process = function(data, compact) {
       
    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 	
       
    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");
       
   105  	}
       
   106  	
       
   107 	/*debug*///print(this.code);
       
   108 	return output;
       
   109 }