3 # create-html-usage.pl -- insert usage lines into README_DOCS.rst
4 # Copyright (C) 2015,2017,2020,2021 Kyle J. McKay. All rights reserved.
5 # License GPLv2 or, at your option, any later version.
12 my $mydir = dirname
($0);
19 for ("$mydir/tg-$name", "$mydir/tg--$name") {
20 -x
$_ and $xname=$_, last;
23 my $usage = `"$xname" -h 2>&1`;
26 ($usage,$opts) = split "\nOptions:\n", $usage;
27 $usage =~ s/^(Usage|\s+Or):\s*/: /mig;
28 $usage =~ s/[ \t]*\n[ \t]+/ /gs;
30 defined $opts or $opts="";
31 $opts =~ s/^[ \t]*(?=-)/: /mig;
32 $opts =~ s/[ \t]*\n[ \t]+/ /gs;
34 return ([split "\n", $usage],[split "\n", $opts]);
35 } elsif ($name eq "help") {
36 return (["tg help [-w] [<command>]"],
37 ["-w view help in browser"]);
38 } elsif ($name eq "status") {
39 my $tgsthelp = $ENV{TG_STATUS_HELP_USAGE
} || "status";
40 return "tg $tgsthelp";
50 while (length($s) > $w && $s =~ /^(.{1,$w})(?<=[]|\w])[ \t]+(.+)$/s) {
54 $ans .= $s if $s !~ /^\s*$/;
61 $l =~ /^tg / and return $l;
66 $textmode=1, shift if defined($ARGV[0]) && $ARGV[0] eq '--text';
71 # From the Perl camel book "Fluent Perl" section (slightly modified)
72 s/(.*?)(\t+)/$1 . ' ' x (length($2) * 8 - length($1) % 8)/eg;
74 $discard and do {$discard = 0; next};
75 /^::\s*$/ and do {$discard = 1; next};
77 s
'^``([^``\n].*)``$'wrap
(78, 4, $1)'e;
78 s'^(\s
*):`(`.+?
`)`: '"$1$2 "'e
;
79 s
'^(\s*):`(.+?)`_: '"$1\"$2\" "'e;
80 s'^(\s
*):(\w
+?
)_?
: '"$1\"$2\""'e
;
81 s
'`([^`]+?>)`_'"$1"'ge;
82 s'`([^`]+?
)`_'"\"".maybe_uc($1)."\""'ge;
83 s'`(`[^`]+?
`)`'"$1"'ge;
84 s
'"(`[^`]+?`)"'"$1"'ge;
85 s' ([A
-Za
-z
]+?
)_
(?
![A
-Za
-z
])'" \"".maybe_uc($1)."\""'ge;
90 if (/^[~]+$/ && $last =~ /^tg ([^\s]+)$/) {
91 my @usage = get_tg_usage($1);
93 if (@usage && ref($usage[0]) eq 'ARRAY
') {
94 @options = @{$usage[1]} if ref($usage[1]) eq 'ARRAY
';
95 @usage = @{$usage[0]};
100 printf "%s", join("",map({wrap(78, 12, "$tab$_")."\n"} @usage));
101 @options and printf "${tab}Options:\n%s",
102 join("",map({wrap(78, 32, "$tab $_")."\n"} @options));
104 printf "%s", join("",map({"$tab| ".'``'.$_.'``'."\n"} @usage));
105 @options and printf "$tab|\n$tab| ".'``Options
:``'."\n%s",
106 join("",map({"$tab| ".'``  '.$_.'``'."\n"} @options));
115 printf "%s\n", $last if defined($last);