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