JSDOC/TokenReader.js
[gnome.introspection-doc-generator] / docs.js
1 //<Script type="text/javascript">
2
3
4 Gtk = imports.gi.Gtk;
5 Gio = imports.gi.Gio;
6 Gdk = imports.gi.Gdk;
7
8 // generic libraries
9 XObject     = imports.XObject.XObject;
10 File        = imports.File.File; 
11 console     = imports.console.console; 
12 Template    = imports.JsTemplate.Template.Template; 
13
14 // Introspecion specific..
15 NameSpace   = imports.Introspect.NameSpace.NameSpace; 
16 Link        = imports.Introspect.Link.Link; 
17
18
19
20 var outputdir = Seed.argv[2];
21
22 if (!outputdir) {
23     throw {
24         name: "ArgumentError", 
25         message: "No output directory specified on the command line\n" +
26           "Usage seed docs.js /var/www/seed  [Gtk] \n"
27     };
28 }
29
30
31 if (!File.isDirectory(outputdir)) {
32     console.log("Creating directory " + outputdir);
33     File.mkdir(outputdir);
34 };
35
36  
37 // Which libraries to build.
38
39 var ns_list = NameSpace.namespaces();
40 if (typeof(Seed.argv[3]) == 'string') {
41     console.log(Seed.argv.length);
42     ns_list = Seed.argv[3].split(',');
43 }
44
45
46 ns_list = ns_list.sort();
47 // let's try and load them, so we find out early what will fail.
48 print("loading library to make sure it works.");
49 ns_list_clean = [];
50 ns_list.forEach(function(ns_name) 
51 {
52     try {
53     
54         var  core = imports.gi[ns_name];
55         ns_list_clean.push(ns_name);
56     } catch( e) {
57         print(JSON.stringify(e));
58     }
59     
60 });
61 ns_list = ns_list_clean;
62
63
64
65 // which languages do we want to output for.
66 langs=[];
67 File.list(__script_path__ + '/templates/').forEach(function(f) {
68     if (!File.isDirectory(__script_path__ + '/templates/' + f)) {
69         return;
70     }
71     if (f == 'resources') {
72         return;
73     }
74     langs.push({
75         name : f,
76         cls_template       : new Template( {
77             templateFile : __script_path__ + '/templates/' + f + '/class.html',
78             Link : Link // links might be specific to languages..
79         }),
80         cls_ix_template    : new Template( {
81             templateFile : __script_path__ + '/templates/' + f + '/class_ix.html',
82             Link : Link // links might be specific to languages..
83         }),
84         reference_template : new Template({
85             templateFile : __script_path__ + '/templates/' + f + '/references.html',
86             Link : Link // links might be specific to languages..
87         }),
88     });
89 });
90
91
92 /*
93 var cls_template = new Template(__script_path__ + '/templates/class.html');
94 var cls_ix_template = new Template(__script_path__ + '/templates/class_ix.html');
95 var reference_template = new Template(__script_path__ + '/templates/references.html');
96 */
97
98 print("Looping throught namespaces");
99 var ns_idx = [];
100 ns_list.forEach(function(ns_name) 
101 {
102     
103     //if (ns_idx.length) {         return ;/* do one - for testing */ } 
104     
105     var  core = imports.gi[ns_name];
106     var idx = { name: ns_name}; 
107     console.log("START:" + ns_name);
108    
109     var ns = NameSpace.ns(ns_name);
110     
111     // gir goes in top level...
112     if (File.exists(ns.gir_file)) {
113         File.copyFile(ns.gir_file, outputdir + '/'+ ns.gir_filename, Gio.FileCopyFlags.OVERWRITE);
114     }
115
116     
117     
118
119     langs.forEach(function(lang) {
120         ns['left_bar'] = lang.cls_ix_template.process(ns);
121         // namespace template
122         
123         if (!File.exists(outputdir + '/'+ lang.name)) {
124                File.mkdir(outputdir + '/'+ lang.name );
125         }
126       
127         
128         Gio.simple_write(outputdir + '/'+ lang.name+ '/' +ns_name +  '.html', lang.cls_template.process(ns));
129         
130         // left bar index of elements in namespace...
131         Gio.simple_write(outputdir + '/'+ lang.name + '/_ix_'+ ns_name +  '.shtml', lang.cls_ix_template.process(ns));
132             
133     });
134      
135     
136     var actions = {
137         'objects' : 'Class',
138         'interfaces' : 'Interface',
139         'structs' : 'Struct',
140         'unions' : 'Union',
141         'enums' : 'Enum'
142         
143     };
144     
145     for (var i in actions) {
146         // we flag GLib as a GObject lib...
147         idx[i]= ns_name == 'GLib' ? 1 : ns[i].length ;
148         
149         ns[i].forEach( function(n) {
150             
151             print('NameSpace.factory(' + actions[i] +','+ns_name+','+n);
152             var odata = XObject.extend(
153                 NameSpace.factory(actions[i], ns_name, n),
154                 { 'left_bar' :ns['left_bar'] }
155             );
156             
157             langs.forEach(function(lang) {
158                 Gio.simple_write(outputdir +  '/'+ lang.name + '/' + ns_name + '.' + n + '.html', 
159                     lang.cls_template.process(odata)
160                 )
161             });
162             console.log(ns_name + '.' +n);
163         }); 
164     }
165     ns_idx.push(idx);
166     
167 });
168
169 var refs = '';
170 var html_file_path = '';
171 var html = ''
172
173 // output cross reference data..
174 langs.forEach(function(lang) {
175     
176     for (var i in NameSpace.references) {
177         
178         html_file_path = [ outputdir, lang.name, i + '.html'].join('/');
179       
180         if (i == 'undefined') {
181           console.log("Undefined name space - ignored");
182           continue;
183         }
184         
185         if (!File.isFile(html_file_path)) {
186           console.log("No HTML file " + html_file_path + " to insert references into - ignored");
187           continue;
188         }
189         
190         refs = lang.reference_template.process(NameSpace.references[i]);
191
192           // HTML to put refs into
193         html =  File.read(html_file_path);
194
195           // do the replacement
196         html = html.replace(/\<!--references--\>/, refs);
197
198           // write back to file
199         Gio.simple_write(html_file_path, html);
200
201     }
202 });
203
204 // set up index and resources.
205 langs.forEach(function(lang) {
206     var ix_template = new Template({
207         templateFile : __script_path__ + '/templates/' + lang.name + '/index.html',
208         Link : Link, // lang specifc?
209     });
210     Gio.simple_write(outputdir + '/' + lang.name +  '/index.html', ix_template.process(ns_idx));
211     File.silentRecursiveCopy(__script_path__ + '/templates/resources/', 
212         outputdir + '/'  + lang.name , Gio.FileCopyFlags.OVERWRITE);
213 });