More commits to sync working copy
[gnome.introspection-doc-generator] / JSDOC / Introspect.js
1 //<script type="text/javascript">
2 //Gtk = imports.gi.Gtk;
3 GI      = imports.gi.GIRepository;
4 GLib    = imports.gi.GLib;
5 xml     = imports.libxml;
6
7
8 imports['Object.js'].load(Object);
9
10 console = imports['console.js'].console;
11 JSDOC   = imports['JSDOC.js'].JSDOC;
12
13 xnew    = imports['xnew.js'].xnew;
14
15  
16
17
18 Introspect = {};
19
20 xnew.load(Introspect,'JSDOC/Introspect');
21
22
23
24 Object.extend(Introspect, {
25     
26    
27     references : { }, 
28     
29     namespaces : function(ns) 
30     {
31         // this should be a class of it's own...
32         this.references[ns] = []; // technically not needed - but fills in files..
33         // get this from GI... (it's the path..)
34         var ret = [];
35        
36         function scanGir(dir) 
37         {
38             if (!GLib.file_test(dir, GLib.FileTest.EXISTS)) {
39                 return;
40             }
41             var gdir = GLib.dir_open(dir,0);
42             
43             while (true) {
44                 
45                 var fn = GLib.dir_read_name(gdir);
46            //      console.log('trying ' +  fn);
47                 if (!fn) {
48                     GLib.dir_close(gdir);
49                     return;;
50                 }
51                 if (!fn.match(/.typelib$/)) {
52                     continue;
53                 }
54                 var par = fn.split('-').shift();
55                  //console.log('trying ' +  par);
56                 if (ret.indexOf(par) > -1) {
57                      continue;
58                 }
59                 ret.push(par);
60                 
61                 
62             } 
63         }
64         var gi = GI.IRepository.get_default();
65         var pth = GI.IRepository.get_search_path ();
66         
67         scanGir(pth[0]);
68         ret.sort();
69         console.dump(ret);
70         return ret;
71         
72     },
73         
74         
75     ns:  function(ns) {
76         var gi = GI.IRepository.get_default();
77         ret = {
78             titleType: 'Namespace',
79             ns: ns,
80             name : ns,
81             alias : ns,
82             objects : [],
83             functions : [],
84             enums : [],
85             structs: [],
86             constants : [],
87             unions : [],
88             
89             // so ns looks like class..
90           
91             extendsClasses : [], // what it extends...
92             childClasses : [], // what uses it..
93             properties : [],
94             constructors : [],
95             methods : [],
96             values : [], /// really constants.
97             signals : [],
98             interfaces: [],
99         };
100      
101         for (var i=0; i <  gi.get_n_infos (ns); i++ ) {
102             var info = gi.get_info (ns, i);
103             
104             var info_type = GI.base_info_get_type (info);
105             switch(info_type) {
106                 case  GI.IInfoType.OBJECT:
107                     ret.objects.push(GI.base_info_get_name(info));
108                     this.clsGatherInterfaces(ns , GI.base_info_get_name(info));
109                     continue;
110                  case  GI.IInfoType.INTERFACE:
111                     ret.interfaces.push(GI.base_info_get_name(info));
112                     continue;
113                 case  GI.IInfoType.FUNCTION:
114                     new Introspect.Method(info, ret, 'functions', []);    
115                     continue;
116                 
117                 case  GI.IInfoType.CALLBACK:
118                    // new Introspect.Callback(info, ret, 'callbacks', []);
119                     continue;
120                 
121                 case  GI.IInfoType.ENUM:
122                 case  GI.IInfoType.FLAGS:
123                     ret.enums.push(GI.base_info_get_name(info));
124                     continue;
125                 case  GI.IInfoType.STRUCT:
126                     if (GI.struct_info_is_gtype_struct (info)) {
127                         continue;
128                     }
129
130                     ret.structs.push(GI.base_info_get_name(info));
131                     continue;
132                 case  GI.IInfoType.UNION:
133                     ret.unions.push(GI.base_info_get_name(info));
134                     continue;
135                 case  GI.IInfoType.CONSTANT:
136                     new Introspect.Constant(info, ret, 'values', []);
137                     
138                     continue;
139                 
140                 
141                 default:
142                     continue;
143             }
144         }
145         //console.dump(this.ifaceList);
146         return ret;
147
148     },
149  
150     
151   
152     // store all the interfaces, so we can show a list of them later...
153     // called when you list the namespace
154     clsGatherInterfaces : function(ns, cls)
155     {
156         var gi = GI.IRepository.get_default();
157         var bb = gi.find_by_name(ns, cls);
158         var fullname = ns+'.'+cls;
159         this.ifaceList = this.ifaceList || { };
160          
161         
162         for(var i =0; i < GI.object_info_get_n_interfaces(bb); i++) {
163            
164             var prop = GI.object_info_get_interface(bb,i);
165            
166             var add =  GI.base_info_get_namespace(prop) +'.' + GI.base_info_get_name(prop);
167             this.ifaceList[add] = this.ifaceList[add] || [];
168             if (this.ifaceList[add].indexOf(fullname) < 0) {
169                 this.ifaceList[add].push(fullname);
170             }
171              
172         }
173         
174        
175         
176     },
177     
178            
179         
180    
181     doc : function(what) {
182         var ns = what.split('.').shift();
183         this.commentLoad(ns);
184         return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
185         
186     },
187     
188     
189     
190     comments : {},
191     
192     commentLoad : function(ns)
193     {
194         
195         if (typeof(this.comments[ns]) != 'undefined') {
196             return;
197         }
198         
199         console.log("LOAD DOCS: " + ns);
200         var gi = GI.IRepository.get_default();
201         var ver = gi.get_version(ns);
202         if (!ver) {
203             this.comments[ns] = {};
204             return;
205         }
206         var ret = { };
207         
208         // no idea why this is broken on my build system.
209         var  getAttribute = function(n, name){
210             var properties = n.properties;
211             while (properties){
212                 if (properties.name == name)
213                     return properties.children.content;
214                 properties = properties.next
215             }
216             return null;
217         }
218                 
219         
220         function walk (element, path) {
221             
222             
223             if (!element) {
224                 return;
225             }
226             
227             var n =  getAttribute(element, 'name') ;
228             //console.log("WALK" + n);
229             if (n) {
230                 path += path.length ? '.' : '';
231                 path += n;
232             }
233             if (element.name == 'return-value') {
234                 path += '.return-value';
235             }
236             
237             
238             var d =   getAttribute(element,'doc');
239             if (d) {
240              //   Seed.print(path + ':' + d);
241                 ret[path] = d;
242             }
243             
244             var child = element.children;
245
246             while (child){
247                 //console.log(child.tag);
248                 if (child.type == "element"){
249                     walk (child, path);
250                 }
251                 child = child.next;
252             }
253         }
254         
255         var pth = GI.IRepository.get_search_path ();
256         
257         
258         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
259        
260         
261         //console.log(fn);
262         var  fn = gir_path + '/'+ ns + '-' + ver + '.gir';
263        // console.log(fn);
264         
265         if (!GLib.file_test(fn, GLib.FileTest.EXISTS)) {
266             console.log('missing docc file ' + fn);
267             this.comments[ns] = {};
268             
269             return;
270         }
271         var doc = xml.parseFile(fn);
272         //console.log("xmldoc?" + doc);
273         walk (doc.root, '');
274         //console.dump(ret);
275         this.comments[ns] = ret;
276
277     },
278     registry : { },
279     factory : function(type, ns, name) {
280         if (typeof (this.registry[ns +'.' + name]) == 'undefined') {
281             this.registry[ns +'.' + name] = new Introspect[type](ns,name);
282             this.registry[ns +'.' + name].load();
283         }
284         
285         return this.registry[ns +'.' + name];
286     }
287         
288             
289     
290 });
291
292
293