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) {
26 "No templateFile or Link sent to Template Contructor..";
33 this.template = Gio.simple_read(templateFile);
36 this.Link = this.Link || imports.TemplateLink.Link; ///????
38 this.templateName = GLib.path_get_basename(templateFile);
46 XObject.extend(Template.prototype , {
51 console.log("Parsing template? " + this.templateName);
53 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
54 this.code = "var output=``"+this.template;
56 this.code = this.code.replace(
57 /<for +each="(.+?)" +in="(.+?)" *>/gi,
58 function (match, eachName, inName) {
59 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+=``";
62 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
63 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
65 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
67 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
69 this.code = this.code.replace(
70 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
71 function (match, code) {
72 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
73 code = code.replace(/(\r?\n)/g, " ");
74 return "``+ \r("+code+") +\r``";
77 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
79 this.code = this.code.replace(
80 /\{!\s*([\s\S]+?)\s*!\}/gi,
81 function (match, code) {
82 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
83 code = code.replace(/(\n)/g, " "); // remove quotes..
84 return "``; "+code+";\routput+=``";
87 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
88 this.code = this.code+"``;";
92 this.code = this.code.replace(/(\r?\n)/g, "\\n");
93 this.code = this.code.replace(/"/g, "\\\"");
95 this.code = this.code.replace(/``/g, "\"");
96 this.code = this.code.replace(/\\r/g, "\n");
97 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
98 this.code = this.code.replace(/\r/g, "\n\n");
103 toCode : function() {
107 keys : function(obj) {
109 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
110 for (var i = 0; i < obj.length; i++) {
122 values : function(obj) {
124 if (obj.constructor.toString().indexOf("Array") > -1) {
125 for (var i = 0; i < obj.length; i++) {
139 process : function(data) {
141 //console.log("processing template");
142 var keys = this.keys;
143 var values = this.values;
145 var makeSortby = this.makeSortby;
146 var makeSignature = this.makeSignature;
147 var summarize = this.summarize ;
148 var makeFuncSkel = this.makeFuncSkel;
149 var resolveLinks = this.resolveLinks;
150 var makeImage = this.makeImage;
151 // usefull for cross refing..
152 Template.data = data;
159 Gio.simple_write('/tmp/template.js', this.code);
160 Seed.print('in /tmp/template.js');
166 //File.write("/tmp/jstookit_eval.js", this.code);
168 //eval('include "/tmp/jstookit_eval.js";');
169 //includeJs("/tmp/jstookit_eval.js");
171 // console.log("done eval of template");
177 isdefined : function (typ) {
178 return typ != 'undefined';
182 summarize : function(desc) {
183 if (typeof desc != "undefined")
184 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
187 /** make a symbol sorter by some attribute */
188 makeSortby : function(attribute) {
189 return function(a, b) {
190 if (a[attribute] != undefined && b[attribute] != undefined) {
191 a = a[attribute]; //.toLowerCase();
192 b = b[attribute];//.toLowerCase();
193 if (a < b) return -1;
199 makeImage : function(alias) {
200 /// http://library.gnome.org/devel/gtk/stable/notebook.png
201 var ns = alias.split('.').shift();
202 var cls = alias.split('.').pop().toLowerCase();
206 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
213 makeSignature : function(params) {
214 if (!params) return "()";
215 var signature = "(" +
218 return $.name.indexOf(".") == -1; // don't show config params in signature
222 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
225 ($.isOptional ? "[" : "") +
227 (new Link().toSymbol(
228 (typeof($.type) == 'object' ) ? 'Function' : $.type
231 "<B><i>" +$.name + "</i></B>" +
232 ($.defaultValue ? "=" +item.defaultValue : "") +
233 ($.isOptional ? "]" : "");
243 makeFuncSkel : function(params) {
244 if (!params) return "function ()\n{\n\n}";
245 return "function (" +
248 return $.name.indexOf(".") == -1; // don't show config params in signature
250 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
256 /** Find symbol {@link ...} strings in text and turn into html links */
257 resolveLinks : function (str, from) {
258 if (!str || typeof(str) == 'undefined') {
262 // gtk specific. now..
263 // @ -> bold.. - they are arguments..
265 str = str.replace(/@([a-z_]+)/gi,
266 function(match, symbolName) {
267 return '<b>' + symbolName + '</b>';
271 str = str.replace(/%([a-z_]+)/gi,
272 function(match, symbolName) {
273 return '<b>' + symbolName + '</b>';
277 str = str.replace(/#([a-z_]+)/gi,
278 function(match, symbolName) {
279 return '<b>' + symbolName + '</b>';
280 // this should do a lookup!!!!
281 /// it could use data in the signature to find out..
282 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
286 str = str.replace(/\n/gi, '<br/>');
289 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
290 function(match, symbolName) {
291 return new Link().toSymbol(symbolName);
296 str = str.replace(/\{([a-z\.\/]+)\}/gi,
297 function(match, symbolName) {
298 //println("got match " + symbolName);
299 bits = symbolName.split('/');
301 for(var i = 0; i < bits.length; i++) {
303 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
306 return mret; //new Link().toSymbol(symbolName);
310 // look for aaaa.\S+ ??? this might work???
312 str = str.replace(/\([a-z]+\.\S+)/gi,
313 function(match, symbolName) {
314 return new Link().toSymbol(symbolName);