JSDOC/Introspect/Link.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 Link    = imports['Template/Link.js'].Link;
11
12
13
14
15 Template = function(templateFile) {
16         if (templateFile) {
17         
18         this.template = Gio.simple_read(templateFile);
19         
20     }
21         
22         this.templateFile = templateFile;
23     this.templateName = GLib.path_get_basename(templateFile);
24         this.code = "";
25         this.parse();
26 }
27
28
29
30
31 XObject.extend(Template.prototype , {
32     
33
34     parse : function() {
35         
36         console.log("Parsing template? " + this.templateName);
37         
38         this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
39         this.code = "var output=``"+this.template;
40
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+=``";
45             }
46         );      
47         this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
48         this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
49         
50         this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
51         
52         //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
53         
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``";
60             }
61         );
62         //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
63         
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+=``";
70             }
71         );
72        //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
73         this.code = this.code+"``;";
74
75         
76         
77         this.code = this.code.replace(/(\r?\n)/g, "\\n");
78         this.code = this.code.replace(/"/g, "\\\"");
79         
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");
84         
85
86     },
87
88     toCode : function() {
89         return this.code;
90     },
91
92     keys : function(obj) {
93         var keys = [];
94         if (obj && obj.constructor.toString().indexOf("Array") > -1) {
95             for (var i = 0; i < obj.length; i++) {
96                 keys.push(i);
97             }
98         }
99         else {
100             for (var i in obj) {
101                 keys.push(i);
102             }
103         }
104         return keys;
105     },
106
107     values : function(obj) {
108         var values = [];
109         if (obj.constructor.toString().indexOf("Array") > -1) {
110             for (var i = 0; i < obj.length; i++) {
111                 values.push(obj[i]);
112             }
113         }
114         else {
115             for (var i in obj) {
116                 values.push(obj[i]);
117             }
118         }
119         
120         
121         return values;
122     },
123
124     process : function(data) {
125         
126         //console.log("processing template");
127         var keys = this.keys;
128         var values = this.values;
129         
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;
138         
139         
140         
141         try {
142              eval(this.code);
143            } catch (e) {
144              Gio.simple_write('/tmp/template.js', this.code);
145              Seed.print('in /tmp/template.js');
146             throw e;
147             Seed.quit();
148             }
149         
150         
151         //File.write("/tmp/jstookit_eval.js", this.code);
152         //try {
153         //eval('include     "/tmp/jstookit_eval.js";');
154         //includeJs("/tmp/jstookit_eval.js");
155             //eval(this.code);
156        // console.log("done eval of template");   
157         
158         return output;
159     },
160
161  
162     isdefined : function (typ) {
163         return typ != 'undefined';
164     },
165     
166     
167     summarize : function(desc) {
168         if (typeof desc != "undefined")
169             return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
170     },
171
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;
179                 if (a > b) return 1;
180                 return 0;
181             }
182         }
183     },
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();
188         if (ns != 'Gtk' ) {
189             return '';//ns;
190         }
191         return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
192         
193         
194     },
195     
196     
197
198     makeSignature : function(params) {
199         if (!params) return "()";
200         var signature = "("     +
201             params.filter(
202                 function($) {
203                     return $.name.indexOf(".") == -1; // don't show config params in signature
204                 }
205             ).map(
206                 function($) {
207                     $.defaultValue = typeof($.defaultValue) == 'undefined' ? false : $.defaultValue;
208                     
209                     return "" +
210                         ($.isOptional ? "[" : "") +
211                         (($.type) ? 
212                             (new Link().toSymbol(
213                                 (typeof($.type) == 'object' ) ? 'Function' : $.type
214                             )) + " " :  ""
215                         )   + 
216                         "<B><i>" +$.name + "</i></B>" +
217                         ($.defaultValue ? "=" +item.defaultValue : "") +
218                         ($.isOptional ? "]" : "");
219                     
220                      
221                 }
222             ).join(", ")
223         +
224         ")";
225         return signature;
226     },
227
228     makeFuncSkel :  function(params) {
229         if (!params) return "function ()\n{\n\n}";
230         return "function ("     +
231             params.filter(
232                 function($) {
233                     return $.name.indexOf(".") == -1; // don't show config params in signature
234                 }
235             ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
236         +
237         ")\n{\n\n}";
238         
239     },
240
241     /** Find symbol {@link ...} strings in text and turn into html links */
242     resolveLinks : function (str, from) {
243         if (!str || typeof(str) == 'undefined') {
244             return '';
245         }
246         
247         // gtk specific. now..
248         // @ -> bold.. - they are arguments..
249         
250         str = str.replace(/@([a-z_]+)/gi,
251             function(match, symbolName) {
252                 return '<b>' + symbolName + '</b>';
253             }
254         );
255         // constants.
256         str = str.replace(/%([a-z_]+)/gi,
257             function(match, symbolName) {
258                 return '<b>' + symbolName + '</b>';
259             }
260         );
261         
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);
268             }
269         );
270         
271         str = str.replace(/\n/gi, '<br/>');
272         
273         /*
274         str = str.replace(/\{@link ([^} ]+) ?\}/gi,
275             function(match, symbolName) {
276                 return new Link().toSymbol(symbolName);
277             }
278         );
279         */
280         /*
281         str = str.replace(/\{([a-z\.\/]+)\}/gi,
282             function(match, symbolName) {
283                 //println("got match " + symbolName);
284                 bits = symbolName.split('/');
285                 var mret = '';
286                 for(var i = 0; i < bits.length; i++) {
287                     
288                     mret += (mret.length ? '&nbsp;|&nbsp;' : '') + new Link().toSymbol(bits[i]);
289                 }
290                 
291                 return mret; //new Link().toSymbol(symbolName);
292             }
293         );
294         */
295         // look for aaaa.\S+  ??? this might work???
296         /*
297         str = str.replace(/\([a-z]+\.\S+)/gi,
298             function(match, symbolName) {
299                 return new Link().toSymbol(symbolName);
300             }
301         );
302         */
303         
304         return str;
305     }
306     
307     
308 });