1 //<script type="text/javscript">
4 GLib = imports.gi.GLib;
6 XObject = imports.XObject.XObject;
8 console = imports.console.console;
10 Link = imports['Template/Link.js'].Link;
15 Template = function(templateFile) {
18 this.template = Gio.simple_read(templateFile);
22 this.templateFile = templateFile;
23 this.templateName = GLib.path_get_basename(templateFile);
31 XObject.extend(Template.prototype , {
36 console.log("Parsing template? " + this.templateName);
38 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
39 this.code = "var output=``"+this.template;
41 this.code = this.code.replace(
42 /<for +each="(.+?)" +in="(.+?)" *>/gi,
43 function (match, eachName, inName) {
44 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+=``";
47 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
48 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
50 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
52 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
54 this.code = this.code.replace(
55 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
56 function (match, code) {
57 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
58 code = code.replace(/(\r?\n)/g, " ");
59 return "``+ \r("+code+") +\r``";
62 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
64 this.code = this.code.replace(
65 /\{!\s*([\s\S]+?)\s*!\}/gi,
66 function (match, code) {
67 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
68 code = code.replace(/(\n)/g, " "); // remove quotes..
69 return "``; "+code+";\routput+=``";
72 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
73 this.code = this.code+"``;";
77 this.code = this.code.replace(/(\r?\n)/g, "\\n");
78 this.code = this.code.replace(/"/g, "\\\"");
80 this.code = this.code.replace(/``/g, "\"");
81 this.code = this.code.replace(/\\r/g, "\n");
82 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
83 this.code = this.code.replace(/\r/g, "\n\n");
92 keys : function(obj) {
94 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
95 for (var i = 0; i < obj.length; i++) {
107 values : function(obj) {
109 if (obj.constructor.toString().indexOf("Array") > -1) {
110 for (var i = 0; i < obj.length; i++) {
124 process : function(data) {
126 //console.log("processing template");
127 var keys = this.keys;
128 var values = this.values;
130 var makeSortby = this.makeSortby;
131 var makeSignature = this.makeSignature;
132 var summarize = this.summarize ;
133 var makeFuncSkel = this.makeFuncSkel;
134 var resolveLinks = this.resolveLinks;
135 var makeImage = this.makeImage;
136 // usefull for cross refing..
137 Template.data = data;
144 Gio.simple_write('/tmp/template.js', this.code);
145 Seed.print('in /tmp/template.js');
151 //File.write("/tmp/jstookit_eval.js", this.code);
153 //eval('include "/tmp/jstookit_eval.js";');
154 //includeJs("/tmp/jstookit_eval.js");
156 // console.log("done eval of template");
162 isdefined : function (typ) {
163 return typ != 'undefined';
167 summarize : function(desc) {
168 if (typeof desc != "undefined")
169 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
172 /** make a symbol sorter by some attribute */
173 makeSortby : function(attribute) {
174 return function(a, b) {
175 if (a[attribute] != undefined && b[attribute] != undefined) {
176 a = a[attribute]; //.toLowerCase();
177 b = b[attribute];//.toLowerCase();
178 if (a < b) return -1;
184 makeImage : function(alias) {
185 /// http://library.gnome.org/devel/gtk/stable/notebook.png
186 var ns = alias.split('.').shift();
187 var cls = alias.split('.').pop().toLowerCase();
191 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
198 makeSignature : function(params) {
199 if (!params) return "()";
200 var signature = "(" +
203 return $.name.indexOf(".") == -1; // don't show config params in signature
207 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
210 ($.isOptional ? "[" : "") +
212 (new Link().toSymbol(
213 (typeof($.type) == 'object' ) ? 'Function' : $.type
216 "<B><i>" +$.name + "</i></B>" +
217 ($.defaultValue ? "=" +item.defaultValue : "") +
218 ($.isOptional ? "]" : "");
228 makeFuncSkel : function(params) {
229 if (!params) return "function ()\n{\n\n}";
230 return "function (" +
233 return $.name.indexOf(".") == -1; // don't show config params in signature
235 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
241 /** Find symbol {@link ...} strings in text and turn into html links */
242 resolveLinks : function (str, from) {
243 if (!str || typeof(str) == 'undefined') {
247 // gtk specific. now..
248 // @ -> bold.. - they are arguments..
250 str = str.replace(/@([a-z_]+)/gi,
251 function(match, symbolName) {
252 return '<b>' + symbolName + '</b>';
256 str = str.replace(/%([a-z_]+)/gi,
257 function(match, symbolName) {
258 return '<b>' + symbolName + '</b>';
262 str = str.replace(/#([a-z_]+)/gi,
263 function(match, symbolName) {
264 return '<b>' + symbolName + '</b>';
265 // this should do a lookup!!!!
266 /// it could use data in the signature to find out..
267 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
271 str = str.replace(/\n/gi, '<br/>');
274 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
275 function(match, symbolName) {
276 return new Link().toSymbol(symbolName);
281 str = str.replace(/\{([a-z\.\/]+)\}/gi,
282 function(match, symbolName) {
283 //println("got match " + symbolName);
284 bits = symbolName.split('/');
286 for(var i = 0; i < bits.length; i++) {
288 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
291 return mret; //new Link().toSymbol(symbolName);
295 // look for aaaa.\S+ ??? this might work???
297 str = str.replace(/\([a-z]+\.\S+)/gi,
298 function(match, symbolName) {
299 return new Link().toSymbol(symbolName);