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