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