diff options
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; |