Updated README.
[mp_doccer.git] / mp_doccer
blobee676b9b72f2a9113186e929927b8974399076ba
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.2';
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 # documentation abstract
42 my $abstract = '';
44 # man section
45 my $man_section = '3';
47 # function (and variable) documentation database
48 my @functions = ();
50 # function categories
51 my %categories = ();
53 # the style sheet
54 my $css = '';
56 # prefix for generated files
57 my $file_prefix = '';
59 # author's name and email
60 my $author = '';
62 # quiet flag
63 my $quiet = 0;
65 # show version
66 my $version = 0;
68 # show usage
69 my $usage = 0;
71 # parse options
72 if (!GetOptions('f|format=s' => \$format,
73 'o|output=s' => \$output,
74 'c|css=s' => \$css,
75 't|title=s' => \$title,
76 'v|version' => \$version,
77 'p|prefix=s' => \$file_prefix,
78 'm|man-section=s' => \$man_section,
79 'a|author=s' => \$author,
80 'b|abstract=s' => \$abstract,
81 'q|quiet' => \$quiet,
82 'h|help' => \$usage)
83 or $usage) {
84 usage();
87 if ($version) {
88 print "$main::VERSION\n";
89 exit(0);
92 # list of source code files
93 my @sources = sort(@ARGV) or usage();
95 extract_doc(@sources);
97 # create
98 if ($format eq 'html') {
99 format_html();
101 elsif ($format eq 'man') {
102 format_man();
104 elsif ($format eq 'localhelp') {
105 format_sh();
107 elsif ($format eq 'html1') {
108 format_html_1();
110 elsif ($format eq 'grutatxt') {
111 format_grutatxt();
113 else {
114 print "Invalid output format '$format'\n";
115 print "Valid ones are: html man localhelp html1 grutatxt\n";
119 # ###################################################################
122 sub extract_doc
123 # extract the documentation from the source code files
125 my (@sources) = @_;
126 my %func_idx;
128 foreach my $f (@sources) {
129 unless (open F, $f) {
130 warn "Can't open $_";
131 next;
134 # $f=$1 if $f =~ /\/([^\/]*)$/;
136 print("Processing $f...\n");
138 while (<F>) {
139 my ($fname, $bdesc, @arg, @argdesc, $desc,
140 $syn, $altsyn, $uniq, @category);
142 chop;
144 unless (/^\s*\/\*\*$/) {
145 next;
148 chop($_ = <F>) or last;
150 # extract function name and brief description
151 ($fname, $bdesc) = /([\w_\.]*) - (.*)/;
153 # possible arguments
154 for (;;) {
155 chop($_ = <F>) or goto eof;
157 unless (/^\s+\*\s+\@([^:]*):\s+(.*)/) {
158 last;
161 push(@arg, $1);
162 push(@argdesc, $2);
165 if (/^\s+\*\//) {
166 goto skipdesc;
169 # rest of lines until */ are the description
170 for (;;) {
171 chop($_ = <F>) or goto eof;
172 last if /^\s+\*\//;
174 # a line with only [text] is a category
175 if (/^\s+\*\s+\[(.*)\]$/) {
176 my $sec = $1;
178 my $s = $categories{$sec};
180 unless (grep /^$fname$/, @$s) {
181 push(@$s, $fname);
182 $categories{$sec} = $s;
185 push(@category, $sec);
187 next;
190 /^\s+\*\s*(.*)$/;
191 $desc .= $1 . "\n";
194 skipdesc:
196 # rest of info until a { or ; is the synopsis
197 for (;;) {
198 chop($_ = <F>) or goto eof;
200 if (/^\s*\/\*\*(.*)\*\//) {
201 $altsyn .= $1 . "\n";
203 elsif (/^([^{;]*)[{;]/) {
204 $syn .= $1 . "\n";
205 last;
207 elsif (/^\s\/\*\*$/) {
208 last;
210 else {
211 $syn .= $_ . "\n";
215 # fix synopsis to have a trailing ;
216 $syn =~ s/^(\s*)//;
217 $syn =~ s/(\s*)$//;
218 $syn .= ";";
220 # delete (posible) leading 'sub'
221 $syn =~ s/^\s*sub\s+//;
223 # calculate a unique name
224 # (to avoid collisions in file names)
225 if ($func_idx{$fname}) {
226 $uniq = $fname . $func_idx{$fname}++;
228 else {
229 $uniq = $fname;
230 $func_idx{$fname} = 1;
233 my $func = {};
235 # store
236 $func->{'file'} = $f;
237 $func->{'func'} = $fname;
238 $func->{'bdesc'} = $bdesc;
239 $func->{'desc'} = $desc;
240 $func->{'syn'} = $syn;
241 $func->{'uniq'} = $uniq;
243 if (@arg) {
244 $func->{'arg'} = \@arg;
247 if (@argdesc) {
248 $func->{'argdesc'} = \@argdesc;
251 if ($altsyn) {
252 $func->{'altsyn'} = $altsyn;
255 if (@category) {
256 $func->{'category'} = \@category;
259 push(@functions, $func);
262 eof:
264 close F;
267 # iterate now the functions, creating the 'prev' and 'next' fields
268 my $prev = undef;
269 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
270 if ($prev) {
271 $prev->{'next'} = $f->{'func'};
272 $f->{'prev'} = $prev->{'func'};
275 $prev = $f;
280 sub usage
282 print << "EOF";
283 mp_doccer $main::VERSION - C Source Code Documentation Generator
284 Copyright (C) 2001/2008 Angel Ortega <angel\@triptico.com>
285 This software is covered by the GPL license. NO WARRANTY.
287 Usage: mp_doccer [options] c_code_files...
289 Options:
291 -o|--output=dest Directory or file where the
292 documentation is generated.
293 -t|--title="title" Title for the documentation.
294 -c|--css="css URL" URL to a Cascade Style Sheet
295 to include in all HTML files.
296 -f|--format="format" Format for the generated
297 documentation.
298 Valid ones are:
299 html man localhelp html1 grutatxt
300 -p|--prefix="prefix" Prefix for the name of the
301 generated files. Main index
302 file will also have this name.
303 -a|--author="author" Sets author info (as name and email)
304 to be included in the documentation.
305 -b|--abstract="text" Abstract for the documentation.
306 -m|--man-section="sect" Section number for the generated
307 man pages.
308 -v|--version Shows version.
309 -q|--quiet Suppress 'built with...' info.
310 -h|--help This help.
312 The mp_doccer Home Page:
313 http://triptico.com/software/mp_doccer.html
316 exit(0);
320 #######################################################
322 sub format_sh
323 # create a help shell script
325 my ($o, $h);
327 unless ($output) {
328 $output = 'localhelp.sh';
331 open F, ">$output" or die "Error: $!";
333 # build the header
335 print F "#!/bin/sh\n\n";
336 printf F "# Help program generated by mp_doccer $main::VERSION on %s\n",scalar(localtime());
337 print F "# mp_doccer is part of the Minimum Profit Text Editor\n";
338 print F "# http://www.triptico.com/software/mp.html\n\n";
340 print F "case \"\$1\" in\n";
342 for (my $n = 0; $n < scalar(@functions); $n++) {
343 my ($f,$syn);
345 $f = $functions[$n];
347 print F "$f->{'func'})\n";
349 print F "cat << EOF\n";
351 print F "$title\n\n";
353 print F "NAME\n\n";
354 print F "$f->{'func'} - $f->{'bdesc'}\n\n";
356 print F "SYNOPSIS\n\n";
358 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
359 $syn =~ s/\@([\w]+)/$1/g;
360 $syn =~ s/\%([\w]+)/$1/g;
362 chomp($syn);
363 print F "$syn\n\n";
365 if ($f->{'arg'}) {
366 my ($a, $d);
368 $a = $f->{'arg'};
369 $d = $f->{'argdesc'};
371 print F "ARGUMENTS\n\n";
373 for (my $n = 0; $n < scalar(@$a); $n++) {
374 print F "$$a[$n] - $$d[$n]\n";
377 print F "\n";
380 if ($f->{'desc'}) {
381 print F "DESCRIPTION\n\n";
383 my ($desc) = $f->{'desc'};
384 $desc =~ s/\@([\w]+)/$1/g;
385 $desc =~ s/\%([\w]+)/$1/g;
387 print F "$desc\n";
389 if ($f->{'category'}) {
390 my $s = $f->{'category'};
392 print F "CATEGORIES\n\n";
394 for (my $n = 0; $n < scalar(@$s); $n++) {
395 print F ", " if $n;
396 print F "$$s[$n]";
399 print F "\n";
403 if ($author) {
404 print F "AUTHOR\n\n";
405 print F "$author\n";
408 print F "EOF\n";
409 print F "\t;;\n";
412 print F "\"\")\n";
413 print F "\techo \"Usage: \$0 {keyword}\"\n";
414 print F "\t;;\n";
416 print F "*)\n";
417 print F "\techo \"No help for \$1\"\n";
418 print F "\texit 1";
419 print F "\t;;\n";
421 print F "esac\n";
422 print F "exit 0\n";
424 close F;
426 chmod 0755, $output;
430 sub format_man
431 # create man pages
433 my ($o, $h);
434 my ($pf);
436 unless ($output) {
437 $output = '.';
440 $output =~ s/\/$//;
442 unless (-d $output) {
443 print "$output must be a directory; aborting\n";
444 exit(1);
447 if ($file_prefix) {
448 $pf = $file_prefix . '_';
451 for(my $n = 0; $n < scalar(@functions); $n++) {
452 my ($f, $syn);
454 $f = $functions[$n];
456 # write the file
457 open F, ">$output/${pf}$f->{'func'}.$man_section" or die "Error: $!";
459 print F ".TH $f->{'func'} $man_section \"\" \"$title\"\n";
460 print F ".SH NAME\n";
461 print F "$f->{'func'} \\- $f->{'bdesc'}\n";
462 print F ".SH SYNOPSIS\n";
463 print F ".nf\n";
465 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
466 print F ".B $syn\n";
467 print F ".fi\n";
469 if ($f->{'arg'}) {
470 my ($a, $d);
472 $a = $f->{'arg'};
473 $d = $f->{'argdesc'};
475 print F ".SH ARGUMENTS\n";
477 for (my $n = 0; $n < scalar(@$a); $n++) {
478 print F ".B $$a[$n] \\-\n";
479 print F "$$d[$n]\n";
480 print F ".sp\n";
484 if ($f->{'desc'}) {
485 print F ".SH DESCRIPTION\n";
487 # take the description
488 my ($desc) = $f->{'desc'};
489 $desc =~ s/\@//g;
490 $desc =~ s/\%//g;
492 chomp($desc);
493 print F "$desc\n";
495 if ($f->{'category'}) {
496 my ($s) = $f->{'category'};
498 print F ".SH CATEGORIES\n";
500 for (my $n = 0; $n < scalar(@$s); $n++) {
501 print F ", " if $n;
502 print F "$$s[$n]";
505 print F "\n";
509 if ($author) {
510 print F ".SH AUTHOR\n";
511 print F "$author\n";
514 close F;
519 # HTML
521 sub html_header
523 my $title = shift;
524 my $ret = '';
526 $ret .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n";
527 $ret .= "\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n";
528 $ret .= "<head><title>$title</title>\n";
529 $ret .= "<link rel = 'StyleSheet' href = '$css' type = 'text/css'>\n" if $css;
530 $ret .= "<meta name = 'generator' content = 'mp_doccer $main::VERSION'>\n";
531 $ret .= "<meta name = 'date' content = '" . scalar(localtime()) . "'>\n";
532 $ret .= "<meta name = 'author' content = '$author'>\n" if $author;
533 $ret .= "</head>\n<body>\n";
535 return $ret;
539 sub html_footer
541 my $ret = "<div class = 'footer'>\n";
543 if ($author) {
544 $ret .= "<span class = 'author'>$author</span>";
547 if (!$quiet) {
548 $ret .= " - <em class = 'built_with'>Built with <a href = 'http://www.triptico.com/software/mp_doccer.html'>mp_doccer $main::VERSION</a></em>";
551 $ret .= "\n</div>\n</body>\n</html>\n";
553 return $ret;
557 sub html_toc
559 my $func_link = shift;
560 my $ret = '';
562 $ret .= "<a name = '_TOP_'></a><h1>$title</h1>\n";
564 $ret .= "<p>$abstract</p>\n" if $abstract;
566 $ret .= "<div class = 'toc'>\n";
568 if (scalar(keys(%categories))) {
569 $ret .= "<h2>By Category</h2>\n";
571 foreach my $sn (sort keys %categories) {
572 $ret .= "<a name = '$sn'></a>\n";
573 $ret .= "<h3 class = 'category'>$sn</h3>\n";
575 $ret .= "<ul class = 'by_category'>\n";
577 $ret .= join('',
578 map { " <li><a href = '" . $func_link->($_) . "'>$_</a></li>\n" }
579 sort(@{$categories{$sn}})
582 $ret .= "</ul>\n";
586 $ret .= "<h2>By Source</h2>\n";
588 foreach my $s (@sources) {
589 my @f = grep { $_->{'file'} eq $s } @functions;
591 unless (@f) {
592 next;
595 $ret .= "<h3 class = 'source_file'>$s</h3>\n";
597 $ret .= "<ul class = 'by_source'>\n";
599 $ret .= join('',
600 map { " <li><a href = '" . $func_link->($_) . "'>$_</a></li>\n" }
601 sort(map { $_->{'func'} } @f)
604 $ret .= "</ul>\n";
607 $ret .= "<h2>Alphabetical</h2>\n";
608 $ret .= "<ul class = 'alphabetical'>\n";
610 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
611 $ret .= " <li><a href = '" . $func_link->($f->{'func'}) .
612 "'>$f->{'func'}</a> - $f->{'bdesc'}</li>\n";
615 $ret .= "</ul></div>\n";
617 return $ret;
621 sub html_func
623 my $f = shift;
624 my $ret = '';
625 my $syn;
627 $ret .= "\n<div class = 'func' style = 'margin-left: 1em;'>\n";
629 $ret .= "<h3>Name</h3>\n";
630 $ret .= "<strong class = 'funcname'>$f->{'func'}</strong> - $f->{'bdesc'}\n";
632 $ret .= "<h3>Synopsis</h3>\n";
634 $syn = defined($f->{'altsyn'}) ? $f->{'altsyn'} : $f->{'syn'};
636 # synopsis decoration
637 $syn =~ s/\b$f->{'func'}\b/\<strong class = 'funcname'>$f->{'func'}\<\/strong>/g;
639 $syn =~ s/@([\w]+)/<em class = 'funcarg'>$1<\/em>/g;
640 $syn =~ s/\%([\w]+)/<em class = 'funcret'>$1<\/em>/g;
642 if ($f->{'arg'}) {
643 foreach my $a (@{$f->{'arg'}}) {
644 $syn =~ s/\b$a\b/\<em class = 'funcarg'>$a\<\/em>/g;
648 $ret .= "<pre class = 'funcsyn'>\n$syn</pre>\n";
650 if ($f->{'arg'}) {
651 my @a = @{$f->{'arg'}};
652 my @d = @{$f->{'argdesc'}};
654 $ret .= "<h3>Arguments</h3>\n";
655 $ret .= "<dl class = 'arguments'>\n";
657 while (@a) {
658 $ret .= " <dt><em class = 'funcarg'>" . shift(@a) . "</em></dt>";
659 $ret .= "<dd>" . shift(@d) . "</dd>\n";
662 $ret .= "</dl>\n";
665 if ($f->{'desc'}) {
666 $ret .= "<h3>Description</h3>\n";
668 # take the description
669 my ($desc) = $f->{'desc'};
671 # decorate function names
672 $desc =~ s/([\w_]+\(\))/<code class = 'funcname'>$1<\/code>/g;
674 # decorate function arguments
675 $desc =~ s/@([\w_]+)/<em class = 'funcarg'>$1<\/em>/g;
677 # decorate return values
678 $desc =~ s/\%([\w_]+)/<em class = 'funcret'>$1<\/em>/g;
680 # replace blank lines
681 $desc =~ s/\n\n/\n<p>\n/gs;
683 $ret .= "<p class = 'description'>$desc</p>\n";
685 if ($f->{category}) {
686 $ret .= "<h3>Categories</h3>\n";
688 $ret .= "<ul class = 'categories'>\n" .
689 join('', map { " <li><a href = '#$_'>$_</a></li>\n" } @{$f->{'category'}}) .
690 "</ul>\n";
694 $ret .= "</div>\n";
698 sub format_html_1
699 # create 1 html page
701 my (%f);
703 if ($file_prefix) {
704 $file_prefix = '_' . $file_prefix;
707 # create the file
708 my $fn = $output . $file_prefix . '.html';
710 open F, ">$fn" or die "Error create $fn: $!";
712 print F html_header($title);
714 print F html_toc( sub { "#" . shift } );
716 # the functions themselves
717 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
718 # avoid duplicate function names
719 if ($f{$f->{'func'}}) {
720 next;
723 $f{$f->{'func'}}++;
725 print F "\n<div class = 'func_container'>\n";
726 print F "<a name = '$f->{'func'}'></a>\n";
727 print F "<h2 style = 'border-bottom: solid 2px;'>$f->{'func'}</h2>\n";
729 print F html_func($f);
731 print F "</div>\n";
734 print F html_footer();
736 close F;
740 sub format_html
741 # create multipage html documents
743 $output = "." unless $output;
744 $output =~ s/\/$//;
746 unless (-d $output) {
747 print "$output must be a directory; aborting\n";
748 exit(1);
751 my $pf = $file_prefix ? $file_prefix . '_' : '';
753 # create the table of contents
754 my $top = $file_prefix || 'index';
756 open TOC, ">$output/${top}.html"
757 or die "Error: $!";
759 print TOC html_header($title);
761 print TOC html_toc( sub { $pf . shift() . ".html" } );
763 print TOC html_footer();
765 close TOC;
767 # the functions themselves
768 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
769 # write the file
770 open F, ">$output/" . $pf . "$f->{'func'}.html"
771 or die "Error: $!";
773 print F html_header($f->{'func'});
775 print F "<div class = 'topnav'>\n";
777 print F ' ', $f->{'prev'} ? "<a href = '${pf}$f->{'prev'}.html'>Prev</a>" : "Prev",
778 " |\n",
779 " <a href = '${top}.html'><b>$title</b></a>",
780 " |\n",
781 ' ', $f->{'next'} ? "<a href = '${pf}$f->{'next'}.html'>Next</a>" : "Next",
782 "\n";
784 print F "</div>\n";
786 print F "<h2 style = 'border-bottom: solid 2px;'>$f->{'func'}</h2>\n";
788 print F html_func($f);
790 print F html_footer();
792 close F;
797 sub _grutatxt_header
799 my $t = shift;
800 my $m = shift;
802 my $s = $t;
803 $s =~ s/./$m/g;
805 return $t . "\n" . $s . "\n\n";
809 sub _gl
811 my $s = shift;
813 $s = lc($s);
814 $s =~ s/\s/_/g;
816 return $s;
820 sub format_grutatxt
821 # create a grutatxt document
823 my (%f);
825 if ($file_prefix) {
826 $file_prefix = '_' . $file_prefix;
829 # create the file
830 my $fn = $output . $file_prefix . '.txt';
832 open F, ">$fn" or die "Error create $fn: $!";
834 print F _grutatxt_header($title, "=");
836 print F "$abstract\n\n" if $abstract;
838 if (scalar(keys(%categories))) {
840 print F _grutatxt_header('By Category', '-');
842 foreach my $sn (sort keys %categories) {
844 print F _grutatxt_header($sn, '~');
846 print F join("\n",
847 map { ' * ./#' . _gl($_) . ' (' . $_ . ')' }
848 sort(@{$categories{$sn}})
851 print F "\n\n";
855 print F _grutatxt_header('By Source', '-');
857 foreach my $s (@sources) {
858 my @f = grep { $_->{'file'} eq $s } @functions;
860 unless (@f) {
861 next;
864 print F _grutatxt_header($s, '~');
866 print F join("\n",
867 map { ' * ./#' . _gl($_) . ' (' . $_ . ')' }
868 sort(map { $_->{'func'} } @f)
871 print F "\n\n";
874 print F _grutatxt_header('Alphabetical', '-');
876 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
877 print F ' * ./#',
878 _gl($f->{'func'}),
879 ' (',
880 $f->{func},
881 ') - ',
882 $f->{bdesc},
883 "\n";
886 print F "\n\n";
888 # the functions themselves
889 foreach my $f (sort { $a->{'func'} cmp $b->{'func'} } @functions) {
890 # avoid duplicate function names
891 if ($f{$f->{'func'}}) {
892 next;
895 $f{$f->{'func'}}++;
897 print F _grutatxt_header($f->{func}, '-');
899 print F _grutatxt_header('Name', '~');
901 print F '*' . $f->{func} . '* - ' . $f->{bdesc} . "\n";
903 print F "\n";
905 print F _grutatxt_header('Synopsis', '~');
907 my $syn = $f->{'altsyn'} || (' ' . $f->{'syn'});
909 # strip arg and return value marks
910 $syn =~ s/[@%]([\w]+)/$1/g;
912 print F $syn . "\n\n";
914 if ($f->{'arg'}) {
915 my @a = @{$f->{'arg'}};
916 my @d = @{$f->{'argdesc'}};
918 print F _grutatxt_header('Arguments', '~');
920 while (@a) {
921 print F ' * ' . shift(@a) . ': ' . shift(@d) . "\n";
924 print F "\n";
927 if ($f->{'desc'}) {
928 print F _grutatxt_header('Description', '~');
930 # take the description
931 my $desc = $f->{'desc'};
933 # decorate function arguments
934 $desc =~ s/@([\w_]+)/_$1_/g;
936 # decorate return values
937 $desc =~ s/\%([\w_]+)/_$1_/g;
939 print F $desc, "\n";
941 if ($f->{category}) {
942 print F _grutatxt_header('Categories', '~');
944 print F join("\n",
945 map { ' * ./#' . _gl($_) . ' (' . $_ . ')' }
946 @{$f->{'category'}});
948 print F "\n";
952 print F "\n";
955 if ($author) {
956 print F "----\n$author ";
959 if (!$quiet) {
960 print F "- Built with http://triptico.com/software/mp_doccer.html (mp_doccer $main::VERSION)";
963 print F "\n";
964 close F;