| 1 | package Pod::Elemental::Transformer::Gatherer 0.103006; | ||||||||||
| 2 | # ABSTRACT: gather related paragraphs under a shared header | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | with 'Pod::Elemental::Transformer'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 1 | ▶ | use MooseX::Types::Moose qw(CodeRef); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Pod::Elemental::Node; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | #pod =head1 OVERVIEW | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod Like the Nester transformer, this Gatherer produces structure and containment | ||||||||||
| 15 | #pod in a Pod document. Unlike that Nester, it does not find top-level elements, | ||||||||||
| 16 | #pod but instead produces them. | ||||||||||
| 17 | #pod | ||||||||||
| 18 | #pod It looks for all elements matching the C<gather_selector>. They are removed | ||||||||||
| 19 | #pod from the node. In the place of the first found element, the C<container> node | ||||||||||
| 20 | #pod is placed into the transformed node, and all the gathered elements are made | ||||||||||
| 21 | #pod children of the container. | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod So, given this document: | ||||||||||
| 24 | #pod | ||||||||||
| 25 | #pod Document | ||||||||||
| 26 | #pod =head1 Foo | ||||||||||
| 27 | #pod =over 4 | ||||||||||
| 28 | #pod =item * xyzzy | ||||||||||
| 29 | #pod =item * abcdef | ||||||||||
| 30 | #pod =back | ||||||||||
| 31 | #pod =head1 Bar | ||||||||||
| 32 | #pod =over 4 | ||||||||||
| 33 | #pod =item * 1234 | ||||||||||
| 34 | #pod =item * 8765 | ||||||||||
| 35 | #pod =back | ||||||||||
| 36 | #pod | ||||||||||
| 37 | #pod ...and this nester... | ||||||||||
| 38 | #pod | ||||||||||
| 39 | #pod my $gatherer = Pod::Elemental::Transformer::Gatherer->new({ | ||||||||||
| 40 | #pod gather_selector => s_command( [ qw(over item back) ] ), | ||||||||||
| 41 | #pod container => Pod::Elemental::Element::Pod5::Command->new({ | ||||||||||
| 42 | #pod command => 'head1', | ||||||||||
| 43 | #pod content => "LISTS\n", | ||||||||||
| 44 | #pod }), | ||||||||||
| 45 | #pod }); | ||||||||||
| 46 | #pod | ||||||||||
| 47 | #pod Then this: | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod $nester->transform_node($document); | ||||||||||
| 50 | #pod | ||||||||||
| 51 | #pod Will result in this document: | ||||||||||
| 52 | #pod | ||||||||||
| 53 | #pod Document | ||||||||||
| 54 | #pod =head1 Foo | ||||||||||
| 55 | #pod =head1 LISTS | ||||||||||
| 56 | #pod =over 4 | ||||||||||
| 57 | #pod =item * xyzzy | ||||||||||
| 58 | #pod =item * abcdef | ||||||||||
| 59 | #pod =back | ||||||||||
| 60 | #pod =over 4 | ||||||||||
| 61 | #pod =item * 1234 | ||||||||||
| 62 | #pod =item * 8765 | ||||||||||
| 63 | #pod =back | ||||||||||
| 64 | #pod =head1 Bar | ||||||||||
| 65 | #pod | ||||||||||
| 66 | #pod =attr gather_selector | ||||||||||
| 67 | #pod | ||||||||||
| 68 | #pod This is a coderef (a predicate) used to find the paragraphs to gather up. | ||||||||||
| 69 | #pod | ||||||||||
| 70 | #pod =cut | ||||||||||
| 71 | |||||||||||
| 72 | 1 | has gather_selector => ( | |||||||||
| 73 | is => 'ro', | ||||||||||
| 74 | isa => CodeRef, | ||||||||||
| 75 | required => 1, | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | #pod =attr container | ||||||||||
| 79 | #pod | ||||||||||
| 80 | #pod This is a Pod::Elemental::Node that will be inserted into the node, containing | ||||||||||
| 81 | #pod all gathered elements. | ||||||||||
| 82 | #pod | ||||||||||
| 83 | #pod =cut | ||||||||||
| 84 | |||||||||||
| 85 | 1 | has container => ( | |||||||||
| 86 | is => 'ro', | ||||||||||
| 87 | does => 'Pod::Elemental::Node', | ||||||||||
| 88 | required => 1, | ||||||||||
| 89 | ); | ||||||||||
| 90 | |||||||||||
| 91 | 1 | ▶ | sub transform_node { | ||||||||
|
Subroutine
transform_node: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 92 | 1 | my ($self, $node) = @_; | |||||||||
| 93 | |||||||||||
| 94 | 1 | my @indexes; | |||||||||
| 95 | 1 | for my $i (0 .. @{ $node->children } - 1) { | |||||||||
| 96 | 5 | ▶ | push @indexes, $i if $self->gather_selector->($node->children->[ $i ]); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 1 | my @paras; | |||||||||
| 100 | 1 | for my $idx (reverse @indexes) { | |||||||||
| 101 | 2 | unshift @paras, splice @{ $node->children }, $idx, 1; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 1 | $self->container->children(\@paras); | |||||||||
| 105 | |||||||||||
| 106 | 1 | splice @{ $node->children }, $indexes[0], 0, $self->container; | |||||||||
| 107 | |||||||||||
| 108 | 1 | return $node; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 112 | |||||||||||
| 113 | 1 | 1; | |||||||||
| 114 | |||||||||||
| 115 | __END__ | ||||||||||