1 //<script type="text/javscript">
4 GLib = imports.gi.GLib;
6 XObject = imports.XObject.XObject;
8 console = imports.console.console;
9 Link = imports['JSDOC/Template/Link.js'].Link;
14 Template = function(templateFile) {
17 this.template = Gio.simple_read(templateFile);
21 this.templateFile = templateFile;
22 this.templateName = GLib.path_get_basename(templateFile);
30 XObject.extend(Template.prototype , {
35 console.log("Parsing template? " + this.templateName);
37 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
38 this.code = "var output=``"+this.template;
40 this.code = this.code.replace(
41 /<for +each="(.+?)" +in="(.+?)" *>/gi,
42 function (match, eachName, inName) {
43 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+=``";
46 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
47 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
49 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
51 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
53 this.code = this.code.replace(
54 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
55 function (match, code) {
56 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
57 code = code.replace(/(\r?\n)/g, " ");
58 return "``+ \r("+code+") +\r``";
61 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.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(/(\n)/g, " "); // remove quotes..
68 return "``; "+code+";\routput+=``";
71 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
72 this.code = this.code+"``;";
76 this.code = this.code.replace(/(\r?\n)/g, "\\n");
77 this.code = this.code.replace(/"/g, "\\\"");
79 this.code = this.code.replace(/``/g, "\"");
80 this.code = this.code.replace(/\\r/g, "\n");
81 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
82 this.code = this.code.replace(/\r/g, "\n\n");
91 keys : function(obj) {
93 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
94 for (var i = 0; i < obj.length; i++) {
106 values : function(obj) {
108 if (obj.constructor.toString().indexOf("Array") > -1) {
109 for (var i = 0; i < obj.length; i++) {
123 process : function(data) {
125 //console.log("processing template");
126 var keys = this.keys;
127 var values = this.values;
129 var makeSortby = this.makeSortby;
130 var makeSignature = this.makeSignature;
131 var summarize = this.summarize ;
132 var makeFuncSkel = this.makeFuncSkel;
133 var resolveLinks = this.resolveLinks;
134 var makeImage = this.makeImage;
135 // usefull for cross refing..
136 Template.data = data;
143 Gio.simple_write('/tmp/template.js', this.code);
144 Seed.print('in /tmp/template.js');
150 //File.write("/tmp/jstookit_eval.js", this.code);
152 //eval('include "/tmp/jstookit_eval.js";');
153 //includeJs("/tmp/jstookit_eval.js");
155 // console.log("done eval of template");
161 isdefined : function (typ) {
162 return typ != 'undefined';
166 summarize : function(desc) {
167 if (typeof desc != "undefined")
168 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
171 /** make a symbol sorter by some attribute */
172 makeSortby : function(attribute) {
173 return function(a, b) {
174 if (a[attribute] != undefined && b[attribute] != undefined) {
175 a = a[attribute]; //.toLowerCase();
176 b = b[attribute];//.toLowerCase();
177 if (a < b) return -1;
183 makeImage : function(alias) {
184 /// http://library.gnome.org/devel/gtk/stable/notebook.png
185 var ns = alias.split('.').shift();
186 var cls = alias.split('.').pop().toLowerCase();
190 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
197 makeSignature : function(params) {
198 if (!params) return "()";
199 var signature = "(" +
202 return $.name.indexOf(".") == -1; // don't show config params in signature
206 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
209 ($.isOptional ? "[" : "") +
211 (new Link().toSymbol(
212 (typeof($.type) == 'object' ) ? 'Function' : $.type
215 "<B><i>" +$.name + "</i></B>" +
216 ($.defaultValue ? "=" +item.defaultValue : "") +
217 ($.isOptional ? "]" : "");
227 makeFuncSkel : function(params) {
228 if (!params) return "function ()\n{\n\n}";
229 return "function (" +
232 return $.name.indexOf(".") == -1; // don't show config params in signature
234 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
240 /** Find symbol {@link ...} strings in text and turn into html links */
241 resolveLinks : function (str, from) {
242 if (!str || typeof(str) == 'undefined') {
246 // gtk specific. now..
247 // @ -> bold.. - they are arguments..
249 str = str.replace(/@([a-z_]+)/gi,
250 function(match, symbolName) {
251 return '<b>' + symbolName + '</b>';
255 str = str.replace(/%([a-z_]+)/gi,
256 function(match, symbolName) {
257 return '<b>' + symbolName + '</b>';
261 str = str.replace(/#([a-z_]+)/gi,
262 function(match, symbolName) {
263 return '<b>' + symbolName + '</b>';
264 // this should do a lookup!!!!
265 /// it could use data in the signature to find out..
266 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
270 str = str.replace(/\n/gi, '<br/>');
273 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
274 function(match, symbolName) {
275 return new Link().toSymbol(symbolName);
280 str = str.replace(/\{([a-z\.\/]+)\}/gi,
281 function(match, symbolName) {
282 //println("got match " + symbolName);
283 bits = symbolName.split('/');
285 for(var i = 0; i < bits.length; i++) {
287 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
290 return mret; //new Link().toSymbol(symbolName);
294 // look for aaaa.\S+ ??? this might work???
296 str = str.replace(/\([a-z]+\.\S+)/gi,
297 function(match, symbolName) {
298 return new Link().toSymbol(symbolName);