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
61
62
63 // which languages do we want to output for.
64 langs=[];
65 File.list(__script_path__ + '/templates/').forEach(function(f) {
66     if (!File.isDirectory(__script_path__ + '/templates/' + f)) {
67         return;
68     }
69     if (f == 'resources') {
70         return;
71     }
72     langs.push({
73         name : f,
74         cls_template       : new Template( {
75             templateFile : __script_path__ + '/templates/' + f + '/class.html',
76             Link : Link // links might be specific to languages..
77         }),
78         cls_ix_template    : new Template( {
79             templateFile : __script_path__ + '/templates/' + f + '/class_ix.html',
80             Link : Link // links might be specific to languages..
81         }),
82         reference_template : new Template({
83             templateFile : __script_path__ + '/templates/' + f + '/references.html',
84             Link : Link // links might be specific to languages..
85         }),
86     });
87 });
88
89
90 /*
91 var cls_template = new Template(__script_path__ + '/templates/class.html');
92 var cls_ix_template = new Template(__script_path__ + '/templates/class_ix.html');
93 var reference_template = new Template(__script_path__ + '/templates/references.html');
94 */
95
96 print("Looping throught namespaces");
97 var ns_idx = [];
98 ns_list.forEach(function(ns_name) 
99 {
100     
101     //if (ns_idx.length) {         return ;/* do one - for testing */ } 
102     
103     var  core = imports.gi[ns_name];
104     var idx = { name: ns_name}; 
105     console.log("START:" + ns_name);
106    
107     var ns = NameSpace.ns(ns_name);
108     
109     // gir goes in top level...
110     if (File.exists(ns.gir_file)) {
111         File.copyFile(ns.gir_file, outputdir + '/'+ ns.gir_filename, Gio.FileCopyFlags.OVERWRITE);
112     }
113     
114     
115     
116
117     langs.forEach(function(lang) {
118         ns['left_bar'] = lang.cls_ix_template.process(ns);
119         // namespace template
120         Gio.simple_write(outputdir + '/'+ lang.name+ '/' +ns_name +  '.html', lang.cls_template.process(ns));
121         
122         // left bar index of elements in namespace...
123         Gio.simple_write(outputdir + '/'+ lang.name + '/_ix_'+ ns_name +  '.shtml', lang.cls_ix_template.process(ns));
124             
125     });
126      
127     
128     var actions = {
129         'objects' : 'Class',
130         'interfaces' : 'Interface',
131         'structs' : 'Struct',
132         'unions' : 'Union',
133         'enums' : 'Enum'
134         
135     };
136     
137     for (var i in actions) {
138         // we flag GLib as a GObject lib...
139         idx[i]= ns_name == 'GLib' ? 1 : ns[i].length ;
140         
141         ns[i].forEach( function(n) {
142             
143             print('NameSpace.factory(' + actions[i] +','+ns_name+','+n);
144             var odata = XObject.extend(
145                 NameSpace.factory(actions[i], ns_name, n),
146                 { 'left_bar' :ns['left_bar'] }
147             );
148             
149             langs.forEach(function(lang) {
150                 Gio.simple_write(outputdir +  '/'+ lang.name + '/' + ns_name + '.' + n + '.html', 
151                     lang.cls_template.process(odata)
152                 )
153             });
154             console.log(ns_name + '.' +n);
155         }); 
156     }
157     ns_idx.push(idx);
158     
159 });
160
161 var refs = '';
162 var html_file_path = '';
163 var html = ''
164
165 // output cross reference data..
166 langs.forEach(function(lang) {
167     
168     for (var i in NameSpace.references) {
169         
170         html_file_path = [ outputdir, lang.name, i + '.html'].join('/');
171       
172         if (i == 'undefined') {
173           console.log("Undefined name space - ignored");
174           continue;
175         }
176         
177         if (!File.isFile(html_file_path)) {
178           console.log("No HTML file " + html_file_path + " to insert references into - ignored");
179           continue;
180         }
181         
182         refs = lang.reference_template.process(NameSpace.references[i]);
183
184           // HTML to put refs into
185         html =  File.read(html_file_path);
186
187           // do the replacement
188         html = html.replace(/\<!--references--\>/, refs);
189
190           // write back to file
191         Gio.simple_write(html_file_path, html);
192
193     }
194 });
195
196 // set up index and resources.
197 langs.forEach(function(lang) {
198     var ix_template = new Template({
199         templateFile : __script_path__ + '/templates/' + lang.name + '/index.html',
200         Link : Link, // lang specifc?
201     });
202     Gio.simple_write(outputdir + '/' + lang.name +  '/index.html', ix_template.process(ns_idx));
203     File.silentRecursiveCopy(__script_path__ + '/templates/resources/', 
204         outputdir + '/'  + lang.name , Gio.FileCopyFlags.OVERWRITE);
205 });