1 //<script type="text/javscript">
3 const Gio = imports.gi.Gio;
4 const GLib = imports.gi.GLib;
6 const XObject = imports.XObject.XObject;
7 const console = imports.console.console;
9 const File = imports.File.File;
17 var Template = XObject.define(
20 XObject.extend(this, cfg);
22 //this.templateFile = templateFile;
24 if (!this.templateFile || !this.Link) {
25 throw "No templateFile or Link sent to Template Constructor..";
27 //console.log("this.templateFile: " + this.templateFile);
29 //this.template = Gio.simple_read(this.templateFile);
30 this.template = File.read(this.templateFile);
31 //console.log("this.template: " + this.template);
33 this.templateName = GLib.path_get_basename(this.templateFile);
43 console.log("Parsing template: " + this.templateName);
44 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
45 this.code = "var output=``" + this.template;
47 this.code = this.code.replace(
48 /<for +each="(.+?)" +in="(.+?)" *>/gi,
49 function (match, eachName, inName) {
50 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+=``";
54 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
55 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
57 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
59 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
61 this.code = this.code.replace(
62 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
63 function (match, code) {
64 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
65 code = code.replace(/(\r?\n)/g, " ");
66 return "``+ \r("+code+") +\r``";
69 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
71 this.code = this.code.replace(
72 /\{!\s*([\s\S]+?)\s*!\}/gi,
73 function (match, code) {
74 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
75 code = code.replace(/(\n)/g, " "); // remove quotes..
76 return "``; "+code+";\routput+=``";
79 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
80 this.code = this.code+"``;";
82 this.code = this.code.replace(/(\r?\n)/g, "\\n");
83 this.code = this.code.replace(/"/g, "\\\"");
85 this.code = this.code.replace(/``/g, "\"");
86 this.code = this.code.replace(/\\r/g, "\n");
87 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
88 this.code = this.code.replace(/\r/g, "\n\n");
95 keys : function(obj) {
97 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
98 for (var i = 0; i < obj.length; i++) {
110 values : function(obj) {
112 if (obj.constructor.toString().indexOf("Array") > -1) {
113 for (var i = 0; i < obj.length; i++) {
127 process : function(data) {
129 //console.log("processing template");
130 var keys = this.keys;
131 var values = this.values;
133 var makeSortby = this.makeSortby;
134 var makeSignature = XObject.createDelegate(this.makeSignature, this);
135 var summarize = this.summarize ;
136 var makeFuncSkel = this.makeFuncSkel;
137 var resolveLinks = this.resolveLinks;
138 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;
149 //Gio.simple_write('/tmp/template.js', this.code);
150 File.write('/tmp/template.js', this.code);
151 //Seed.print('in /tmp/template.js');
152 console.log('in /tmp/template.js');
157 //File.write("/tmp/jstookit_eval.js", this.code);
159 //eval('include "/tmp/jstookit_eval.js";');
160 //includeJs("/tmp/jstookit_eval.js");
162 // console.log("done eval of template");
167 isdefined : function (typ) {
168 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;
178 /** make a symbol sorter by some attribute */
179 makeSortby : function(attribute) {
180 return function(a, b) {
181 if (a[attribute] != undefined && b[attribute] != undefined) {
182 a = a[attribute]; //.toLowerCase();
183 b = b[attribute];//.toLowerCase();
184 if (a < b) return -1;
192 makeImage : function(alias) {
193 /// http://library.gnome.org/devel/gtk/stable/notebook.png
194 var ns = alias.split('.').shift();
195 var cls = alias.split('.').pop().toLowerCase();
199 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) {
249 if (!str || typeof(str) == 'undefined') {
253 // gtk specific. now..
254 // @ -> bold.. - they are arguments..
256 str = str.replace(/@([a-z_]+)/gi,
257 function(match, symbolName) {
258 return '<b>' + symbolName + '</b>';
263 str = str.replace(/%([a-z_]+)/gi,
264 function(match, symbolName) {
265 return '<b>' + symbolName + '</b>';
269 str = str.replace(/#([a-z_]+)/gi,
270 function(match, symbolName) {
271 return '<b>' + symbolName + '</b>';
272 // this should do a lookup!!!!
273 /// it could use data in the signature to find out..
274 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
278 str = str.replace(/\n/gi, '<br/>');
281 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
282 function(match, symbolName) {
283 return new Link().toSymbol(symbolName);
288 str = str.replace(/\{([a-z\.\/]+)\}/gi,
289 function(match, symbolName) {
290 //println("got match " + symbolName);
291 bits = symbolName.split('/');
293 for(var i = 0; i < bits.length; i++) {
295 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
298 return mret; //new Link().toSymbol(symbolName);
302 // look for aaaa.\S+ ??? this might work???
304 str = str.replace(/\([a-z]+\.\S+)/gi,
305 function(match, symbolName) {
306 return new Link().toSymbol(symbolName);