PageRenderTime 20ms CodeModel.GetById 6ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/branches/jsdoc_tk_gui/setup/jsDoc/JsPlate.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 130 lines | 107 code | 6 blank | 17 comment | 1 complexity | c2ec3a3d1a14fcc2762b756424e3c07b MD5 | raw file
  1/**
  2 * @fileOverview
  3 * @name JsPlate
  4 * @author Michael Mathews micmath@gmail.com
  5 * @url $HeadURL: https://jsdoc-toolkit.googlecode.com/svn/branches/jsdoc_tk_gui/setup/app/JsPlate.js $
  6 * @revision $Id: JsPlate.js 313 2007-11-11 22:01:03Z sebastien.bordes $
  7 * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8 *          (See the accompanying README file for full details.)
  9
 10 */
 11
 12 /**
 13  * @class A lightweight template engine for JavaScript.
 14  * @constructor
 15  * @author Michael Mathews <a href="mailto:micmath@gmail.com">micmath@gmail.com</a>
 16  * @param {string} template
 17  */
 18JsPlate = function(template) {
 19	this.template = IO.readFile(template);
 20
 21	this.code = "";
 22	this.parse();
 23}
 24
 25/** Converts a template into evalable code. */
 26JsPlate.prototype.parse = function() {
 27	this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
 28	this.code = "var output=``"+this.template;
 29
 30	this.code = this.code.replace(
 31		/<for each="(.+?)" in="(.+?)"(?: sortby="(.+?)")?>/g, 
 32		function (match, eachName, inName, sortby) {
 33			if (!sortby) sortby = "asis";
 34			
 35			return "``; var $"+eachName+"_keys = "+sortby+"("+inName+"); for(var $"+eachName+"_i = 0; $"+eachName+"_i < $"+eachName+"_keys.length; $"+eachName+"_i++) { var $"+eachName+"_last = ($"+eachName+"_i == $"+eachName+"_keys.length-1); var $"+eachName+"_key = $"+eachName+"_keys[$"+eachName+"_i]; var "+eachName+" = "+inName+"[$"+eachName+"_key]; output+=``";
 36		}
 37	);	
 38	this.code = this.code.replace(/<if test="(.+?)">/g, "``; if ($1) { output+=``");
 39	this.code = this.code.replace(/<\/(if|for)>/g, "``; }; output+=``");
 40	this.code = this.code.replace(
 41		/\{\+\s*([\s\S]+?)\s*\+\}/gi,
 42		function (match, code) {
 43			code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
 44			code = code.replace(/(\r?\n)/g, " ");
 45			return "``+"+code+"+``";
 46		}
 47	);
 48	this.code = this.code.replace(
 49		/\{!\s*([\s\S]+?)\s*!\}/gi,
 50		function (match, code) {
 51			code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
 52			code = code.replace(/(\r?\n)/g, " ");
 53			return "``; "+code+"; output+=``";
 54		}
 55	);
 56	this.code = this.code+"``;";
 57
 58	this.code = this.code.replace(/(\r?\n)/g, "\\n");
 59	this.code = this.code.replace(/"/g, "\\\"");
 60	this.code = this.code.replace(/``/g, "\"");
 61}
 62
 63/**
 64 * @private
 65 */
 66JsPlate.prototype.toCode = function() {
 67	return this.code;
 68}
 69
 70/**
 71 * @private
 72 * @static
 73 * @memberOf JsPlate
 74 */
 75JsPlate.keys = function(obj) {
 76	var keys = [];
 77	if (obj.constructor.toString().indexOf("Array") > -1) {
 78		for (var i = 0; i < obj.length; i++) keys.push(i);
 79	}
 80	else {
 81		for (var i in obj) { keys.push(i); }
 82	}
 83	return keys.sort();
 84};
 85
 86/**
 87 * @private
 88 * @static
 89 * @memberOf JsPlate
 90 */
 91JsPlate.values = function(obj) {
 92	var values = [];
 93	if (obj.constructor.toString().indexOf("Array") > -1) {
 94		for (var i = 0; i < obj.length; i++) values.push(obj[i]);
 95	}
 96	else {
 97		for (var i in obj) { values.push(obj[i]); }
 98	}
 99	return values.sort();
100};
101
102/**
103 * @private
104 * @static
105 * @memberOf JsPlate
106 */
107JsPlate.asis = function(obj) {
108	var keys = [];
109	if (obj.constructor.toString().indexOf("Array") > -1) {
110		for (var i = 0; i < obj.length; i++) keys.push(i);
111	}
112	else {
113		for (var i in obj) { keys.push(i); }
114	}
115	return keys;
116};
117
118/**
119 * Return the output. This must be called after parse()
120 * @param {object} data What shall represent the "data" in your template.
121 * @return {string}
122 */
123JsPlate.prototype.process = function(data) {
124	var keys = JsPlate.keys;
125	var values = JsPlate.values;
126	var asis = JsPlate.asis;
127	eval(this.code);
128	return output;
129	//print(this.code)
130}