Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rsrchboy committed Feb 27, 2012
0 parents commit 899a962
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .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/
4 changes: 4 additions & 0 deletions Changes
@@ -0,0 +1,4 @@
Revision history for Pod-Elemental-Transformer-List-Converter

{{$NEXT}}
- initial release
Empty file added README.pod
Empty file.
8 changes: 8 additions & 0 deletions dist.ini
@@ -0,0 +1,8 @@
name = Pod-Elemental-Transformer-List-Converter
author = Chris Weyl <cweyl@alumni.drew.edu>
license = LGPL_2_1
copyright_holder = Chris Weyl
copyright_year = 2012


[@RSRCHBOY]
118 changes: 118 additions & 0 deletions 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<head2>.
=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<Pod::Elemental::Transformer> takes a given node's children, and
converts any list commands to another command, C<head2> 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<Pod::Elemental::Transformer>
L<Pod::Weaver::Section::Collect::FromOther>
=cut
118 changes: 118 additions & 0 deletions 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

0 comments on commit 899a962

Please sign in to comment.