File Coverage

blib/lib/Pod/Elemental/Transformer/Gatherer.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 0 1 0.0
total 32 33 96.9


line stmt bran cond sub pod time code
1             package Pod::Elemental::Transformer::Gatherer 0.103006;
2             # ABSTRACT: gather related paragraphs under a shared header
3              
4 1     1   518 use Moose;
  1         2  
  1         7  
5             with 'Pod::Elemental::Transformer';
6              
7 1     1   5675 use namespace::autoclean;
  1         4  
  1         9  
8              
9 1     1   80 use MooseX::Types::Moose qw(CodeRef);
  1         1  
  1         11  
10 1     1   4136 use Pod::Elemental::Node;
  1         2  
  1         13  
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             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             has container => (
86             is => 'ro',
87             does => 'Pod::Elemental::Node',
88             required => 1,
89             );
90              
91             sub transform_node {
92 1     1 0 7 my ($self, $node) = @_;
93              
94 1         2 my @indexes;
95 1         2 for my $i (0 .. @{ $node->children } - 1) {
  1         25  
96 5 100       154 push @indexes, $i if $self->gather_selector->($node->children->[ $i ]);
97             }
98              
99 1         2 my @paras;
100 1         3 for my $idx (reverse @indexes) {
101 2         3 unshift @paras, splice @{ $node->children }, $idx, 1;
  2         45  
102             }
103              
104 1         28 $self->container->children(\@paras);
105              
106 1         2 splice @{ $node->children }, $indexes[0], 0, $self->container;
  1         23  
107              
108 1         3 return $node;
109             }
110              
111             __PACKAGE__->meta->make_immutable;
112              
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             Pod::Elemental::Transformer::Gatherer - gather related paragraphs under a shared header
124              
125             =head1 VERSION
126              
127             version 0.103006
128              
129             =head1 OVERVIEW
130              
131             Like the Nester transformer, this Gatherer produces structure and containment
132             in a Pod document. Unlike that Nester, it does not find top-level elements,
133             but instead produces them.
134              
135             It looks for all elements matching the C<gather_selector>. They are removed
136             from the node. In the place of the first found element, the C<container> node
137             is placed into the transformed node, and all the gathered elements are made
138             children of the container.
139              
140             So, given this document:
141              
142             Document
143             =head1 Foo
144             =over 4
145             =item * xyzzy
146             =item * abcdef
147             =back
148             =head1 Bar
149             =over 4
150             =item * 1234
151             =item * 8765
152             =back
153              
154             ...and this nester...
155              
156             my $gatherer = Pod::Elemental::Transformer::Gatherer->new({
157             gather_selector => s_command( [ qw(over item back) ] ),
158             container => Pod::Elemental::Element::Pod5::Command->new({
159             command => 'head1',
160             content => "LISTS\n",
161             }),
162             });
163              
164             Then this:
165              
166             $nester->transform_node($document);
167              
168             Will result in this document:
169              
170             Document
171             =head1 Foo
172             =head1 LISTS
173             =over 4
174             =item * xyzzy
175             =item * abcdef
176             =back
177             =over 4
178             =item * 1234
179             =item * 8765
180             =back
181             =head1 Bar
182              
183             =head1 PERL VERSION
184              
185             This library should run on perls released even a long time ago. It should work
186             on any version of perl released in the last five years.
187              
188             Although it may work on older versions of perl, no guarantee is made that the
189             minimum required version will not be increased. The version may be increased
190             for any reason, and there is no promise that patches will be accepted to lower
191             the minimum required perl.
192              
193             =head1 ATTRIBUTES
194              
195             =head2 gather_selector
196              
197             This is a coderef (a predicate) used to find the paragraphs to gather up.
198              
199             =head2 container
200              
201             This is a Pod::Elemental::Node that will be inserted into the node, containing
202             all gathered elements.
203              
204             =head1 AUTHOR
205              
206             Ricardo SIGNES <cpan@semiotic.systems>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is copyright (c) 2022 by Ricardo SIGNES.
211              
212             This is free software; you can redistribute it and/or modify it under
213             the same terms as the Perl 5 programming language system itself.
214              
215             =cut