tg.sh: quote problem alternate directory paths
[topgit/pro.git] / create-html-usage.pl
bloba82c22ac075b8765cb0080bb47ce6d0b11203a70
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 if (defined($last)) {
58 printf "%s\n", $last;
59 if ($line =~ /^[~]+$/ && $last =~ /^tg ([^\s]+)$/) {
60 my @usage = get_tg_usage($1);
61 if (@usage) {
62 printf "%s\n", $line;
63 if ($textmode) {
64 printf "%s", join("",map({wrap(78, 12, "$tab$_")."\n"} @usage));
65 } else {
66 printf "%s", join("",map({"$tab| ".'``'.$_.'``'."\n"} @usage));
68 $line = "";
72 $last = $line;
75 printf "%s\n", $last if defined($last);
76 exit 0;