#!/usr/bin/perl -w # Tries to get what a user would want to know from scripts in standard input or # @ARGV and send it to stdout. # Used by http://www.cv.nrao.edu/~rreid/software/sd2h # # Changes: # 2009/12/09: Options! Most importantly -h to HTMLize the output. Be sure to # get a copy of the corresponding CSS, # http://www.cv.nrao.edu/~rreid/software/style.css # -d: Turn on debugging. use strict; my $indent = ' '; my $dohtml = 0; my $debug = 0; use Getopt::Long; GetOptions('html' => \$dohtml, 'debug' => \$debug); sub ehprint { my $line = $_[0]; if ($dohtml) { $line =~ s//>/g; } print $line; } my $nopendiv = 0; # Counts open
s for debugging. sub enddiv { if ($dohtml) { print "
\n"; } --$nopendiv; } my $chopspace = ''; my $infirstcomment = 1; my $alreadyinfirstdocstr = 0; my $alreadyinfirstcomment = 0; my $ispython = 0; my $firstfuncline = 0; my $infunccomment = 0; my $inFuncSig = 0; while(<>){ next if /^#\s*-\*-/; # Skip emacs mode specifiers. if (/^#!.*python/) { $ispython = 1; } elsif($infirstcomment && /^"""(.*)/ && !$alreadyinfirstdocstr){ my $firstline = $1; chomp $firstline; if($firstline =~ /"""/){ $firstline =~ s/"""//; $infirstcomment = 0; } else{ $alreadyinfirstdocstr = 1; } if ($dohtml) { print '
'; ++$nopendiv; if (length($firstline) > 0) { ehprint($firstline); print "\n" if $infirstcomment; } enddiv() if !$infirstcomment; } else { print "$firstline\n"; } $infirstcomment = 0; } elsif($infirstcomment && /^#+\s*([^!].*)/ && !$alreadyinfirstcomment){ my $firstline = $1; if (length($firstline) > 0) { $alreadyinfirstcomment = 1; if ($dohtml) { print '
'; ++$nopendiv; } ehprint("$firstline\n"); } } elsif($alreadyinfirstdocstr && /(.*?)"""/){ my $lastline = $1; ehprint("$lastline\n") if length($lastline) > 0; enddiv(); print "\n"; $alreadyinfirstdocstr = 0; $infirstcomment = 0; } elsif($alreadyinfirstcomment && /^(#+\s*)?$/){ enddiv(); print "\n"; $alreadyinfirstcomment = 0; $infirstcomment = 0; } elsif($alreadyinfirstcomment && /^#+\s*(.+)$/){ ehprint("$1\n"); } elsif($alreadyinfirstdocstr){ my $line = $_; chomp $line; ehprint($line . "\n"); } elsif(/^\s*\w/ && !$infunccomment){ if ($infirstcomment) { $infirstcomment = 0; if ($alreadyinfirstcomment) { $alreadyinfirstcomment = 0; enddiv(); } } if (/^(class\s+[^_].*)/) { # Start of public non-nested class my $class = $1; if ($class =~ /:/) { # End of class sig. $inFuncSig = 0; $firstfuncline = 1; # Start looking for docstring. $class =~ s/://; } else { $inFuncSig = 1; # Sig continues on next line. } if ($dohtml) { print "
"; ++$nopendiv; ehprint("$class"); enddiv() if !$inFuncSig; } else { print "$indent$class\n"; } } elsif (/^def ([^_].*)/) { # Start of public non-nested func. my $func = $1; if ($func =~ /\):/) { # End of func sig, a bit stricter. $inFuncSig = 0; $firstfuncline = 1; # Start looking for docstring. $func =~ s/://; } else { $inFuncSig = 1; # Sig continues on next line. } if ($dohtml) { print "
"; ++$nopendiv; ehprint("$func"); enddiv() if !$inFuncSig; } else { print "$indent$func\n"; } } elsif (/^\s+def __call__/){ $firstfuncline = 1; } elsif(/^\s+(def|class) _/){ $firstfuncline = 0; $inFuncSig = 0; } elsif($inFuncSig) { my $line = $_; chomp $line; if ($line =~ /\):/) { $inFuncSig = 0; $firstfuncline = 1; $line =~ s/://; } if ($dohtml) { ehprint("$line\n"); enddiv() if !$inFuncSig; } else { print "$indent$line\n"; } } } elsif ($firstfuncline) { if (/^(\s+)"""(.*)/) { # 3 is for the """. $chopspace = ' ' x length($1); my $comment = $2; $infunccomment = 1; if ($comment =~ /"""/) { $comment =~ s/"""//; $infunccomment = 0; } if ($dohtml) { print "
"; ++$nopendiv; ehprint($comment); if (length($comment) > 0) { $chopspace .= ' '; # Everything should be indented by the """. print "\n" if $infunccomment; } enddiv() if !$infunccomment; } else { print "$indent$indent$comment\n"; } } $firstfuncline = 0; } elsif ($infunccomment) { my $comment = $_; chomp $comment; $comment =~ s/^$chopspace//; if ($comment =~ /"""/) { $comment =~ s/"""//; $infunccomment = 0; } if ($dohtml) { ehprint($comment); if (!$infunccomment) { enddiv(); } else { print "\n"; } } else { print "$indent$indent$comment\n"; } } } if ($debug and $nopendiv != 0) { print "

There are $nopendiv open <div>s!

\n"; }