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, "\\r");
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, "\\r"); // 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;
144 var Options = imports.Options ? imports.Options.Options : false;
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 */
178 makeSortby : function (attributeA, attributeB) {
179 return function(aa, bb) {
182 if (aa[attributeA] != undefined && bb[attributeA] != undefined) {
183 a = aa[attributeA]; //.toLowerCase();
184 b = bb[attributeA];//.toLowerCase();
185 if (a < b) return -1;
187 // same.. try attribute b..
189 if (attributeB && (aa[attributeB] != undefined && bb[attributeB] != undefined)) {
190 a = aa[attributeB]; //.toLowerCase();
191 b = bb[attributeB];//.toLowerCase();
192 if (a < b) return -1;
194 // same.. try attribute b..
203 makeImage : function(alias) {
204 /// http://library.gnome.org/devel/gtk/stable/notebook.png
205 var ns = alias.split('.').shift();
206 var cls = alias.split('.').pop().toLowerCase();
210 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
217 makeSignature : function(params) {
218 if (!params) return "()";
219 var Link = this.Link;
220 var signature = "(" +
223 return $.name.indexOf(".") == -1; // don't show config params in signature
227 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
230 ($.isOptional ? "[" : "") +
232 (new Link().toSymbol(
233 (typeof($.type) == 'object' ) ? 'Function' : $.type
236 "<B><i>" +$.name + "</i></B>" +
237 ($.defaultValue ? "=" +item.defaultValue : "") +
238 ($.isOptional ? "]" : "");
248 makeFuncSkel : function(params) {
249 if (!params) return "function ()\n{\n\n}";
250 return "function (" +
253 return $.name.indexOf(".") == -1; // don't show config params in signature
255 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
261 /** Find symbol {@link ...} strings in text and turn into html links */
262 resolveLinks : function (str, from) {
263 if (!str || typeof(str) == 'undefined') {
267 // gtk specific. now..
268 // @ -> bold.. - they are arguments..
270 str = str.replace(/@([a-z_]+)/gi,
271 function(match, symbolName) {
272 return '<b>' + symbolName + '</b>';
276 str = str.replace(/%([a-z_]+)/gi,
277 function(match, symbolName) {
278 return '<b>' + symbolName + '</b>';
282 str = str.replace(/#([a-z_]+)/gi,
283 function(match, symbolName) {
284 return '<b>' + symbolName + '</b>';
285 // this should do a lookup!!!!
286 /// it could use data in the signature to find out..
287 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
291 str = str.replace(/\n/gi, '<br/>');
294 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
295 function(match, symbolName) {
296 return new Link().toSymbol(symbolName);
301 str = str.replace(/\{([a-z\.\/]+)\}/gi,
302 function(match, symbolName) {
303 //println("got match " + symbolName);
304 bits = symbolName.split('/');
306 for(var i = 0; i < bits.length; i++) {
308 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
311 return mret; //new Link().toSymbol(symbolName);
315 // look for aaaa.\S+ ??? this might work???
317 str = str.replace(/\([a-z]+\.\S+)/gi,
318 function(match, symbolName) {
319 return new Link().toSymbol(symbolName);