Subject: | Verbatim paragraphs not preserved if indentation doesn't match |
Markdown uses 4 spaces or a tab for verbatim blocks.
Pod allows any initial whitespace to indicate a verbatim block.
Since I use 2 spaces for all my indentation,
my paragraphs don't retain their verbatim-ness from pod to markdown.
Attached patch finds the line with the shortest indentation,
determines the minimum required to upgrade to 4,
and prepends that number of spaces to each line.
If you're not interested in this module anymore I'd be happy to take
comaint on it. Either way it would be great if we could get the latest
version of the repo on http://marcel-maint.github.com/
Thanks for all your great work!
Subject: | pod-markdown-verbatim.patch |
diff --git a/lib/Pod/Markdown.pm b/lib/Pod/Markdown.pm
index c58c282..26eb437 100644
--- a/lib/Pod/Markdown.pm
+++ b/lib/Pod/Markdown.pm
@@ -145,6 +145,24 @@ sub command {
sub verbatim {
my ($parser, $paragraph) = @_;
+
+ # POD verbatim can start with any number of spaces (or tabs)
+ # markdown should be 4 spaces (or a tab)
+ # so indent any paragraphs so that all lines start with at least 4 spaces
+ my @lines = split /\n/, $paragraph;
+ my $indent = ' ' x 4;
+ foreach my $line ( @lines ){
+ next unless $line =~ m/^( +)/;
+ # find the smallest indentation
+ $indent = $1 if length($1) < length($indent);
+ }
+ if( (my $smallest = length($indent)) < 4 ){
+ # invert to get what needs to be prepended
+ $indent = ' ' x (4 - $smallest);
+ # leave tabs alone
+ $paragraph = join "\n", map { /^\t/ ? $_ : $indent . $_ } @lines;
+ }
+
$parser->_save($paragraph);
}
diff --git a/t/verbatim.t b/t/verbatim.t
new file mode 100755
index 0000000..08b1449
--- /dev/null
+++ b/t/verbatim.t
@@ -0,0 +1,91 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use Test::More tests => 1;
+use Test::Differences;
+use Pod::Markdown;
+
+my $parser = Pod::Markdown->new;
+$parser->parse_from_filehandle(\*DATA);
+my $markdown = $parser->as_markdown;
+my $expect = <<'EOMARKDOWN';
+# SYNOPSIS
+
+ # 4 spaces
+ # should come out the same
+
+# TABS
+
+ These tabs
+ can be left alone
+
+# 3 SPACES
+
+ 3 spaces should be converted to 4.
+ Here, too
+
+And also
+
+ here.
+
+# MIXED (You don't really want to do that, though, do you?)
+
+Mixed paragraphs should all get the same indentation added
+to preserve the formatting:
+
+ 4 spaces (+ 2 = 6)
+ a tab
+ 3 spaces (+ 2 = 5)
+ 2 spaces (+ 2 = 4) (the minimum)
+
+# 5 spaces
+
+ Because you can
+ if you want to
+
+# THAT'S ENOUGH
+EOMARKDOWN
+
+1 while chomp $markdown;
+1 while chomp $expect;
+
+eq_or_diff $markdown, $expect, "this file's POD as markdown";
+
+__DATA__
+=head1 SYNOPSIS
+
+ # 4 spaces
+ # should come out the same
+
+=head1 TABS
+
+ These tabs
+ can be left alone
+
+=head1 3 SPACES
+
+ 3 spaces should be converted to 4.
+ Here, too
+
+And also
+
+ here.
+
+=head1 MIXED (You don't really want to do that, though, do you?)
+
+Mixed paragraphs should all get the same indentation added
+to preserve the formatting:
+
+ 4 spaces (+ 2 = 6)
+ a tab
+ 3 spaces (+ 2 = 5)
+ 2 spaces (+ 2 = 4) (the minimum)
+
+=head1 5 spaces
+
+ Because you can
+ if you want to
+
+=head1 THAT'S ENOUGH
+
+=cut