Debian Codegraph @

rhino/examples/jsdoc.js

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /**
8 * Process a JavaScript source file and process special comments
9 * to produce an HTML file of documentation, similar to javadoc.
10 * @author Norris Boyd
11 * @see rhinotip.jar
12 * @lastmodified xx
13 * @version 1.2 Roland Pennings: Allow multiple files for a function.
14 * @version 1.3 Roland Pennings: Removes ../.. from the input directory name
15 */
16 defineClass("File")
17
18 var functionDocArray = [];
19 var inputDirName = "";
20 var indexFileArray = [];
21 var indexFile = "";
22 var indexFileName = "index_files";
23 var indexFunctionArray = [];
24 var indexFunction = "";
25 var indexFunctionName = "index_functions";
26 var FileList = [];
27 var DirList = [];
28 var outputdir = null;
29 var debug = 0;
30
31
32
33 /**
34 * Process JavaScript source file <code>f</code>, writing jsdoc to
35 * file <code>out</code>.
36 * @param f input file
37 * @param fname name of the input file (without the path)
38 * @param inputdir directory of the input file
39 * @param out output file
40 */
41 function processFile(f, fname, inputdir, out) {
42 var s;
43 var firstLine = true;
44 indexFileArray[fname] = "";
45
46 // write the header of the output file
47 out.writeLine('<HTML><HEADER><TITLE>' + fname + '</TITLE><BODY>');
48 if (inputdir != null) {
49 outstr = '<a name=\"_top_\"></a><pre><a href=\"' + indexFile + '\">Index Files</a> ';
50 outstr += '<a href=\"' + indexFunction + '\">Index Functions</a></pre><hr>';
51 out.writeLine(outstr);
52 }
53
54 // process the input file
55 var comment = "";
56 while ((s = f.readLine()) != null) {
57 var m = s.match(/\/\*\*(.*)/);
58 if (m != null) {
59 // Found a comment start.
60 s = "*" + m[1];
61 do {
62 m = s.match(/(.*)\*\//);
63 if (m != null) {
64 // Found end of comment.
65 comment += m[1];
66 break;
67 }
68 // Strip leading whitespace and "*".
69 comment += s.replace(/^\s*\*/, "");
70 s = f.readLine();
71 } while (s != null);
72
73 if (debug)
74 print("Found comment " + comment);
75
76 if (firstLine) {
77 // We have a comment for the whole file.
78 out.writeLine('<H1>File ' + fname + '</H1>');
79 out.writeLine(processComment(comment,firstLine,fname));
80 out.writeLine('<HR>');
81 firstLine = false;
82 comment = "";
83 continue;
84 }
85 }
86 // match the beginning of the function
87 // NB we also match functions without a comment!
88 // if we have two comments one after another only the last one will be taken
89 m = s.match(/^\s*function\s+((\w+)|(\w+)(\s+))\(([^)]*)\)/);
90 if (m != null)
91 {
92 // Found a function start
93 var htmlText = processFunction(m[1], m[5], comment); // sjm changed from 2nd to 5th arg
94
95 // Save the text in a global variable, so we
96 // can write out a table of contents first.
97 functionDocArray[functionDocArray.length] = {name:m[1], text:htmlText};
98
99 // Store the function also in the indexFunctionArray
100 // so we can have a separate file with the function table of contents
101 if (indexFunctionArray[m[1]]) {
102 // print("ERROR: function: " + m[1] + " is defined more than once!");
103 // Allow multiple files for a function
104 with (indexFunctionArray[m[1]]) {
105 filename = filename + "|" + fname;
106 // print("filename = " + filename);
107 }
108 }
109 else {
110 indexFunctionArray[m[1]] = {filename:fname};
111 }
112 //reset comment
113 comment = "";
114 }
115 // match a method being bound to a prototype
116 m = s.match(/^\s*(\w*)\.prototype\.(\w*)\s*=\s*function\s*\(([^)]*)\)/);
117 if (m != null)
118 {
119 // Found a method being bound to a prototype.
120 var htmlText = processPrototypeMethod(m[1], m[2], m[3], comment);
121
122 // Save the text in a global variable, so we
123 // can write out a table of contents first.
124 functionDocArray[functionDocArray.length] = {name:m[1]+".prototype."+m[2], text:htmlText};
125
126 // Store the function also in the indexFunctionArray
127 // so we can have a separate file with the function table of contents
128 if (indexFunctionArray[m[1]]) {
129 // print("ERROR: function: " + m[1] + " is defined more than once!");
130 // Allow multiple files for a function
131 with (indexFunctionArray[m[1]]) {
132 filename = filename + "|" + fname;
133 // print("filename = " + filename);
134 }
135 }
136 else {
137 indexFunctionArray[m[1]] = {filename:fname};
138 }
139 //reset comment
140 comment = "";
141 }
142
143
144 firstLine = false;
145 }
146
147 // Write table of contents.
148 for (var i=0; i < functionDocArray.length; i++) {
149 with (functionDocArray[i]) {
150 out.writeLine('function <A HREF=#' + name +
151 '>' + name + '</A><BR>');
152 }
153 }
154 out.writeLine('<HR>');
155
156 // Now write the saved function documentation.
157 for (i=0; i < functionDocArray.length; i++) {
158 with (functionDocArray[i]) {
159 out.writeLine('<A NAME=' + name + '>');
160 out.writeLine(text);
161 }
162 }
163 out.writeLine('</BODY></HTML>');
164
165 // Now clean up the doc array
166 functionDocArray = [];
167 }
168
169 /**
170 * Process function and associated comment.
171 * @param name the name of the function
172 * @param args the args of the function as a single string
173 * @param comment the text of the comment
174 * @return a string for the HTML text of the documentation
175 */
176 function processFunction(name, args, comment) {
177 if (debug)
178 print("Processing " + name + " " + args + " " + comment);
179 return "<H2>Function " + name + "</H2>" +
180 "<PRE>" +
181 "function " + name + "(" + args + ")" +
182 "</PRE>" +
183 processComment(comment,0,name) +
184 "<P><BR><BR>";
185 }
186
187 /**
188 * Process a method being bound to a prototype.
189 * @param proto the name of the prototype
190 * @param name the name of the function
191 * @param args the args of the function as a single string
192 * @param comment the text of the comment
193 * @return a string for the HTML text of the documentation
194 */
195 function processPrototypeMethod(proto, name, args, comment) {
196 if (debug)
197 print("Processing " + proto + ".prototype." + name + " " + args + " " + comment);
198 return "<H2> Method " + proto + ".prototype." + name + "</H2>" +
199 "<PRE>" +
200 proto + ".prototype." + name + " = function(" + args + ")" +
201 "</PRE>" +
202 processComment(comment,0,name) +
203 "<P><BR><BR>";
204 }
205
206
207 /**
208 * Process comment.
209 * @param comment the text of the comment
210 * @param firstLine shows if comment is at the beginning of the file
211 * @param fname name of the file (without path)
212 * @return a string for the HTML text of the documentation
213 */
214 function processComment(comment,firstLine,fname) {
215 var tags = {};
216 // Use the "lambda" form of regular expression replace,
217 // where the replacement object is a function rather
218 // than a string. The function is called with the
219 // matched text and any parenthetical matches as
220 // arguments, and the result of the function used as the
221 // replacement text.
222 // Here we use the function to build up the "tags" object,
223 // which has a property for each "@" tag that is the name
224 // of the tag, and whose value is an array of the
225 // text following that tag.
226 comment = comment.replace(/@(\w+)\s+([^@]*)/g,
227 function (s, name, text) {
228 var a = tags[name] || [];
229 a.push(text);
230 tags[name] = a;
231 return "";
232 });
233
234 // if we have a comment at the beginning of a file
235 // store the comment for the index file
236 if (firstLine) {
237 indexFileArray[fname] = comment;
238 }
239
240 var out = comment + '<P>';
241 if (tags["param"]) {
242 // Create a table of parameters and their descriptions.
243 var array = tags["param"];
244 var params = "";
245 for (var i=0; i < array.length; i++) {
246 var m = array[i].match(/(\w+)\s+(.*)/);
247 params += '<TR><TD><I>'+m[1]+'</I></TD>' +
248 '<TD>'+m[2]+'</TD></TR>';
249 }
250 out += '<TABLE WIDTH="90%" BORDER=1>';
251 out += '<TR BGCOLOR=0xdddddddd>';
252 out += '<TD><B>Parameter</B></TD>';
253 out += '<TD><B>Description</B></TD></TR>';
254 out += params;
255 out += '</TABLE><P>';
256 }
257 if (tags["return"]) {
258 out += "<DT><B>Returns:</B><DD>";
259 out += tags["return"][0] + "</DL><P>";
260 }
261 if (tags["author"]) {
262 // List the authors together, separated by commas.
263 out += '<DT><B>Author:</B><DD>';
264 var array = tags["author"];
265 for (var i=0; i < array.length; i++) {
266 out += array[i];
267 if (i+1 < array.length)
268 out += ", ";
269 }
270 out += '</DL><P>';
271 }
272 if (tags["version"]) {
273 // Show the version.
274 out += '<DT><B>Version:</B><DD>';
275 var array = tags["version"];
276 for (var i=0; i < array.length; i++) {
277 out += array[i];
278 if (i+1 < array.length)
279 out += "<BR><DD>";
280 }
281 out += '</DL><P>';
282 }
283 if (tags["see"]) {
284 // List the see modules together, separated by <BR>.
285 out += '<DT><B>Dependencies:</B><DD>';
286 var array = tags["see"];
287 for (var i=0; i < array.length; i++) {
288 out += array[i];
289 if (i+1 < array.length)
290 out += "<BR><DD>";
291 }
292 out += '</DL><P>';
293 }
294 if (tags["lastmodified"]) {
295 // Shows a last modified description with client-side js.
296 out += '<DT><B>Last modified:</B><DD>';
297 out += '<script><!--\n';
298 out += 'document.writeln(document.lastModified);\n';
299 out += '// ---></script>\n';
300 out += '</DL><P>';
301 }
302
303 // additional tags can be added here (i.e., "if (tags["see"])...")
304 return out;
305 }
306
307 /**
308 * Create an html output file
309 * @param outputdir directory to put the file
310 * @param htmlfile name of the file
311 */
312 function CreateOutputFile(outputdir,htmlfile)
313 {
314 if (outputdir==null)
315 {
316 var outname = htmlfile;
317 }
318 else
319 {
320 var separator = Packages.java.io.File.separator;
321 var outname = outputdir + separator + htmlfile.substring(htmlfile.lastIndexOf(separator),htmlfile.length);
322 }
323 print("output file: " + outname);
324 return new File(outname);
325 }
326
327 /**
328 * Process a javascript file. Puts the generated HTML file in the outdir
329 * @param filename name of the javascript file
330 * @inputdir input directory of the file (default null)
331 */
332 function processJSFile(filename,inputdir)
333 {
334 if (debug) print("filename = " + filename + " inputdir = " + inputdir);
335
336 if (!filename.match(/\.js$/)) {
337 print("Expected filename to end in '.js'; had instead " +
338 filename + ". I don't treat the file.");
339 } else {
340 if (inputdir==null)
341 {
342 var inname = filename;
343 }
344 else
345 {
346 var separator = Packages.java.io.File.separator;
347 var inname = inputdir + separator + filename;
348 }
349 print("Processing file " + inname);
350
351 var f = new File(inname);
352
353 // create the output file
354 var htmlfile = filename.replace(/\.js$/, ".html");
355
356 var out = CreateOutputFile(outputdir,htmlfile);
357
358 processFile(f, filename, inputdir, out);
359 out.close();
360 }
361 }
362
363 /**
364 * Generate index files containing links to the processed javascript files
365 * and the generated functions
366 */
367 function GenerateIndex(dirname)
368 {
369 // construct the files index file
370 var out = CreateOutputFile(outputdir,indexFile);
371
372 // write the beginning of the file
373 out.writeLine('<HTML><HEADER><TITLE>File Index - directory: ' + dirname + '</TITLE><BODY>');
374 out.writeLine('<H1>File Index - directory: ' + dirname + '</H1>\n');
375 out.writeLine('<TABLE WIDTH="90%" BORDER=1>');
376 out.writeLine('<TR BGCOLOR=0xdddddddd>');
377 out.writeLine('<TD><B>File</B></TD>');
378 out.writeLine('<TD><B>Description</B></TD></TR>');
379
380 var separator = Packages.java.io.File.separator;
381
382 // sort the index file array
383 var SortedFileArray = [];
384 for (var fname in indexFileArray)
385 SortedFileArray.push(fname);
386 SortedFileArray.sort();
387
388 for (var i=0; i < SortedFileArray.length; i++) {
389 var fname = SortedFileArray[i];
390 var htmlfile = fname.replace(/\.js$/, ".html");
391 out.writeLine('<TR><TD><A HREF=\"' + htmlfile + '\">' + fname + '</A></TD></TD><TD>');
392 if (indexFileArray[fname])
393 out.writeLine(indexFileArray[fname]);
394 else
395 out.writeLine('No comments');
396 out.writeLine('</TD></TR>\n');
397 }
398 out.writeLine('</TABLE></BODY></HTML>');
399 out.close();
400
401 // construct the functions index file
402 var out = CreateOutputFile(outputdir,indexFunction);
403
404 // write the beginning of the file
405 out.writeLine('<HTML><HEADER><TITLE>Function Index - directory: ' + dirname + '</TITLE><BODY>');
406 out.writeLine('<H1>Function Index - directory: ' + dirname + '</H1>\n');
407 out.writeLine('<TABLE WIDTH="90%" BORDER=1>');
408 out.writeLine('<TR BGCOLOR=0xdddddddd>');
409 out.writeLine('<TD><B>Function</B></TD>');
410 out.writeLine('<TD><B>Files</B></TD></TR>');
411
412 // sort the function array
413 var SortedFunctionArray = [];
414 for (var functionname in indexFunctionArray)
415 SortedFunctionArray.push(functionname);
416 SortedFunctionArray.sort();
417
418 for (var j=0; j < SortedFunctionArray.length; j++) {
419 var funcname = SortedFunctionArray[j];
420 with (indexFunctionArray[funcname]) {
421 var outstr = '<TR><TD>' + funcname + '</TD><TD>';
422 var filelst = filename.split("|");
423 for (var i in filelst) {
424 var htmlfile = filelst[i].replace(/\.js$/, ".html");
425 outstr += '<A HREF=\"' + htmlfile + '#' + funcname + '\">' + filelst[i] + '</A>&nbsp;';
426 }
427 outstr += '</TD></TR>';
428 out.writeLine(outstr);
429 }
430 }
431 out.writeLine('</TABLE></BODY></HTML>');
432 out.close();
433 }
434
435
436 /**
437 * prints the options for JSDoc
438 */
439 function PrintOptions()
440 {
441 print("You can use the following options:\n");
442 print("-d: specify an output directory for the generated html files\n");
443 print("-i: processes all files in an input directory (you can specify several directories)\n");
444 quit();
445 }
446
447
448 // Main Script
449 // first read the arguments
450 if (! arguments)
451 PrintOptions();
452
453 for (var i=0; i < arguments.length; i++) {
454 if (debug) print("argument: + \'" + arguments[i] + "\'");
455 if (arguments[i].match(/^\-/)) {
456 if (String(arguments[i])=="-d"){
457 // output directory for the generated html files
458
459 outputdir = String(arguments[i+1]);
460 if (debug) print("outputdir: + \'" + outputdir + "\'");
461
462 i++;
463 }
464 else if (String(arguments[i])=="-i"){
465 // process all files in an input directory
466
467 DirList.push(String(arguments[i+1]));
468 if (debug) print("inputdir: + \'" + arguments[i+1] + "\'");
469 i++;
470 }
471 else {
472 print("Unknown option: " + arguments[i] + "\n");
473 PrintOptions();
474 }
475 }
476 else
477 {
478 // we have a single file
479 if (debug) print("file: + \'" + arguments[i] + "\'");
480
481 FileList.push(String(arguments[i]));
482 }
483 }
484
485 // first handle the single files
486 for (var i in FileList)
487 processJSFile(FileList[i],null);
488
489 // then handle the input directories
490 for (var j in DirList) {
491 var inputdir = String(DirList[j]);
492
493 print("Process input directory: " + inputdir);
494
495 // clean up index arrays
496 var indexFileArray = [];
497 var indexFunctionArray = [];
498
499 // for the directory name get rid of ../../ or ..\..\
500 inputDirName = inputdir.replace(/\.\.\/|\.\.\\/g,"");
501
502 indexFile = indexFileName + "_" + inputDirName + ".html";
503 indexFunction = indexFunctionName + "_" + inputDirName + ".html";
504
505 print("indexFile = " + indexFile);
506 print("indexFunction = " + indexFunction);
507
508 // read the files in the directory
509 var DirFile = new java.io.File(inputdir);
510 var lst = DirFile.list();
511 var separator = Packages.java.io.File.separator;
512
513 for (var i=0; i < lst.length; i++)
514 {
515 processJSFile(String(lst[i]),inputdir);
516 }
517
518 // generate the index files for the input directory
519 GenerateIndex(inputDirName);
520 }
521
522
523