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