Updated TODO.
[mp_doccer.git] / mp_doccer
blob1cc321595ddd17cdae999dabcd72660290a7a660
1 #!/usr/bin/perl
4 # mp_doccer - Documentation generator
6 # Copyright (C) 2001/2008 Angel Ortega <angel@triptico.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # http://www.triptico.com/software/mp_doccer.html
25 use strict;
26 use warnings;
28 $main::VERSION = '1.2.1';
30 use Getopt::Long;
32 # output format
33 my $format = 'html';
35 # output file or directory
36 my $output = '';
38 # documentation title
39 my $title = 'API';
41 # man section
42 my $man_section = '3';
44 # function (and variable) documentation database
45 my @functions = ();
47 # function categories
48 my %categories = ();
50 # the style sheet
51 my $css = '';
53 # prefix for generated files
54 my $file_prefix = '';
56 # author's name and email
57 my $author = '';
59 # quiet flag
60 my $quiet = 0;
62 # show version
63 my $version = 0;
65 # show usage
66 my $usage = 0;
68 # parse options
69 if (!GetOptions('f|format=s' => \$format,
70 'o|output=s' => \$output,
71 'c|css=s' => \$css,
72 't|title=s' => \$title,
73 'v|version' => \$version,
74 'p|prefix=s' => \$file_prefix,
75 'm|man-section=s' => \$man_section,
76 'a|author=s' => \$author,
77 'q|quiet' => \$quiet,
78 'h|help' => \$usage)
79 or $usage) {
80 usage();
83 if ($version) {
84 print "$main::VERSION\n";
85 exit(0);
88 # list of source code files
89 my @sources = sort(@ARGV) or usage();
91 extract_doc(@sources);
93 # create
94 if ($format eq 'html') {
95 format_html();
97 elsif ($format eq 'man') {
98 format_man();
100 elsif ($format eq 'localhelp') {
101 format_sh();
103 elsif ($format eq 'html1') {
104 format_html_1();
106 else {
107 print "Invalid output format '$format'\n";
108 print "Valid ones are: html man localhelp html1\n";
112 # ###################################################################
115 sub extract_doc
116 # extract the documentation from the source code files
118 my (@sources) = @_;
119 my %func_idx;
121 foreach my $f (@sources) {
122 unless (open F, $f) {
123 warn "Can't open $_";
124 next;
127 # $f=$1 if $f =~ /\/([^\/]*)$/;
129 print("Processing $f...\n");
131 while (<F>) {
132 my ($fname, $bdesc, @arg, @argdesc, $desc,
133 $syn, $altsyn, $uniq, @category);
135 chop;
137 unless (/^\s*\/\*\*$/) {
138 next;
141 chop($_ = <F>) or last;
143 # extract function name and brief description
144 ($fname, $bdesc) = /([\w_\.]*) - (.*)/;
146 # possible arguments
147 for (;;) {
148 chop($_ = <F>) or goto eof;
150 unless (/^\s+\*\s+\@([^:]*):\s+(.*)/) {
151 last;
154 push(@arg, $1);
155 push(@argdesc, $2);
158 if (/^\s+\*\//) {
159 goto skipdesc;
162 # rest of lines until */ are the description
163 for (;;) {
164 chop($_ = <F>) or goto eof;
165 last if /^\s+\*\//;
167 # a line with only [text] is a category
168 if (/^\s+\*\s+\[(.*)\]$/) {
169 my $sec = $1;
171 my $s = $categories{$sec};
173 unless (grep /^$fname$/, @$s) {
174 push(@$s, $fname);
175 $categories{$sec} = $s;
178 push(@category, $sec);
180 next;
183 /^\s+\*\s*(.*)$/;
184 $desc .= $1 . "\n";
187 skipdesc:
189 # rest of info until a { or ; is the synopsis
190 for (;;) {
191 chop($_ = <F>) or goto eof;
193 if (/^\s*\/\*\*(.*)\*\//) {
194 $altsyn .= $1 . "\n";
196 elsif (/^([^{;]*)[{;]/) {
197 $syn .= $1 . "\n";
198 last;
200 elsif (/^\s\/\*\*$/) {
201 last;
203 else {
204 $syn .= $_ . "\n";
208 # fix synopsis to have a trailing ;
209 $syn =~ s/^(\s*)//;
210 $syn =~ s/(\s*)$//;
211 $syn .= ";";
213 # delete (posible) leading 'sub'
214 $syn =~ s/^\s*sub\s+//;
216 # calculate a unique name
217 # (to avoid collisions in file names)
218 if ($func_idx{$fname}) {
219 $uniq = $fname . $func_idx{$fname}++;
221 else {
222 $uniq = $fname;
223 $func_idx{$fname} = 1;
226 my $func = {};
228 # store
229 $func->{'file'} = $f;
230 $func->{'func'} = $fname;
231 $func->{'bdesc'} = $bdesc;
232 $func->{'desc'} = $desc;
233 $func->{'syn'} = $syn;
234 $func->{'uniq'} = $uniq;
236 if (@arg) {
237 $func->{'arg'} = \@arg;
240 if (@argdesc) {
241 $func->{'argdesc'} = \@argdesc;
244 if ($altsyn) {
245 $func->{'altsyn'} = $altsyn;
248 if (@category) {
249 $func->{'category'} = \@category;
252 push(@functions, $func);
255 eof:
257 close F;
260 # iterate now the functions, creating the 'prev' and 'next' fields
261 my $prev = undef;
262 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
263 if ($prev) {
264 $prev->{'next'} = $f->{'func'};
265 $f->{'prev'} = $prev->{'func'};
268 $prev = $f;
273 sub usage
275 print << "EOF";
276 mp_doccer $main::VERSION - C Source Code Documentation Generator
277 Copyright (C) 2001/2008 Angel Ortega <angel\@triptico.com>
278 This software is covered by the GPL license. NO WARRANTY.
280 Usage: mp_doccer [options] c_code_files...
282 Options:
284 -o|--output=dest Directory or file where the
285 documentation is generated.
286 -t|--title="title" Title for the documentation.
287 -c|--css="css URL" URL to a Cascade Style Sheet
288 to include in all HTML files.
289 -f|--format="format" Format for the generated
290 documentation.
291 Valid ones are:
292 html man localhelp html1
293 -p|--prefix="prefix" Prefix for the name of the
294 generated files. Main index
295 file will also have this name.
296 -a|--author="author" Sets author info (as name and email)
297 to be included in the documentation.
298 -m|--man-section="sect" Section number for the generated
299 man pages.
300 -v|--version Shows version.
301 -q|--quiet Suppress 'built with...' info.
302 -h|--help This help.
304 The mp_doccer Home Page:
305 http://www.triptico.com/software/mp_doccer.html
308 exit(0);
312 #######################################################
314 sub format_sh
315 # create a help shell script
317 my ($o, $h);
319 unless ($output) {
320 $output = 'localhelp.sh';
323 open F, ">$output" or die "Error: $!";
325 # build the header
327 print F "#!/bin/sh\n\n";
328 printf F "# Help program generated by mp_doccer $main::VERSION on %s\n",scalar(localtime());
329 print F "# mp_doccer is part of the Minimum Profit Text Editor\n";
330 print F "# http://www.triptico.com/software/mp.html\n\n";
332 print F "case \"\$1\" in\n";
334 for (my $n = 0; $n < scalar(@functions); $n++) {
335 my ($f,$syn);
337 $f = $functions[$n];
339 print F "$f->{'func'})\n";
341 print F "cat << EOF\n";
343 print F "$title\n\n";
345 print F "NAME\n\n";
346 print F "$f->{'func'} - $f->{'bdesc'}\n\n";
348 print F "SYNOPSIS\n\n";
350 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
351 $syn =~ s/\@([\w]+)/$1/g;
352 $syn =~ s/\%([\w]+)/$1/g;
354 chomp($syn);
355 print F "$syn\n\n";
357 if ($f->{'arg'}) {
358 my ($a, $d);
360 $a = $f->{'arg'};
361 $d = $f->{'argdesc'};
363 print F "ARGUMENTS\n\n";
365 for (my $n = 0; $n < scalar(@$a); $n++) {
366 print F "$$a[$n] - $$d[$n]\n";
369 print F "\n";
372 if ($f->{'desc'}) {
373 print F "DESCRIPTION\n\n";
375 my ($desc) = $f->{'desc'};
376 $desc =~ s/\@([\w]+)/$1/g;
377 $desc =~ s/\%([\w]+)/$1/g;
379 print F "$desc\n";
381 if ($f->{'category'}) {
382 my $s = $f->{'category'};
384 print F "CATEGORIES\n\n";
386 for (my $n = 0; $n < scalar(@$s); $n++) {
387 print F ", " if $n;
388 print F "$$s[$n]";
391 print F "\n";
395 if ($author) {
396 print F "AUTHOR\n\n";
397 print F "$author\n";
400 print F "EOF\n";
401 print F "\t;;\n";
404 print F "\"\")\n";
405 print F "\techo \"Usage: \$0 {keyword}\"\n";
406 print F "\t;;\n";
408 print F "*)\n";
409 print F "\techo \"No help for \$1\"\n";
410 print F "\texit 1";
411 print F "\t;;\n";
413 print F "esac\n";
414 print F "exit 0\n";
416 close F;
418 chmod 0755, $output;
422 sub format_man
423 # create man pages
425 my ($o, $h);
426 my ($pf);
428 unless ($output) {
429 $output = '.';
432 $output =~ s/\/$//;
434 unless (-d $output) {
435 print "$output must be a directory; aborting\n";
436 exit(1);
439 if ($file_prefix) {
440 $pf = $file_prefix . '_';
443 for(my $n = 0; $n < scalar(@functions); $n++) {
444 my ($f, $syn);
446 $f = $functions[$n];
448 # write the file
449 open F, ">$output/${pf}$f->{'func'}.$man_section" or die "Error: $!";
451 print F ".TH $f->{'func'} $man_section \"\" \"$title\"\n";
452 print F ".SH NAME\n";
453 print F "$f->{'func'} \\- $f->{'bdesc'}\n";
454 print F ".SH SYNOPSIS\n";
455 print F ".nf\n";
457 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
458 print F ".B $syn\n";
459 print F ".fi\n";
461 if ($f->{'arg'}) {
462 my ($a, $d);
464 $a = $f->{'arg'};
465 $d = $f->{'argdesc'};
467 print F ".SH ARGUMENTS\n";
469 for (my $n = 0; $n < scalar(@$a); $n++) {
470 print F ".B $$a[$n] \\-\n";
471 print F "$$d[$n]\n";
472 print F ".sp\n";
476 if ($f->{'desc'}) {
477 print F ".SH DESCRIPTION\n";
479 # take the description
480 my ($desc) = $f->{'desc'};
481 $desc =~ s/\@//g;
482 $desc =~ s/\%//g;
484 chomp($desc);
485 print F "$desc\n";
487 if ($f->{'category'}) {
488 my ($s) = $f->{'category'};
490 print F ".SH CATEGORIES\n";
492 for (my $n = 0; $n < scalar(@$s); $n++) {
493 print F ", " if $n;
494 print F "$$s[$n]";
497 print F "\n";
501 if ($author) {
502 print F ".SH AUTHOR\n";
503 print F "$author\n";
506 close F;
511 # HTML
513 sub html_header
515 my $title = shift;
516 my $ret = '';
518 $ret .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n";
519 $ret .= "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n";
520 $ret .= "<head><title>$title</title>\n";
521 $ret .= "<link rel = 'StyleSheet' href = '$css' type = 'text/css'>\n" if $css;
522 $ret .= "<meta name = 'generator' content = 'mp_doccer $main::VERSION'>\n";
523 $ret .= "<meta name = 'date' content = '" . scalar(localtime()) . "'>\n";
524 $ret .= "<meta name = 'author' content = '$author'>\n" if $author;
525 $ret .= "</head>\n<body>\n";
527 return $ret;
531 sub html_footer
533 my $ret = "<div class = 'footer'>\n";
535 if ($author) {
536 $ret .= "<span class = 'author'>$author</span>";
539 if (!$quiet) {
540 $ret .= " - <em class = 'built_with'>Built with <a href = 'http://www.triptico.com/software/mp_doccer.html'>mp_doccer $main::VERSION</a></em>";
543 $ret .= "\n</div>\n</body>\n</html>\n";
545 return $ret;
549 sub html_toc
551 my $func_link = shift;
552 my $ret = '';
554 $ret .= "<a name = '_TOP_'></a><h1>$title</h1>\n<div class = 'toc'>\n";
556 if (scalar(keys(%categories))) {
557 $ret .= "<h2>By Category</h2>\n";
559 foreach my $sn (sort keys %categories) {
560 $ret .= "<a name = '$sn'></a>\n";
561 $ret .= "<h3 class = 'category'>$sn</h3>\n";
563 $ret .= "<ul class = 'by_category'>\n";
565 $ret .= join('',
566 map { " <li><a href = '" . $func_link->($_) . "'>$_</a></li>\n" }
567 sort(@{$categories{$sn}})
570 $ret .= "</ul>\n";
574 $ret .= "<h2>By Source</h2>\n";
576 foreach my $s (@sources) {
577 my @f = grep { $_->{'file'} eq $s } @functions;
579 unless (@f) {
580 next;
583 $ret .= "<h3 class = 'source_file'>$s</h3>\n";
585 $ret .= "<ul class = 'by_source'>\n";
587 $ret .= join('',
588 map { " <li><a href = '" . $func_link->($_) . "'>$_</a></li>\n" }
589 sort(map { $_->{'func'} } @f)
592 $ret .= "</ul>\n";
595 $ret .= "<h2>Alphabetical</h2>\n";
596 $ret .= "<ul class = 'alphabetical'>\n";
598 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
599 $ret .= " <li><a href = '" . $func_link->($f->{'func'}) .
600 "'>$f->{'func'}</a> - $f->{'bdesc'}</li>\n";
603 $ret .= "</ul></div>\n";
605 return $ret;
609 sub html_func
611 my $f = shift;
612 my $ret = '';
613 my $syn;
615 $ret .= "\n<div class = 'func' style = 'margin-left: 1em;'>\n";
617 $ret .= "<h3>Name</h3>\n";
618 $ret .= "<strong class = 'funcname'>$f->{'func'}</strong> - $f->{'bdesc'}\n";
620 $ret .= "<h3>Synopsis</h3>\n";
622 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
624 # synopsis decoration
625 $syn =~ s/\b$f->{'func'}\b/\<strong class = 'funcname'>$f->{'func'}\<\/strong>/g;
627 $syn =~ s/@([\w]+)/<em class = 'funcarg'>$1<\/em>/g;
628 $syn =~ s/\%([\w]+)/<em class = 'funcret'>$1<\/em>/g;
630 if ($f->{'arg'}) {
631 foreach my $a (@{$f->{'arg'}}) {
632 $syn =~ s/\b$a\b/\<em class = 'funcarg'>$a\<\/em>/g;
636 $ret .= "<pre class = 'funcsyn'>\n$syn</pre>\n";
638 if ($f->{'arg'}) {
639 my @a = @{$f->{'arg'}};
640 my @d = @{$f->{'argdesc'}};
642 $ret .= "<h3>Arguments</h3>\n";
643 $ret .= "<dl class = 'arguments'>\n";
645 while (@a) {
646 $ret .= " <dt><em class = 'funcarg'>" . shift(@a) . "</em></dt>";
647 $ret .= "<dd>" . shift(@d) . "</dd>\n";
650 $ret .= "</dl>\n";
653 if ($f->{'desc'}) {
654 $ret .= "<h3>Description</h3>\n";
656 # take the description
657 my ($desc) = $f->{'desc'};
659 # decorate function names
660 $desc =~ s/([\w_]+\(\))/<code class = 'funcname'>$1<\/code>/g;
662 # decorate function arguments
663 $desc =~ s/@([\w_]+)/<em class = 'funcarg'>$1<\/em>/g;
665 # decorate return values
666 $desc =~ s/\%([\w_]+)/<em class = 'funcret'>$1<\/em>/g;
668 # replace blank lines
669 $desc =~ s/\n\n/\n<p>\n/gs;
671 $ret .= "<p class = 'description'>$desc</p>\n";
673 if ($f->{category}) {
674 $ret .= "<h3>Categories</h3>\n";
676 $ret .= "<ul class = 'categories'>\n" .
677 join('', map { " <li><a href = '#$_'>$_</a></li>\n" } @{$f->{'category'}}) .
678 "</ul>\n";
682 $ret .= "</div>\n";
686 sub format_html_1
687 # create 1 html page
689 my (%f);
691 if ($file_prefix) {
692 $file_prefix = '_' . $file_prefix;
695 # create the file
696 my $fn = $output . $file_prefix . '.html';
698 open F, ">$fn" or die "Error create $fn: $!";
700 print F html_header($title);
702 print F html_toc( sub { "#" . shift } );
704 # the functions themselves
705 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
706 # avoid duplicate function names
707 if ($f{$f->{'func'}}) {
708 next;
711 $f{$f->{'func'}}++;
713 print F "\n<div class = 'func_container'>\n";
714 print F "<a name = '$f->{'func'}'></a>\n";
715 print F "<h2 style = 'border-bottom: solid 2px;'>$f->{'func'}</h2>\n";
717 print F html_func($f);
719 print F "</div>\n";
722 print F html_footer();
724 close F;
728 sub format_html
729 # create multipage html documents
731 $output = "." unless $output;
732 $output =~ s/\/$//;
734 unless (-d $output) {
735 print "$output must be a directory; aborting\n";
736 exit(1);
739 my $pf = $file_prefix ? $file_prefix . '_' : '';
741 # create the table of contents
742 my $top = $file_prefix || 'index';
744 open TOC, ">$output/${top}.html"
745 or die "Error: $!";
747 print TOC html_header($title);
749 print TOC html_toc( sub { $pf . shift() . ".html" } );
751 print TOC html_footer();
753 close TOC;
755 # the functions themselves
756 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
757 # write the file
758 open F, ">$output/" . $pf . "$f->{'func'}.html"
759 or die "Error: $!";
761 print F html_header($f->{'func'});
763 print F "<div class = 'topnav'>\n";
765 print F ' ', $f->{'prev'} ? "<a href = '${pf}$f->{'prev'}.html'>Prev</a>" : "Prev",
766 " |\n",
767 " <a href = '${top}.html'><b>$title</b></a>",
768 " |\n",
769 ' ', $f->{'next'} ? "<a href = '${pf}$f->{'next'}.html'>Next</a>" : "Next",
770 "\n";
772 print F "</div>\n";
774 print F "<h2 style = 'border-bottom: solid 2px;'>$f->{'func'}</h2>\n";
776 print F html_func($f);
778 print F html_footer();
780 close F;