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