#!/usr/bin/perl -w # Outputs the first comment from standard input. # See sd2h. use strict; my $infirstcomment = 1; my $reallyinfirstcomment = 0; my $ispython = 0; my $firstfuncline = 0; my $infunccomment = 0; while(<>){ next if /^#\s*-\*-/; # Skip emacs mode specifiers. if (/^#!.*python/) { $ispython = 1; } elsif($infirstcomment && /^"""(.*)/ && !$reallyinfirstcomment){ my $firstline = $1; if($firstline =~ /"""/){ $firstline =~ s/"""//; $infirstcomment = 0; } else{ $reallyinfirstcomment = 1; } chomp $firstline; print "$firstline\n" if length($firstline) > 0; } elsif($reallyinfirstcomment && /(.*?)"""/){ my $lastline = $1; print "$lastline\n" if length($lastline) > 0; print "\n"; $reallyinfirstcomment = 0; $infirstcomment = 0; } elsif($reallyinfirstcomment){ print; } elsif($infirstcomment && /^# (.+)/){ print "$1\n"; } elsif($infirstcomment && /^#\s*$/){ print "\n"; } elsif(/^\s*\w/ && !$infunccomment){ $infirstcomment = 0; #if ($ispython && /^(def|class) (.+)/) { if (/^\s*(def|class) ([^_].*)/) { my $func = $2; chomp $func; $func =~ s/://; print "\t$func"; $firstfuncline = 1; } elsif(/^\s+def __call__/){ $firstfuncline = 1; } elsif(/^\s+(def|class) _/){ $firstfuncline = 0; } } elsif ($firstfuncline) { if (/^\s+"""(.*)/) { my $comment = $1; chomp $comment; $infunccomment = 1; if ($comment =~ /"""/) { $comment =~ s/"""//; $infunccomment = 0; } print "\t\t$comment\n"; } $firstfuncline = 0; } elsif ($infunccomment) { my $comment = $_; chomp $comment; if ($comment =~ /"""/) { $comment =~ s/"""//; $infunccomment = 0; } print "\t\t$comment\n"; } }