diff options
author | Scott Baker <scott@perturb.org> | 2020-08-03 13:08:33 -0700 |
---|---|---|
committer | Karl Williamson <khw@cpan.org> | 2020-11-03 18:09:18 -0700 |
commit | 6804f65db1a6911a10cf327b3d7076c12c4f860b (patch) | |
tree | 75e37dbec3311afe0e9961c52b966db16c701f69 /ext/Pod-Html/lib | |
parent | f73391e47e605dbc951ddd9776e3c8c25686d2d7 (diff) | |
download | perl-6804f65db1a6911a10cf327b3d7076c12c4f860b.tar.gz |
Make pod2html remove whitespace from literal blocks
This brings pod2html functionality similar to how Metacpan handles
whitespace
Diffstat (limited to 'ext/Pod-Html/lib')
-rw-r--r-- | ext/Pod-Html/lib/Pod/Html.pm | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/ext/Pod-Html/lib/Pod/Html.pm b/ext/Pod-Html/lib/Pod/Html.pm index 71555e723c..038050ffb7 100644 --- a/ext/Pod-Html/lib/Pod/Html.pm +++ b/ext/Pod-Html/lib/Pod/Html.pm @@ -367,6 +367,9 @@ sub pod2html { # set options for input parser my $parser = Pod::Simple::SimpleTree->new; + # Normalize whitespace indenting + $parser->strip_verbatim_indent(\&trim_leading_whitespace); + $parser->codes_in_verbatim(0); $parser->accept_targets(qw(html HTML)); $parser->no_errata_section(!$Poderrors); # note the inverse @@ -842,4 +845,33 @@ sub relativize_url { return $rel_path; } +# Remove any level of indentation (spaces or tabs) from each code block consistently +# Borrowed from: https://metacpan.org/source/HAARG/MetaCPAN-Pod-XHTML-0.002001/lib/Pod/Simple/Role/StripVerbatimIndent.pm +sub trim_leading_whitespace { + my ($para) = @_; + my $tab_width = 4; + + # Start by converting tabs to spaces + for my $line (@$para) { + # Count how many tabs on the beginnging of the line + my ($tabs) = $line =~ /^(\t*)/; + my $tab_count = length($tabs); + + # Remove all the tabs, and add them back as spaces + $line =~ s/^\t+//g; + $line = (" " x ($tab_count * $tab_width)) . $line; + } + + # Find the line with the least amount of indent, as that's our "base" + my @indent_levels = (sort(map { $_ =~ /^( *)./mg } @$para)); + my $indent = $indent_levels[0] || ""; + + # Remove the "base" amount of indent from each line + foreach (@$para) { + $_ =~ s/^\Q$indent//mg; + } + + return; +} + 1; |