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;
2             # ABSTRACT: gather related paragraphs under a shared header
3             $Pod::Elemental::Transformer::Gatherer::VERSION = '0.103005';
4 1     1   750 use Moose;
  1         3  
  1         9  
5             with 'Pod::Elemental::Transformer';
6              
7 1     1   6910 use namespace::autoclean;
  1         3  
  1         12  
8              
9 1     1   75 use MooseX::Types::Moose qw(CodeRef);
  1         4  
  1         11  
10 1     1   5191 use Pod::Elemental::Node;
  1         5  
  1         14  
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 8 my ($self, $node) = @_;
93              
94 1         3 my @indexes;
95 1         3 for my $i (0 .. @{ $node->children } - 1) {
  1         28  
96 5 100       185 push @indexes, $i if $self->gather_selector->($node->children->[ $i ]);
97             }
98              
99 1         8 my @paras;
100 1         4 for my $idx (reverse @indexes) {
101 2         4 unshift @paras, splice @{ $node->children }, $idx, 1;
  2         53  
102             }
103              
104 1         33 $self->container->children(\@paras);
105              
106 1         3 splice @{ $node->children }, $indexes[0], 0, $self->container;
  1         27  
107              
108 1         4 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.103005
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 ATTRIBUTES
184              
185             =head2 gather_selector
186              
187             This is a coderef (a predicate) used to find the paragraphs to gather up.
188              
189             =head2 container
190              
191             This is a Pod::Elemental::Node that will be inserted into the node, containing
192             all gathered elements.
193              
194             =head1 AUTHOR
195              
196             Ricardo SIGNES <rjbs@cpan.org>
197              
198             =head1 COPYRIGHT AND LICENSE
199              
200             This software is copyright (c) 2020 by Ricardo SIGNES.
201              
202             This is free software; you can redistribute it and/or modify it under
203             the same terms as the Perl 5 programming language system itself.
204              
205             =cut