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;
27 this.template = Gio.simple_read(templateFile);
30 this.Link = this.Link || imports.TemplateLink.Link; ///????
32 this.templateName = GLib.path_get_basename(templateFile);
40 XObject.extend(Template.prototype , {
45 console.log("Parsing template? " + this.templateName);
47 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
48 this.code = "var output=``"+this.template;
50 this.code = this.code.replace(
51 /<for +each="(.+?)" +in="(.+?)" *>/gi,
52 function (match, eachName, inName) {
53 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+=``";
56 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
57 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
59 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
61 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
63 this.code = this.code.replace(
64 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
65 function (match, code) {
66 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
67 code = code.replace(/(\r?\n)/g, " ");
68 return "``+ \r("+code+") +\r``";
71 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
73 this.code = this.code.replace(
74 /\{!\s*([\s\S]+?)\s*!\}/gi,
75 function (match, code) {
76 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
77 code = code.replace(/(\n)/g, " "); // remove quotes..
78 return "``; "+code+";\routput+=``";
81 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
82 this.code = this.code+"``;";
86 this.code = this.code.replace(/(\r?\n)/g, "\\n");
87 this.code = this.code.replace(/"/g, "\\\"");
89 this.code = this.code.replace(/``/g, "\"");
90 this.code = this.code.replace(/\\r/g, "\n");
91 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
92 this.code = this.code.replace(/\r/g, "\n\n");
101 keys : function(obj) {
103 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
104 for (var i = 0; i < obj.length; i++) {
116 values : function(obj) {
118 if (obj.constructor.toString().indexOf("Array") > -1) {
119 for (var i = 0; i < obj.length; i++) {
133 process : function(data) {
135 //console.log("processing template");
136 var keys = this.keys;
137 var values = this.values;
139 var makeSortby = this.makeSortby;
140 var makeSignature = this.makeSignature;
141 var summarize = this.summarize ;
142 var makeFuncSkel = this.makeFuncSkel;
143 var resolveLinks = this.resolveLinks;
144 var makeImage = this.makeImage;
145 // usefull for cross refing..
146 Template.data = data;
153 Gio.simple_write('/tmp/template.js', this.code);
154 Seed.print('in /tmp/template.js');
160 //File.write("/tmp/jstookit_eval.js", this.code);
162 //eval('include "/tmp/jstookit_eval.js";');
163 //includeJs("/tmp/jstookit_eval.js");
165 // console.log("done eval of template");
171 isdefined : function (typ) {
172 return typ != 'undefined';
176 summarize : function(desc) {
177 if (typeof desc != "undefined")
178 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
181 /** make a symbol sorter by some attribute */
182 makeSortby : function(attribute) {
183 return function(a, b) {
184 if (a[attribute] != undefined && b[attribute] != undefined) {
185 a = a[attribute]; //.toLowerCase();
186 b = b[attribute];//.toLowerCase();
187 if (a < b) return -1;
193 makeImage : function(alias) {
194 /// http://library.gnome.org/devel/gtk/stable/notebook.png
195 var ns = alias.split('.').shift();
196 var cls = alias.split('.').pop().toLowerCase();
200 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
207 makeSignature : function(params) {
208 if (!params) return "()";
209 var signature = "(" +
212 return $.name.indexOf(".") == -1; // don't show config params in signature
216 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
219 ($.isOptional ? "[" : "") +
221 (new Link().toSymbol(
222 (typeof($.type) == 'object' ) ? 'Function' : $.type
225 "<B><i>" +$.name + "</i></B>" +
226 ($.defaultValue ? "=" +item.defaultValue : "") +
227 ($.isOptional ? "]" : "");
237 makeFuncSkel : function(params) {
238 if (!params) return "function ()\n{\n\n}";
239 return "function (" +
242 return $.name.indexOf(".") == -1; // don't show config params in signature
244 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
250 /** Find symbol {@link ...} strings in text and turn into html links */
251 resolveLinks : function (str, from) {
252 if (!str || typeof(str) == 'undefined') {
256 // gtk specific. now..
257 // @ -> bold.. - they are arguments..
259 str = str.replace(/@([a-z_]+)/gi,
260 function(match, symbolName) {
261 return '<b>' + symbolName + '</b>';
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>';
274 // this should do a lookup!!!!
275 /// it could use data in the signature to find out..
276 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
280 str = str.replace(/\n/gi, '<br/>');
283 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
284 function(match, symbolName) {
285 return new Link().toSymbol(symbolName);
290 str = str.replace(/\{([a-z\.\/]+)\}/gi,
291 function(match, symbolName) {
292 //println("got match " + symbolName);
293 bits = symbolName.split('/');
295 for(var i = 0; i < bits.length; i++) {
297 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
300 return mret; //new Link().toSymbol(symbolName);
304 // look for aaaa.\S+ ??? this might work???
306 str = str.replace(/\([a-z]+\.\S+)/gi,
307 function(match, symbolName) {
308 return new Link().toSymbol(symbolName);