File Coverage

blib/lib/Pod/Weaver/Role/SectionReplacer.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Pod::Weaver::Role::SectionReplacer;
2              
3             # ABSTRACT: A Pod::Weaver section that will replace itself in the original document.
4              
5 1     1   32902 use Moose::Role;
  0            
  0            
6             with 'Pod::Weaver::Role::Transformer';
7              
8             use Moose::Autobox 0.11;
9             use Pod::Elemental::Selectors -all;
10              
11             our $VERSION = '1.00';
12              
13             has original_section => (
14             is => 'rw',
15             );
16              
17             has section_name => (
18             is => 'ro',
19             isa => 'Str',
20             default => sub { $_[ 0 ]->default_section_name },
21             );
22              
23             requires 'default_section_name';
24              
25             has section_aliases => (
26             is => 'ro',
27             isa => 'ArrayRef[Str]',
28             default => sub { $_[ 0 ]->default_section_aliases },
29             );
30              
31             sub default_section_aliases { []; }
32              
33             sub transform_document {
34             my ( $self, $document ) = @_;
35              
36             # Build a selector for a =head1 with the correct content text.
37             my $command_selector = s_command('head1');
38             my $aliases = [ $self->section_name, @{ $self->section_aliases } ];
39             my $named_selector = sub {
40             my ( $node ) = @_;
41              
42             my $content = $node->content;
43             $content =~ s/^\s+//;
44             $content =~ s/\s+$//;
45              
46             return( $command_selector->( $_[ 0 ] ) &&
47             $aliases->any() eq $content );
48             };
49              
50             return unless $document->children->grep($named_selector)->length;
51              
52             # Take the first matching section found...
53             $self->original_section($document->children->grep($named_selector)->first);
54              
55             # ...and prune it from the document.
56             my $in_node = $document->children;
57             for ( my $i = 0; $i <= $#{ $in_node }; $i++ ) {
58             next unless $in_node->[ $i ] == $self->original_section;
59              
60             splice @{ $in_node }, $i, 1;
61             last;
62             }
63             };
64              
65             sub mvp_aliases { { section_alias => 'section_aliases', }; }
66             sub mvp_multivalue_args { ( 'section_aliases', ); }
67              
68             no Moose::Role;
69             1;
70              
71             __END__
72              
73             =pod
74              
75             =head1 NAME
76              
77             Pod::Weaver::Role::SectionReplacer - A Pod::Weaver section that will replace itself in the original document.
78              
79             =head1 VERSION
80              
81             version 1.00
82              
83             =head1 SYNOPSIS
84              
85             A role for L<Pod::Weaver> plugins, allowing them to replace a named
86             section of the input document rather than appending a potentially
87             duplicate section.
88              
89             =begin readme
90              
91             =head1 INSTALLATION
92              
93             To install this module, run the following commands:
94              
95             perl Build.PL
96             ./Build
97             ./Build test
98             ./Build install
99              
100             =end readme
101              
102             =for readme stop
103              
104             =head1 IMPLEMENTING
105              
106             This role is used by plugins that will find an existing section in the input
107             document.
108             It will prune the first existing section from the input document and make it
109             available under C<original_section> method:
110              
111             $section_plugin->original_section();
112              
113             The plugin could then choose to keep the original, by inserting it
114             into the document again, or to write something new instead, or some
115             combination of the two.
116              
117             =head2 REQUIRED METHODS
118              
119             =over
120              
121             =item B<< $plugin->default_section_name() >>
122              
123             The plugin must provide a method, C<default_section_name> which will return
124             the default name of the section, as used in the =head1 line, this is
125             available for later query via the C<section_name> accessor:
126              
127             $section_plugin->section_name
128              
129             It is recommended that you use this accessor for generating the section
130             title rather than hard-coding a value directly, because it then allows
131             the end-user to configure the section name in their weaver.ini, eg:
132              
133             [ReplaceLegal]
134             section_name = MY CUSTOMIZED LICENSE AND COPYRIGHT HEADING
135              
136             =back
137              
138             =head2 OPTIONAL METHODS
139              
140             =over
141              
142             =item B<< $plugin->default_section_aliases >>
143              
144             The plugin may also provide a C<default_section_aliases> method, which
145             should return an arrayref of alternative section names to match.
146             Like C<section_name> this allows the end-user to override the default
147             section aliases:
148              
149             [ReplaceLegal]
150             section_name = MY CUSTOMIZED LICENSE AND COPYRIGHT HEADING
151             section_alias = LICENSE AND COPYRIGHT
152             section_alias = COPYRIGHT AND LICENSE
153             section_alias = LICENCE AND COPYRIGHT
154             section_alias = COPYRIGHT AND LICENCE
155              
156             =back
157              
158             =head2 INTERNAL METHODS
159              
160             These methods are mostly internal to the role, but if you're also using
161             them in your plugin, you will need reconcile the return values.
162              
163             =over
164              
165             =item B<< $plugin->mvp_aliases() >>
166              
167             Tells L<Config::MVP> that C<section_alias> is a synonym for C<section_aliases>
168             in the weaver.ini for plugins that use this role.
169              
170             =item B<< $plugin->mvp_multivalue_args() >>
171              
172             Tells L<Config::MVP> that C<section_aliases> always takes multiple-values
173             and should be stored as an arrayref.
174              
175             =item B<< $plugin->transform_document() >>
176              
177             L<Pod::Weaver:Role::SectionReplacer> implements the role
178             L<Pod::Weaver::Role::Transformer>, and as such it provides its own
179             C<< $plugin->tranform_document() >> method in order to prune the original
180             section from the input document before any further weaving is done.
181              
182             If your plugin wishes to implement a C<< transform_document() >> of its
183             own, you will need to reconcile the two.
184              
185             =back
186              
187             =for readme continue
188              
189             =head1 SUPPORT AND DOCUMENTATION
190              
191             After installing, you can find documentation for this module with the
192             perldoc command.
193              
194             perldoc Pod::Weaver::Role::SectionReplacer
195              
196             You can also look for information at:
197              
198             =over
199              
200             =item RT, CPAN's request tracker
201              
202             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Pod-Weaver-Role-SectionReplacer
203              
204             =item AnnoCPAN, Annotated CPAN documentation
205              
206             http://annocpan.org/dist/Pod-Weaver-Role-SectionReplacer
207              
208             =item CPAN Ratings
209              
210             http://cpanratings.perl.org/d/Pod-Weaver-Role-SectionReplacer
211              
212             =item Search CPAN
213              
214             http://search.cpan.org/dist/Pod-Weaver-Role-SectionReplacer/
215              
216             =back
217              
218             =head1 AUTHOR
219              
220             Sam Graham <libpod-weaver-role-sectionreplacer-perl BLAHBLAH illusori.co.uk>
221              
222             =head1 COPYRIGHT AND LICENSE
223              
224             This software is copyright (c) 2010 by Sam Graham <libpod-weaver-role-sectionreplacer-perl BLAHBLAH illusori.co.uk>.
225              
226             This is free software; you can redistribute it and/or modify it under
227             the same terms as the Perl 5 programming language system itself.
228              
229             =cut