1 //<script type="text/javscript">
4 GLib = imports.gi.GLib;
6 XObject = imports.XObject.XObject;
8 console = imports.console.console;
18 Template = XObject.define(
22 XObject.extend(this, cfg)
23 //this.templateFile = templateFile;
24 if (!this.templateFile || !this.Link) {
25 throw "No templateFile or Link sent to Template Contructor..";
30 this.template = Gio.simple_read(this.templateFile);
31 this.templateName = GLib.path_get_basename(this.templateFile);
40 console.log("Parsing template? " + this.templateName);
42 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
43 this.code = "var output=``"+this.template;
45 this.code = this.code.replace(
46 /<for +each="(.+?)" +in="(.+?)" *>/gi,
47 function (match, eachName, inName) {
48 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+=``";
51 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
52 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
54 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
56 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
58 this.code = this.code.replace(
59 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
60 function (match, code) {
61 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
62 code = code.replace(/(\r?\n)/g, " ");
63 return "``+ \r("+code+") +\r``";
66 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
68 this.code = this.code.replace(
69 /\{!\s*([\s\S]+?)\s*!\}/gi,
70 function (match, code) {
71 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
72 code = code.replace(/(\n)/g, " "); // remove quotes..
73 return "``; "+code+";\routput+=``";
76 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
77 this.code = this.code+"``;";
81 this.code = this.code.replace(/(\r?\n)/g, "\\n");
82 this.code = this.code.replace(/"/g, "\\\"");
84 this.code = this.code.replace(/``/g, "\"");
85 this.code = this.code.replace(/\\r/g, "\n");
86 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
87 this.code = this.code.replace(/\r/g, "\n\n");
96 keys : function(obj) {
98 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
99 for (var i = 0; i < obj.length; i++) {
111 values : function(obj) {
113 if (obj.constructor.toString().indexOf("Array") > -1) {
114 for (var i = 0; i < obj.length; i++) {
128 process : function(data) {
130 //console.log("processing template");
131 var keys = this.keys;
132 var values = this.values;
134 var makeSortby = this.makeSortby;
135 var makeSignature = XObject.createDelegate(this.makeSignature, this);
136 var summarize = this.summarize ;
137 var makeFuncSkel = this.makeFuncSkel;
138 var resolveLinks = this.resolveLinks;
139 var makeImage = this.makeImage;
140 // usefull for cross refing..
141 Template.data = data;
143 var Link = this.Link;
148 Gio.simple_write('/tmp/template.js', this.code);
149 Seed.print('in /tmp/template.js');
155 //File.write("/tmp/jstookit_eval.js", this.code);
157 //eval('include "/tmp/jstookit_eval.js";');
158 //includeJs("/tmp/jstookit_eval.js");
160 // console.log("done eval of template");
166 isdefined : function (typ) {
167 return typ != 'undefined';
171 summarize : function(desc) {
172 if (typeof desc != "undefined")
173 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
176 /** make a symbol sorter by some attribute */
177 makeSortby : function(attribute) {
178 return function(a, b) {
179 if (a[attribute] != undefined && b[attribute] != undefined) {
180 a = a[attribute]; //.toLowerCase();
181 b = b[attribute];//.toLowerCase();
182 if (a < b) return -1;
188 makeImage : function(alias) {
189 /// http://library.gnome.org/devel/gtk/stable/notebook.png
190 var ns = alias.split('.').shift();
191 var cls = alias.split('.').pop().toLowerCase();
195 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
202 makeSignature : function(params) {
203 if (!params) return "()";
204 var Link = this.Link;
205 var signature = "(" +
208 return $.name.indexOf(".") == -1; // don't show config params in signature
212 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
215 ($.isOptional ? "[" : "") +
217 (new Link().toSymbol(
218 (typeof($.type) == 'object' ) ? 'Function' : $.type
221 "<B><i>" +$.name + "</i></B>" +
222 ($.defaultValue ? "=" +item.defaultValue : "") +
223 ($.isOptional ? "]" : "");
233 makeFuncSkel : function(params) {
234 if (!params) return "function ()\n{\n\n}";
235 return "function (" +
238 return $.name.indexOf(".") == -1; // don't show config params in signature
240 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
246 /** Find symbol {@link ...} strings in text and turn into html links */
247 resolveLinks : function (str, from) {
248 if (!str || typeof(str) == 'undefined') {
252 // gtk specific. now..
253 // @ -> bold.. - they are arguments..
255 str = str.replace(/@([a-z_]+)/gi,
256 function(match, symbolName) {
257 return '<b>' + symbolName + '</b>';
261 str = str.replace(/%([a-z_]+)/gi,
262 function(match, symbolName) {
263 return '<b>' + symbolName + '</b>';
267 str = str.replace(/#([a-z_]+)/gi,
268 function(match, symbolName) {
269 return '<b>' + symbolName + '</b>';
270 // this should do a lookup!!!!
271 /// it could use data in the signature to find out..
272 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
276 str = str.replace(/\n/gi, '<br/>');
279 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
280 function(match, symbolName) {
281 return new Link().toSymbol(symbolName);
286 str = str.replace(/\{([a-z\.\/]+)\}/gi,
287 function(match, symbolName) {
288 //println("got match " + symbolName);
289 bits = symbolName.split('/');
291 for(var i = 0; i < bits.length; i++) {
293 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
296 return mret; //new Link().toSymbol(symbolName);
300 // look for aaaa.\S+ ??? this might work???
302 str = str.replace(/\([a-z]+\.\S+)/gi,
303 function(match, symbolName) {
304 return new Link().toSymbol(symbolName);