-
Notifications
You must be signed in to change notification settings - Fork 7
/
create-html-usage.pl
executable file
·116 lines (107 loc) · 2.91 KB
/
create-html-usage.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl
# create-html-usage.pl -- insert usage lines into README_DOCS.rst
# Copyright (C) 2015,2017,2020,2021 Kyle J. McKay. All rights reserved.
# License GPLv2 or, at your option, any later version.
use strict;
use warnings;
use File::Basename;
my $mydir = dirname($0);
my $last = undef;
sub get_tg_usage($)
{
my $name = shift;
my $xname;
for ("$mydir/tg-$name", "$mydir/tg--$name") {
-x $_ and $xname=$_, last;
}
if (defined $xname) {
my $usage = `"$xname" -h 2>&1`;
chomp $usage;
my $opts;
($usage,$opts) = split "\nOptions:\n", $usage;
$usage =~ s/^(Usage|\s+Or):\s*/: /mig;
$usage =~ s/[ \t]*\n[ \t]+/ /gs;
$usage =~ s/^: //mig;
defined $opts or $opts="";
$opts =~ s/^[ \t]*(?=-)/: /mig;
$opts =~ s/[ \t]*\n[ \t]+/ /gs;
$opts =~ s/^: //mig;
return ([split "\n", $usage],[split "\n", $opts]);
} elsif ($name eq "help") {
return (["tg help [-w] [<command>]"],
["-w view help in browser"]);
} elsif ($name eq "status") {
my $tgsthelp = $ENV{TG_STATUS_HELP_USAGE} || "status";
return "tg $tgsthelp";
}
return undef;
}
sub wrap
{
my ($w, $i, $s) = @_;
my $h = ' ' x $i;
my $ans = '';
while (length($s) > $w && $s =~ /^(.{1,$w})(?<=[]|\w])[ \t]+(.+)$/s) {
$ans .= $1."\n";
$s = "$h$2";
}
$ans .= $s if $s !~ /^\s*$/;
return $ans;
}
sub maybe_uc
{
my $l = shift;
$l =~ /^tg / and return $l;
return uc($l);
}
my $textmode;
$textmode=1, shift if defined($ARGV[0]) && $ARGV[0] eq '--text';
my $tab = ' ' x 8;
my $discard = 0;
while (<>) {
chomp;
# From the Perl camel book "Fluent Perl" section (slightly modified)
s/(.*?)(\t+)/$1 . ' ' x (length($2) * 8 - length($1) % 8)/eg;
if ($textmode) {
$discard and do {$discard = 0; next};
/^::\s*$/ and do {$discard = 1; next};
m'^```+$' and next;
s'^``([^``\n].*)``$'wrap(78, 4, $1)'e;
s'^(\s*):`(`.+?`)`: '"$1$2 "'e;
s'^(\s*):`(.+?)`_: '"$1\"$2\" "'e;
s'^(\s*):(\w+?)_?: '"$1\"$2\""'e;
s'`([^`]+?>)`_'"$1"'ge;
s'`([^`]+?)`_'"\"".maybe_uc($1)."\""'ge;
s'`(`[^`]+?`)`'"$1"'ge;
s'"(`[^`]+?`)"'"$1"'ge;
s' ([A-Za-z]+?)_(?![A-Za-z])'" \"".maybe_uc($1)."\""'ge;
s'::$':';
}
if (defined($last)) {
printf "%s\n", $last;
if (/^[~]+$/ && $last =~ /^tg ([^\s]+)$/) {
my @usage = get_tg_usage($1);
my @options = ();
if (@usage && ref($usage[0]) eq 'ARRAY') {
@options = @{$usage[1]} if ref($usage[1]) eq 'ARRAY';
@usage = @{$usage[0]};
}
if (@usage) {
printf "%s\n", $_;
if ($textmode) {
printf "%s", join("",map({wrap(78, 12, "$tab$_")."\n"} @usage));
@options and printf "${tab}Options:\n%s",
join("",map({wrap(78, 32, "$tab $_")."\n"} @options));
} else {
printf "%s", join("",map({"$tab| ".'``'.$_.'``'."\n"} @usage));
@options and printf "$tab|\n$tab| ".'``Options:``'."\n%s",
join("",map({"$tab| ".'``  '.$_.'``'."\n"} @options));
}
$_ = "";
}
}
}
$last = $_;
}
printf "%s\n", $last if defined($last);
exit 0;