1 //<script type="text/javscript">
4 GLib = imports.gi.GLib;
6 XObject = imports.XObject.XObject;
10 console = imports.console.console;
11 Link = imports['JSDOC/Template/Link.js'].Link;
16 Template = function(templateFile) {
19 this.template = Gio.simple_read(templateFile);
23 this.templateFile = templateFile;
24 this.templateName = GLib.path_get_basename(templateFile);
32 XObject.extend(Template.prototype , {
37 console.log("Parsing template? " + this.templateName);
39 this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
40 this.code = "var output=``"+this.template;
42 this.code = this.code.replace(
43 /<for +each="(.+?)" +in="(.+?)" *>/gi,
44 function (match, eachName, inName) {
45 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+=``";
48 this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
49 this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
51 this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
53 //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
55 this.code = this.code.replace(
56 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
57 function (match, code) {
58 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
59 code = code.replace(/(\r?\n)/g, " ");
60 return "``+ \r("+code+") +\r``";
63 //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
65 this.code = this.code.replace(
66 /\{!\s*([\s\S]+?)\s*!\}/gi,
67 function (match, code) {
68 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
69 code = code.replace(/(\n)/g, " "); // remove quotes..
70 return "``; "+code+";\routput+=``";
73 //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
74 this.code = this.code+"``;";
78 this.code = this.code.replace(/(\r?\n)/g, "\\n");
79 this.code = this.code.replace(/"/g, "\\\"");
81 this.code = this.code.replace(/``/g, "\"");
82 this.code = this.code.replace(/\\r/g, "\n");
83 //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
84 this.code = this.code.replace(/\r/g, "\n\n");
93 keys : function(obj) {
95 if (obj && obj.constructor.toString().indexOf("Array") > -1) {
96 for (var i = 0; i < obj.length; i++) {
108 values : function(obj) {
110 if (obj.constructor.toString().indexOf("Array") > -1) {
111 for (var i = 0; i < obj.length; i++) {
125 process : function(data) {
127 //console.log("processing template");
128 var keys = this.keys;
129 var values = this.values;
131 var makeSortby = this.makeSortby;
132 var makeSignature = this.makeSignature;
133 var summarize = this.summarize ;
134 var makeFuncSkel = this.makeFuncSkel;
135 var resolveLinks = this.resolveLinks;
136 var makeImage = this.makeImage;
137 // usefull for cross refing..
138 Template.data = data;
145 Gio.simple_write('/tmp/template.js', this.code);
146 Seed.print('in /tmp/template.js');
152 //File.write("/tmp/jstookit_eval.js", this.code);
154 //eval('include "/tmp/jstookit_eval.js";');
155 //includeJs("/tmp/jstookit_eval.js");
157 // console.log("done eval of template");
163 isdefined : function (typ) {
164 return typ != 'undefined';
168 summarize : function(desc) {
169 if (typeof desc != "undefined")
170 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
173 /** make a symbol sorter by some attribute */
174 makeSortby : function(attribute) {
175 return function(a, b) {
176 if (a[attribute] != undefined && b[attribute] != undefined) {
177 a = a[attribute]; //.toLowerCase();
178 b = b[attribute];//.toLowerCase();
179 if (a < b) return -1;
185 makeImage : function(alias) {
186 /// http://library.gnome.org/devel/gtk/stable/notebook.png
187 var ns = alias.split('.').shift();
188 var cls = alias.split('.').pop().toLowerCase();
192 return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
199 makeSignature : function(params) {
200 if (!params) return "()";
201 var signature = "(" +
204 return $.name.indexOf(".") == -1; // don't show config params in signature
208 $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
211 ($.isOptional ? "[" : "") +
213 (new Link().toSymbol(
214 (typeof($.type) == 'object' ) ? 'Function' : $.type
217 "<B><i>" +$.name + "</i></B>" +
218 ($.defaultValue ? "=" +item.defaultValue : "") +
219 ($.isOptional ? "]" : "");
229 makeFuncSkel : function(params) {
230 if (!params) return "function ()\n{\n\n}";
231 return "function (" +
234 return $.name.indexOf(".") == -1; // don't show config params in signature
236 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
242 /** Find symbol {@link ...} strings in text and turn into html links */
243 resolveLinks : function (str, from) {
244 if (!str || typeof(str) == 'undefined') {
248 // gtk specific. now..
249 // @ -> bold.. - they are arguments..
251 str = str.replace(/@([a-z_]+)/gi,
252 function(match, symbolName) {
253 return '<b>' + symbolName + '</b>';
257 str = str.replace(/%([a-z_]+)/gi,
258 function(match, symbolName) {
259 return '<b>' + symbolName + '</b>';
263 str = str.replace(/#([a-z_]+)/gi,
264 function(match, symbolName) {
265 return '<b>' + symbolName + '</b>';
266 // this should do a lookup!!!!
267 /// it could use data in the signature to find out..
268 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
272 str = str.replace(/\n/gi, '<br/>');
275 str = str.replace(/\{@link ([^} ]+) ?\}/gi,
276 function(match, symbolName) {
277 return new Link().toSymbol(symbolName);
282 str = str.replace(/\{([a-z\.\/]+)\}/gi,
283 function(match, symbolName) {
284 //println("got match " + symbolName);
285 bits = symbolName.split('/');
287 for(var i = 0; i < bits.length; i++) {
289 mret += (mret.length ? ' | ' : '') + new Link().toSymbol(bits[i]);
292 return mret; //new Link().toSymbol(symbolName);
296 // look for aaaa.\S+ ??? this might work???
298 str = str.replace(/\([a-z]+\.\S+)/gi,
299 function(match, symbolName) {
300 return new Link().toSymbol(symbolName);