README: fix typo
[topgit/pro.git] / create-html-usage.pl
blobbf18f7454ab2fef662016be3c11a8c9479f9a1fc
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 return "tg status [-v] [--exit-code]";
32 return undef;
35 sub wrap
37 my ($w, $i, $s) = @_;
38 my $h = ' ' x $i;
39 my $ans = '';
40 while (length($s) > $w && $s =~ /^(.{1,$w})(?<=\w)\b[ \t]+(.+)$/s) {
41 $ans .= $1."\n";
42 $s = "$h$2";
44 $ans .= $s if $s !~ /^\s*$/;
45 return $ans;
48 my $textmode;
49 $textmode=1, shift if defined($ARGV[0]) && $ARGV[0] eq '--text';
50 my $tab = ' ' x 8;
51 while (my $line = <>) {
52 chomp $line;
53 1 while $line =~ s/\t+/" " x (($+[0] - $-[0]) * 8 - $-[0] % 8)/e;
54 $line =~ s'^``(.*)``$'wrap(78, 4, $1)'e if $textmode;
55 $line =~ s'^(\s*):`(.+?)`_:'"$1$2 "'e if $textmode;
56 if (defined($last)) {
57 printf "%s\n", $last;
58 if ($line =~ /^[~]+$/ && $last =~ /^tg ([^\s]+)$/) {
59 my @usage = get_tg_usage($1);
60 if (@usage) {
61 printf "%s\n", $line;
62 if ($textmode) {
63 printf "%s", join("",map({wrap(78, 12, "$tab$_")."\n"} @usage));
64 } else {
65 printf "%s", join("",map({"$tab| ".'``'.$_.'``'."\n"} @usage));
67 $line = "";
71 $last = $line;
74 printf "%s\n", $last if defined($last);
75 exit 0;