unit formatting of negatives
[barcat.git] / reformat-podusage
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use open qw( :std :utf8 );
5 use re '/msx';
6
7 our $VERSION = '1.00';
8
9 local $/ = undef;  # slurp
10 my $source = readline;
11 my $pod = $source;
12 $pod =~ s/^=over\K/ 25/;  # indent options list
13 $pod =~ s{
14         ^=item \h \N*\n\n \N*\n \K  # first line
15         (?: (?: ^=over .*? ^=back\n )? (?!=) \N*\n )*
16 }{\n}g;  # abbreviate options
17 $pod =~ s/[.,](?=\n)//g;  # trailing punctuation
18 $pod =~ s/^=item\ \K(?=--)/____/g;  # align long options
19 # abbreviate <variable> indicators
20 $pod =~ s/\Q>.../s>/g;
21 $pod =~ s/<(?:number|count|seconds)>/N/g;
22 $pod =~ s/<character(s?)>/\Uchar$1/g;
23 $pod =~ s/\Q | /|/g;
24 $pod =~ s/(?<!\w)<([a-z]+)>/\U$1/g;  # uppercase
25
26 require Pod::Usage;
27 my $parser = Pod::Usage->new(USAGE_OPTIONS => {
28         -indent => 2, -width => 78,
29 });
30 $parser->select('SYNOPSIS', 'OPTIONS');
31 $parser->output_string(\my $usage);
32 $parser->parse_string_document($pod);
33
34 $usage =~ s/\n(?=\n\h)//msg;  # strip space between items
35 $usage =~ s/^\ \ \K____/    /g;  # nbsp substitute
36
37 if ($ARGV eq '-') {
38         # custom formatted minimal usage text from pod document
39         print $usage;
40 }
41 elsif (open my $rewrite, '>', $ARGV) {
42         # replace perl code between program end and pod start
43         $source =~ s/^__END__\n \K .*? (?=^=)/$usage/;
44         print {$rewrite} $source;
45 }