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