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