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