Skip to content

Commit 5c8dd08

Browse files
committed
Improve static doc generation (no content change)
1 parent 3f854b6 commit 5c8dd08

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

doc/make-doc.pl

+49-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use 5.038002;
44
use warnings;
55

6-
our $VERSION = "0.02 - 20250103";
6+
our $VERSION = "0.03 - 20250106";
77
our $CMD = $0 =~ s{.*/}{}r;
88

99
sub usage {
@@ -12,6 +12,7 @@ sub usage {
1212
exit $err;
1313
} # usage
1414

15+
use Pod::Text;
1516
use File::Find;
1617
use List::Util qw( first );
1718
use Encode qw( encode decode );
@@ -26,7 +27,7 @@ sub usage {
2627
-d "doc" or mkdir "doc", 0775;
2728

2829
my @pm; # Do *NOT* scan t/
29-
find (sub { m/\.pm$/ and push @pm => $File::Find::name }, "lib");
30+
-d "lib" and find (sub { m/\.pm$/ and push @pm => $File::Find::name }, "lib");
3031
@pm or @pm = sort glob "*.pm";
3132
if (@pm == 0 and open my $fh, "<", "Makefile.PL") {
3233
my @mpl = <$fh>;
@@ -41,6 +42,7 @@ sub usage {
4142
}
4243
}
4344

45+
push @pm => @ARGV;
4446
@pm = sort grep { ! -l $_ } @pm;
4547
@pm or die "No documentation source files found\n";
4648

@@ -49,26 +51,56 @@ sub usage {
4951
say " $_" for @pm;
5052
}
5153

54+
sub dext {
55+
my ($pm, $ext) = @_;
56+
my $fn = $pm =~ s{^lib/}{}r
57+
=~ s{^(?:App|scripts|examples)/}{}r
58+
=~ s{/}{-}gr
59+
=~ s{(?:\.pm)?$}{.$ext}r # examples, scripts
60+
=~ s{^(?=CSV_XS\.)}{Text-}r
61+
=~ s{^(?=Peek\.)} {Data-}r
62+
=~ s{^(?=Read\.)} {Spreadsheet-}r
63+
=~ s{^(SpeedTest)} {\L$1}ri
64+
=~ s{^}{doc/}r;
65+
$fn;
66+
} # dext
67+
5268
my %enc;
69+
my %pod;
70+
{ # Check if file had pod at all
71+
foreach my $pm (@pm) {
72+
open my $fh, ">", \$pod{$pm};
73+
Pod::Text->new->parse_from_file ($pm, $fh);
74+
close $fh;
75+
}
76+
}
77+
5378
eval { require Pod::Checker; };
5479
if ($@) {
5580
warn "Cannot convert pod to markdown: $@\n";
5681
}
5782
else {
5883
my $fail = 0;
84+
my %ignore_empty = (
85+
"lib/DBI/ProfileData.pm" => 7,
86+
"Peek.pm" => 4,
87+
"Read.pm" => 5,
88+
);
5989
foreach my $pm (@pm) {
6090
open my $eh, ">", \my $err;
6191
my $pc = Pod::Checker->new ();
6292
my $ok = $pc->parse_from_file ($pm, $eh);
6393
close $eh;
6494
$enc{$pm} = $pc->{encoding};
6595
$err && $err =~ m/\S/ or next;
66-
if ($pm eq "lib/DBI/ProfileData.pm") {
67-
# DBI::Profile has 7 warnings on empty previous paragraphs
68-
# as it uses =head2 for all possible invocation alternatives
69-
# Ignore these warnings if those are all
70-
my @err = grep m/ WARNING: / => split m/\n+/ => $err;
71-
@err == 7 && $err eq join "\n" => @err, "" and next;
96+
# Ignore warnings here on empty previous paragraphs as it
97+
# uses =head2 for all possible invocation alternatives
98+
if (my $ni = $ignore_empty{$pm}) {
99+
my $pat = qr{ WARNING: empty section };
100+
my @err = split m/\n+/ => $err;
101+
my @wrn = grep m/$pat/ => @err;
102+
@wrn == $ni and $err = join "\n" => grep !m/$pat/ => @err;
103+
$err =~ m/\S/ or next;
72104
}
73105
say $pm;
74106
say $err;
@@ -83,10 +115,9 @@ sub usage {
83115
}
84116
else {
85117
foreach my $pm (@pm) {
86-
my $md = $pm =~ s{^lib/(?:App/)?}{}r =~ s{/}{-}gr =~ s{\.pm$}{.md}r =~ s{^}{doc/}r;
87-
printf STDERR "%-43s <- %s\n", $md, $pm if $opt_v;
118+
my $md = dext ($pm, "md");
88119
my $enc = $enc{$pm} ? "encoding($enc{$pm})" : "bytes";
89-
say "$pm ($enc)" if $opt_v > 1;
120+
printf STDERR "%-43s <- %s (%s)\n", $md, $pm, $enc if $opt_v;
90121
open my $ph, "<:$enc", $pm;
91122
my $p = Pod::Markdown->new ();
92123
$p->output_string (\my $m);
@@ -111,8 +142,9 @@ sub usage {
111142
}
112143
else {
113144
foreach my $pm (@pm) {
114-
my $html = $pm =~ s{^lib/(?:App/)?}{}r =~ s{/}{-}gr =~ s{\.pm$}{.html}r =~ s{^}{doc/}r;
115-
printf STDERR "%-43s <- %s\n", $html, $pm if $opt_v;
145+
$pod{$pm} or next; # Skip HTML for files without pod
146+
my $html = dext ($pm, "html");
147+
printf STDERR "%-43s <- %s (%s)\n", $html, $pm, $enc{$pm} // "-" if $opt_v;
116148
my $tf = "x_$$.html";
117149
unlink $tf if -e $tf;
118150
Pod::Html::pod2html ("--infile=$pm", "--outfile=$tf", "--quiet");
@@ -142,7 +174,7 @@ sub usage {
142174
split m/:+/ => $ENV{PATH};
143175
$opt_v and say $nrf;
144176
foreach my $pm (@pm) {
145-
my $man = $pm =~ s{^lib/(?:App/)?}{}r =~ s{/}{-}gr =~ s{\.pm$}{.3}r =~ s{^}{doc/}r;
177+
my $man = dext ($pm, "3");
146178
printf STDERR "%-43s <- %s\n", $man, $pm if $opt_v;
147179
open my $fh, ">", \my $p;
148180
Pod::Man->new (section => 3)->parse_from_file ($pm, $fh);
@@ -171,6 +203,7 @@ sub usage {
171203
|\x{201d}|\xe2\x80\x9d )}{"}grx # "
172204
=~ s{(?:\x{2212}|\xe2\x88\x92
173205
|\x{2010}|\xe2\x80\x90 )}{-}grx # -
206+
=~ s{(?:\x{2022}|\xe2\x80\xa2 )}{*}grx # BULLET
174207
=~ s{(?:\e\[|\x9b)[0-9;]*m} {}grx); # colors
175208
}
176209

@@ -181,7 +214,8 @@ sub usage {
181214
}
182215
$opt_v and say "Writing $mfn (", length $p, ")";
183216
open $oh, ">:encoding(utf-8)", $mfn or die "$mfn: $!\n";
184-
print $oh $p;
217+
# nroff / troff / grotty cause double-encoding
218+
print $oh encode ("iso-8859-1", decode ("utf-8", $p));
185219
close $oh;
186220
}
187221
}

0 commit comments

Comments
 (0)