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