diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd40d8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +blib* +Makefile +Makefile.old +Build +_build* +pm_to_blib* +*.tar.gz +.lwpcookies +Pod-Elemental-Transformer-List-Converter-* +cover_db +*.old +inc +.*.swp +META.yml +*.bak +MYMETA.* +.build/ +releases/ diff --git a/Changes b/Changes new file mode 100644 index 0000000..f754e62 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Pod-Elemental-Transformer-List-Converter + +{{$NEXT}} +- initial release diff --git a/README.pod b/README.pod new file mode 100644 index 0000000..e69de29 diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..2316e39 --- /dev/null +++ b/dist.ini @@ -0,0 +1,8 @@ +name = Pod-Elemental-Transformer-List-Converter +author = Chris Weyl +license = LGPL_2_1 +copyright_holder = Chris Weyl +copyright_year = 2012 + + +[@RSRCHBOY] diff --git a/lib/Pod/Elemental/Transformer/List/Converter.pm b/lib/Pod/Elemental/Transformer/List/Converter.pm new file mode 100644 index 0000000..3b7c1ff --- /dev/null +++ b/lib/Pod/Elemental/Transformer/List/Converter.pm @@ -0,0 +1,118 @@ +package Pod::Elemental::Transformer::List::Converter; + +# ABSTRACT: Convert a list to... something else + +use Moose; +use namespace::autoclean; +use Moose::Autobox; + +use Pod::Elemental; +use Pod::Elemental::Transformer 0.102361; + +with 'Pod::Elemental::Transformer'; + +# debugging... +#use Smart::Comments; + +=attr command + +The command we change C<=item> elements to; defaults to C. + +=method command + +Accessor to the command attribute. + +=cut + +has command => (is => 'rw', isa => 'Str', default => 'head2'); + +=method transform_node($node) + +Takes a node, and replaces any C<=item>'s with our target command (by default, +C<=head2>). We also drop any command elements found for C<=over> and +C<=back>. + +=cut + +sub transform_node { + my ($self, $node) = @_; + + my %drop = map { $_ => 1 } qw{ over back }; + my @elements; + + ### get children, and loop over them... + ELEMENT_LOOP: for my $element ($node->children->flatten) { + + do { push @elements, $element; next ELEMENT_LOOP } + unless $element->does('Pod::Elemental::Command'); + + if ($element->does('Pod::Elemental::Command')) { + + my $command = $element->command; + next ELEMENT_LOOP if $drop{$command}; + + if ($command eq 'item') { + + my $content = $element->content; + + ### $content + if ($content =~ /^\*\s*$/) { + + warn 'not handling plain * items yet'; + next ELEMENT_LOOP; + } + elsif ($content =~ /^\*/) { + + $content =~ s/^\*\s*//; + } + + chomp $content; + $element->command($self->command); + $element->content($content); + } + + push @elements, $element; + } + } + + $node->children([ @elements ]); + return; +} + +__PACKAGE__->meta->make_immutable; + +1; + +__END__ + +=head1 SYNOPSIS + + # somewhere inside your code... + my $transformer = Pod::Elemental::Transformer::List::Converter->new; + $transformer->transform_node($node); + +=head1 DESCRIPTION + +This L takes a given node's children, and +converts any list commands to another command, C by default. + +That is: + +=over 4 + +=item C<=item> becomes C<=head2>, and + +=item C<=over> and <=back> commands are dropped entirely. + +=back + +As you can imagine, it's important to be selective with the nodes you run +through this transformer -- if you pass the entire document to it, it will +obliterate any lists found. + +=head1 SEE ALSO + +L +L + +=cut diff --git a/t/equiv-pods.t b/t/equiv-pods.t new file mode 100644 index 0000000..2702929 --- /dev/null +++ b/t/equiv-pods.t @@ -0,0 +1,118 @@ + +# The logic in this file is essentally stolen from a file of the same name in +# Pod::Elemental::Transformer::List. Thanks! :) + +package X; +use strict; +use warnings; + +# HOW TO READ THESE TESTS: +# All the list_id tests get a big string; it's two parts, divided by a ------ +# line. The first half is what you write. The second part is what it's +# transformed to before publishing. + +use Test::More 'no_plan'; +use Test::Differences; + +use Pod::Elemental; +use Pod::Elemental::Transformer::Pod5; +use Pod::Elemental::Transformer::List::Converter; + +my $pod5 = Pod::Elemental::Transformer::Pod5->new; +my $list = Pod::Elemental::Transformer::List::Converter->new; + +sub list_is { + my ($comment, $string) = @_; + my ($input, $want) = split /^-{10,}$/m, $string; + $want =~ s/\A\n//; # stupid + + $input = "=pod\n\n$input"; + $want = "=pod\n\n$want\n=cut\n"; + my $doc = Pod::Elemental->read_string($input); + $pod5->transform_node($doc); + $list->transform_node($doc); + eq_or_diff($doc->as_pod_string, $want, $comment); +} + +list_is simple_list => <<'END_POD'; +=over 4 + +=item foo + +=item bar + +=item baz + +=back +-------------------------------------- +=head2 foo + +=head2 bar + +=head2 baz +END_POD + +list_is bullet => <<'END_POD'; +=over 4 + +=item * foo + +=item * bar + +=item * baz + +=back +-------------------------------------- +=head2 foo + +=head2 bar + +=head2 baz +END_POD + +list_is bullet_and_following_para => <<'END_POD'; +=over 4 + +=item * foo + +Hi there! + +=item * bar + +Bip! + +=item * baz + +=back +-------------------------------------- +=head2 foo + +Hi there! + +=head2 bar + +Bip! + +=head2 baz +END_POD + +#my $list = Pod::Elemental::Transformer::List::Converter->new->; +$list->command('head3'); + +list_is simple_list_to_head3_not_head2 => <<'END_POD'; +=over 4 + +=item foo + +=item bar + +=item baz + +=back +-------------------------------------- +=head3 foo + +=head3 bar + +=head3 baz +END_POD