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