testlib: suppress superfluous messages from rm
[topgit/pro.git] / create-html-usage.pl
blob8afba3dd8bdbda38d12c6de9b84c1c1dd18a4a0f
1 #!/usr/bin/env perl
3 # create-html-usage.pl -- insert usage lines into README
4 # Cpoyright (C) 2015 Kyle J. McKay. All rights reserved.
5 # License GPLv2 or, at your option, any later version.
7 use strict;
8 use warnings;
10 use File::Basename;
12 my $mydir = dirname($0);
13 my $last = undef;
15 sub get_tg_usage($)
17 my $name = shift;
18 my $xname;
19 for ("$mydir/tg-$name", "$mydir/tg--$name") {
20 -x $_ and $xname=$_, last;
22 if (defined $xname) {
23 my $usage = `"$xname" -h 2>&1`;
24 chomp $usage;
25 $usage =~ s/^(Usage|\s+Or):\s*//mig;
26 return split "\n", $usage;
27 } elsif ($name eq "help") {
28 return "tg help [-w] [<command>]";
29 } elsif ($name eq "status") {
30 my $tgsthelp = $ENV{TG_STATUS_HELP_USAGE} || "status";
31 return "tg $tgsthelp";
33 return undef;
36 sub wrap
38 my ($w, $i, $s) = @_;
39 my $h = ' ' x $i;
40 my $ans = '';
41 while (length($s) > $w && $s =~ /^(.{1,$w})(?<=\w)\b[ \t]+(.+)$/s) {
42 $ans .= $1."\n";
43 $s = "$h$2";
45 $ans .= $s if $s !~ /^\s*$/;
46 return $ans;
49 my $textmode;
50 $textmode=1, shift if defined($ARGV[0]) && $ARGV[0] eq '--text';
51 my $tab = ' ' x 8;
52 while (my $line = <>) {
53 chomp $line;
54 1 while $line =~ s/\t+/" " x (($+[0] - $-[0]) * 8 - $-[0] % 8)/e;
55 $line =~ s'^``(.*)``$'wrap(78, 4, $1)'e if $textmode;
56 $line =~ s'^(\s*):`(.+?)`_:'"$1$2 "'e if $textmode;
57 $line =~ s'^(\s*):(\w+?)_:'"$1$2"'e if $textmode;
58 if (defined($last)) {
59 printf "%s\n", $last;
60 if ($line =~ /^[~]+$/ && $last =~ /^tg ([^\s]+)$/) {
61 my @usage = get_tg_usage($1);
62 if (@usage) {
63 printf "%s\n", $line;
64 if ($textmode) {
65 printf "%s", join("",map({wrap(78, 12, "$tab$_")."\n"} @usage));
66 } else {
67 printf "%s", join("",map({"$tab| ".'``'.$_.'``'."\n"} @usage));
69 $line = "";
73 $last = $line;
76 printf "%s\n", $last if defined($last);
77 exit 0;