JSDOC/Template.js
[gnome.introspection-doc-generator] / JSDOC / Template.js
1 //<script type="text/javscript">
2
3 Gio = imports.gi.Gio;
4 GLib = imports.gi.GLib;
5
6 XObject = imports.XObject.XObject;
7
8 console = imports.console.console;
9
10 /**
11  * Template 
12  * 
13  * 
14  */
15   
16   
17
18 Template = XObject.define(
19     
20
21     function(cfg) {
22         XObject.extend(this, cfg)
23         //this.templateFile = templateFile;
24         if (!this.templateFile  || !this.Link) {
25             throw {
26                 "No templateFile or Link sent to Template Contructor..";
27                }
28            }
29         
30         
31         if (templateFile) {
32             
33             this.template = Gio.simple_read(templateFile);
34             
35         }
36         this.Link = this.Link || imports.TemplateLink.Link; ///????
37         
38         this.templateName = GLib.path_get_basename(templateFile);
39         this.code = "";
40         this.parse();
41     }
42
43
44
45
46 XObject.extend(Template.prototype , {
47     
48
49     parse : function() {
50         
51         console.log("Parsing template? " + this.templateName);
52         
53         this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
54         this.code = "var output=``"+this.template;
55
56         this.code = this.code.replace(
57             /<for +each="(.+?)" +in="(.+?)" *>/gi, 
58             function (match, eachName, inName) {
59                 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+=``";
60             }
61         );      
62         this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
63         this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
64         
65         this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
66         
67         //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
68         
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(/(\r?\n)/g, " ");
74                 return "``+ \r("+code+") +\r``";
75             }
76         );
77         //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
78         
79         this.code = this.code.replace(
80             /\{!\s*([\s\S]+?)\s*!\}/gi,
81             function (match, code) {
82                 code = code.replace(/"/g, "``"); // prevent qoute-escaping of inline code
83                 code = code.replace(/(\n)/g, " "); // remove quotes..
84                 return "``; "+code+";\routput+=``";
85             }
86         );
87        //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
88         this.code = this.code+"``;";
89
90         
91         
92         this.code = this.code.replace(/(\r?\n)/g, "\\n");
93         this.code = this.code.replace(/"/g, "\\\"");
94         
95         this.code = this.code.replace(/``/g, "\"");
96         this.code = this.code.replace(/\\r/g, "\n");
97         //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
98         this.code = this.code.replace(/\r/g, "\n\n");
99         
100
101     },
102
103     toCode : function() {
104         return this.code;
105     },
106
107     keys : function(obj) {
108         var keys = [];
109         if (obj && obj.constructor.toString().indexOf("Array") > -1) {
110             for (var i = 0; i < obj.length; i++) {
111                 keys.push(i);
112             }
113         }
114         else {
115             for (var i in obj) {
116                 keys.push(i);
117             }
118         }
119         return keys;
120     },
121
122     values : function(obj) {
123         var values = [];
124         if (obj.constructor.toString().indexOf("Array") > -1) {
125             for (var i = 0; i < obj.length; i++) {
126                 values.push(obj[i]);
127             }
128         }
129         else {
130             for (var i in obj) {
131                 values.push(obj[i]);
132             }
133         }
134         
135         
136         return values;
137     },
138
139     process : function(data) {
140         
141         //console.log("processing template");
142         var keys = this.keys;
143         var values = this.values;
144         
145         var makeSortby = this.makeSortby;
146         var makeSignature = this.makeSignature;
147         var summarize = this.summarize ;
148         var makeFuncSkel = this.makeFuncSkel;
149         var resolveLinks = this.resolveLinks;
150         var makeImage = this.makeImage;
151         // usefull for cross refing..
152         Template.data = data;
153         
154         
155         
156         try {
157              eval(this.code);
158            } catch (e) {
159              Gio.simple_write('/tmp/template.js', this.code);
160              Seed.print('in /tmp/template.js');
161             throw e;
162             Seed.quit();
163             }
164         
165         
166         //File.write("/tmp/jstookit_eval.js", this.code);
167         //try {
168         //eval('include     "/tmp/jstookit_eval.js";');
169         //includeJs("/tmp/jstookit_eval.js");
170             //eval(this.code);
171        // console.log("done eval of template");   
172         
173         return output;
174     },
175
176  
177     isdefined : function (typ) {
178         return typ != 'undefined';
179     },
180     
181     
182     summarize : function(desc) {
183         if (typeof desc != "undefined")
184             return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
185     },
186
187     /** make a symbol sorter by some attribute */
188     makeSortby : function(attribute) {
189         return function(a, b) {
190             if (a[attribute] != undefined && b[attribute] != undefined) {
191                 a = a[attribute]; //.toLowerCase();
192                 b = b[attribute];//.toLowerCase();
193                 if (a < b) return -1;
194                 if (a > b) return 1;
195                 return 0;
196             }
197         }
198     },
199     makeImage : function(alias) {
200         ///  http://library.gnome.org/devel/gtk/stable/notebook.png
201         var ns = alias.split('.').shift();
202         var cls = alias.split('.').pop().toLowerCase();
203         if (ns != 'Gtk' ) {
204             return '';//ns;
205         }
206         return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
207         
208         
209     },
210     
211     
212
213     makeSignature : function(params) {
214         if (!params) return "()";
215         var signature = "("     +
216             params.filter(
217                 function($) {
218                     return $.name.indexOf(".") == -1; // don't show config params in signature
219                 }
220             ).map(
221                 function($) {
222                     $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
223                     
224                     return "" +
225                         ($.isOptional ? "[" : "") +
226                         (($.type) ? 
227                             (new Link().toSymbol(
228                                 (typeof($.type) == 'object' ) ? 'Function' : $.type
229                             )) + " " :  ""
230                         )   + 
231                         "<B><i>" +$.name + "</i></B>" +
232                         ($.defaultValue ? "=" +item.defaultValue : "") +
233                         ($.isOptional ? "]" : "");
234                     
235                      
236                 }
237             ).join(", ")
238         +
239         ")";
240         return signature;
241     },
242
243     makeFuncSkel :  function(params) {
244         if (!params) return "function ()\n{\n\n}";
245         return "function ("     +
246             params.filter(
247                 function($) {
248                     return $.name.indexOf(".") == -1; // don't show config params in signature
249                 }
250             ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
251         +
252         ")\n{\n\n}";
253         
254     },
255
256     /** Find symbol {@link ...} strings in text and turn into html links */
257     resolveLinks : function (str, from) {
258         if (!str || typeof(str) == 'undefined') {
259             return '';
260         }
261         
262         // gtk specific. now..
263         // @ -> bold.. - they are arguments..
264         
265         str = str.replace(/@([a-z_]+)/gi,
266             function(match, symbolName) {
267                 return '<b>' + symbolName + '</b>';
268             }
269         );
270         // constants.
271         str = str.replace(/%([a-z_]+)/gi,
272             function(match, symbolName) {
273                 return '<b>' + symbolName + '</b>';
274             }
275         );
276         
277         str = str.replace(/#([a-z_]+)/gi,
278             function(match, symbolName) {
279                 return '<b>' + symbolName + '</b>';
280                 // this should do a lookup!!!!
281                 /// it could use data in the signature to find out..
282                 //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
283             }
284         );
285         
286         str = str.replace(/\n/gi, '<br/>');
287         
288         /*
289         str = str.replace(/\{@link ([^} ]+) ?\}/gi,
290             function(match, symbolName) {
291                 return new Link().toSymbol(symbolName);
292             }
293         );
294         */
295         /*
296         str = str.replace(/\{([a-z\.\/]+)\}/gi,
297             function(match, symbolName) {
298                 //println("got match " + symbolName);
299                 bits = symbolName.split('/');
300                 var mret = '';
301                 for(var i = 0; i < bits.length; i++) {
302                     
303                     mret += (mret.length ? '&nbsp;|&nbsp;' : '') + new Link().toSymbol(bits[i]);
304                 }
305                 
306                 return mret; //new Link().toSymbol(symbolName);
307             }
308         );
309         */
310         // look for aaaa.\S+  ??? this might work???
311         /*
312         str = str.replace(/\([a-z]+\.\S+)/gi,
313             function(match, symbolName) {
314                 return new Link().toSymbol(symbolName);
315             }
316         );
317         */
318         
319         return str;
320     }
321     
322     
323 });