version 1.10 marking stable log support
[barcat.git] / reformat-podusage
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5 use open qw( :std :utf8 );
6 use re '/msx';
7
8 our $VERSION = '1.01';
9
10 local $/ = undef;  # slurp
11 my $source = readline;
12 my $pod = $source;
13 $pod =~ s/^=over\K/ 25/;  # indent options list
14 $pod =~ s/[BC]<([^>]+)>/$1/g;  # unbolden
15 $pod =~ s{
16         ^=item \h \N*\n\n \N*\n \K  # first line
17         (?: (?: ^=over .*? ^=back\n )? (?!=) \N*\n )*
18 }{\n}g;  # abbreviate options
19 $pod =~ s/^=item\ \K(?=--)/____/g;  # align long options
20 # abbreviate <variable> indicators
21 $pod =~ s/\Q>.../s>/g;
22 $pod =~ s/I<(?:number|count|seconds)>/N/g;
23 $pod =~ s/I<character(s?)>/\Uchar$1/g;
24 $pod =~ s/\Q | /|/g;
25 $pod =~ s/I<([a-z]+)> (?![.,])/\U$1/g;  # uppercase
26 $pod =~ s/[.,](?=\n)//g;  # trailing punctuation
27
28 require Pod::Usage;
29 my $parser = Pod::Usage->new(USAGE_OPTIONS => {
30         -indent => 2, -width => 78,
31 });
32 $parser->select('SYNOPSIS', 'OPTIONS');
33 $parser->output_string(\my $usage);
34 $parser->parse_string_document($pod);
35
36 $usage =~ s/\n(?=\n\h)//msg;  # strip space between items
37 $usage =~ s/^\ \ \K____/    /g;  # nbsp substitute
38
39 if (open my $logo, '<', 'mascot.txt') {
40         # append logo lines to top usage lines
41         my @ll = split /\n/, readline $logo;
42         my @ul = split /\n/, $usage, @ll + 1;
43         # centered in empty space on the second (longest) line
44         my $pad = (78 - 1 + length($ul[1]) - length($ll[0])) >> 1;
45         $ul[$_] .= (' ' x ($pad - length($ul[$_]))) . $ll[$_] for 0..$#ll;
46         $usage = join "\n", @ul;
47 }
48
49 if ($ARGV eq '-') {
50         # custom formatted minimal usage text from pod document
51         print $usage;
52 }
53 elsif (open my $rewrite, '>', $ARGV) {
54         # replace perl code between program end and pod start
55         $source =~ s/^__END__\n \K .*? (?=^=)/$usage/;
56         print {$rewrite} $source;
57 }