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..";
31 this.template = Gio.simple_read(templateFile);
32 this.templateName = GLib.path_get_basename(templateFile);
41 console.log("Parsing template? " + this.templateName);
43 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
44 this.code = "var output=``"+this.template;
46 this.code = this.code.replace(
47 /<for +each="(.+?)" +in="(.+?)" *>/gi,
48 function (match, eachName, inName) {
49 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+=``";
52 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
53 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
55 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
57 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
59 this.code = this.code.replace(
60 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
61 function (match, code) {
62 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
63 code = code.replace(/(\r?\n)/g, " ");
64 return "``+ \r("+code+") +\r``";
67 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.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(/(\n)/g, " "); // remove quotes..
74 return "``; "+code+";\routput+=``";
77 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
78 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");
97 keys : function(obj) {
99 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
100 for (var i = 0; i < obj.length; i++) {
112 values : function(obj) {
114 if (obj.constructor.toString().indexOf("Array") > -1) {
115 for (var i = 0; i < obj.length; i++) {
129 process : function(data) {
131 //console.log("processing template");
132 var keys = this.keys;
133 var values = this.values;
135 var makeSortby = this.makeSortby;
136 var makeSignature = this.makeSignature;
137 var summarize = this.summarize ;
138 var makeFuncSkel = this.makeFuncSkel;
139 var resolveLinks = this.resolveLinks;
140 var makeImage = this.makeImage;
141 // usefull for cross refing..
142 Template.data = data;
144 var Link = this.Link;
149 Gio.simple_write('/tmp/template.js', this.code);
150 Seed.print('in /tmp/template.js');
156 //File.write("/tmp/jstookit_eval.js", this.code);
158 //eval('include "/tmp/jstookit_eval.js";');
159 //includeJs("/tmp/jstookit_eval.js");
161 // console.log("done eval of template");
167 isdefined : function (typ) {
168 return typ != 'undefined';
172 summarize : function(desc) {
173 if (typeof desc != "undefined")
174 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
177 /** make a symbol sorter by some attribute */
178 makeSortby : function(attribute) {
179 return function(a, b) {
180 if (a[attribute] != undefined && b[attribute] != undefined) {
181 a = a[attribute]; //.toLowerCase();
182 b = b[attribute];//.toLowerCase();
183 if (a < b) return -1;
189 makeImage : function(alias) {
190 /// http://library.gnome.org/devel/gtk/stable/notebook.png
191 var ns = alias.split('.').shift();
192 var cls = alias.split('.').pop().toLowerCase();
196 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
203 makeSignature : function(params) {
204 if (!params) return "()";
206 var signature = "(" +
209 return $.name.indexOf(".") == -1; // don't show config params in signature
213 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
216 ($.isOptional ? "[" : "") +
218 (new _this.Link().toSymbol(
219 (typeof($.type) == 'object' ) ? 'Function' : $.type
222 "<B><i>" +$.name + "</i></B>" +
223 ($.defaultValue ? "=" +item.defaultValue : "") +
224 ($.isOptional ? "]" : "");
234 makeFuncSkel : function(params) {
235 if (!params) return "function ()\n{\n\n}";
236 return "function (" +
239 return $.name.indexOf(".") == -1; // don't show config params in signature
241 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
247 /** Find symbol {@link ...} strings in text and turn into html links */
248 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>';
262 str = str.replace(/%([a-z_]+)/gi,
263 function(match, symbolName) {
264 return '<b>' + symbolName + '</b>';
268 str = str.replace(/#([a-z_]+)/gi,
269 function(match, symbolName) {
270 return '<b>' + symbolName + '</b>';
271 // this should do a lookup!!!!
272 /// it could use data in the signature to find out..
273 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
277 str = str.replace(/\n/gi, '<br/>');
280 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
281 function(match, symbolName) {
282 return new Link().toSymbol(symbolName);
287 str = str.replace(/\{([a-z\.\/]+)\}/gi,
288 function(match, symbolName) {
289 //println("got match " + symbolName);
290 bits = symbolName.split('/');
292 for(var i = 0; i < bits.length; i++) {
294 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
297 return mret; //new Link().toSymbol(symbolName);
301 // look for aaaa.\S+ ??? this might work???
303 str = str.replace(/\([a-z]+\.\S+)/gi,
304 function(match, symbolName) {
305 return new Link().toSymbol(symbolName);