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