File Coverage

blib/lib/Pod/Weaver/Plugin/AppendPrepend.pm
Criterion Covered Total %
statement 5 60 8.3
branch 0 28 0.0
condition 0 3 0.0
subroutine 2 3 66.6
pod 0 1 0.0
total 7 95 7.3


line stmt bran cond sub pod time code
1             package Pod::Weaver::Plugin::AppendPrepend;
2              
3 1     1   376311 use 5.010001;
  1         3  
4 1     1   622 use Moose;
  1         614673  
  1         9  
5             with 'Pod::Weaver::Role::Finalizer';
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-02-06'; # DATE
9             our $DIST = 'Pod-Weaver-Plugin-AppendPrepend'; # DIST
10             our $VERSION = '0.021'; # VERSION
11              
12             # regex
13             has exclude_modules => (
14             is => 'rw',
15             isa => 'Str',
16             );
17             has exclude_files => (
18             is => 'rw',
19             isa => 'Str',
20             );
21             has ignore => (
22             is => 'rw',
23             #isa => 'Bool',
24             default => sub { 1 },
25             );
26              
27             sub finalize_document {
28 0     0 0   my ($self, $document, $input) = @_;
29              
30 0           my $filename = $input->{filename};
31              
32 0 0         if (defined $self->exclude_files) {
33 0           my $re = $self->exclude_files;
34 0           eval { $re = qr/$re/ };
  0            
35 0 0         $@ and die "Invalid regex in exclude_files: $re";
36 0 0         if ($filename =~ $re) {
37 0           $self->log_debug(["skipped file '%s' (matched exclude_files)", $filename]);
38 0           return;
39             }
40             }
41              
42 0           my $package;
43 0 0         if ($filename =~ m!^lib/(.+)\.pm$!) {
44 0           $package = $1;
45 0           $package =~ s!/!::!g;
46 0 0         if (defined $self->exclude_modules) {
47 0           my $re = $self->exclude_modules;
48 0           eval { $re = qr/$re/ };
  0            
49 0 0         $@ and die "Invalid regex in exclude_modules: $re";
50 0 0         if ($package =~ $re) {
51 0           $self->log (["skipped package %s (matched exclude_modules)", $package]);
52 0           return;
53             }
54             }
55             }
56              
57             # get list of head1 commands with their position in document
58 0           my %headlines_pos;
59             {
60 0           my $i = -1;
  0            
61 0           for (@{ $document->children }) {
  0            
62 0           $i++;
63 0 0 0       next unless $_->can('command') && $_->command eq 'head1';
64 0           my $name = $_->{content};
65 0 0         next if defined $headlines_pos{$name};
66 0           $headlines_pos{$name} = $i;
67             }
68             }
69             #$self->log_debug(["current headlines in the document: %s", \%headlines_pos]);
70              
71 0           for my $h (keys %headlines_pos) {
72 0           my ($which, $target) = $h =~ /\A(append|prepend):(.+)\z/;
73 0 0         next unless $target;
74 0           $self->log_debug(["%s to section %s", $which, $target]);
75 0 0         unless (defined $headlines_pos{$target}) {
76 0 0         if ($self->ignore) {
77 0           $self->log(["Skipping $which $target: no such section"]);
78 0           next;
79             } else {
80 0           $self->log_fatal(["Can't $which $target: no such section"]);
81             }
82             }
83 0           my $section_elem = $document->children->[$headlines_pos{$target}];
84 0           my $appprep_elem = $document->children->[$headlines_pos{$h}];
85 0 0         if ($which eq 'prepend') {
86 0           unshift @{ $section_elem->children }, @{ $appprep_elem->children };
  0            
  0            
87             } else {
88 0           push @{ $section_elem->children }, @{ $appprep_elem->children };
  0            
  0            
89             }
90              
91             }
92              
93             # delete all append:/prepend: sections
94 0           for my $h (sort {$headlines_pos{$b} <=> $headlines_pos{$a}}
  0            
95             keys %headlines_pos) {
96 0 0         next unless $h =~ /\A(append|prepend):/;
97 0           splice @{ $document->children }, $headlines_pos{$h}, 1;
  0            
98             }
99             }
100              
101             1;
102             # ABSTRACT: Merge append:FOO and prepend:FOO sections in POD
103              
104             __END__
105              
106             =pod
107              
108             =encoding UTF-8
109              
110             =head1 NAME
111              
112             Pod::Weaver::Plugin::AppendPrepend - Merge append:FOO and prepend:FOO sections in POD
113              
114             =head1 VERSION
115              
116             This document describes version 0.021 of Pod::Weaver::Plugin::AppendPrepend (from Perl distribution Pod-Weaver-Plugin-AppendPrepend), released on 2024-02-06.
117              
118             =head1 SYNOPSIS
119              
120             In your F<weaver.ini>:
121              
122             [-AppendPrepend]
123             ;exclude_modules = REGEX
124             ;exclude_files = REGEX
125              
126             In your POD:
127              
128             =head1 prepend:FILES
129              
130             foo
131              
132             =head1 append:COPYRIGHT AND LICENSE
133              
134             blah blah blah
135              
136             In the final document, the text 'foo' will be prepended to the FILES section
137             while 'blah blah blah' will be appended to the COPYRIGHT AND LICENSE section.
138             The original prepend:* and append:* sections will be removed.
139              
140             =head1 DESCRIPTION
141              
142             This plugin searches for sections named C<prepend:TARGET> and C<append:TARGET>
143             where I<TARGET> is a section name. The text in C<prepend:*> section will be
144             prepended to the target section, while text in C<append:*> section will be
145             appended to the target section. Target section must exist.
146              
147             This plugin is useful if you have a section generated by other modules but want
148             to add some text to it.
149              
150             =for Pod::Coverage finalize_document
151              
152             =head1 CONFIGURATION
153              
154             =head2 exclude_modules
155              
156             =head2 exclude_files
157              
158             =head2 ignore
159              
160             Bool. Default to true. If set to true (the default), then when target headline
161             does not exist, instead of dying, ignore append/prepend the headline.
162              
163             =head1 HOMEPAGE
164              
165             Please visit the project's homepage at L<https://metacpan.org/release/Pod-Weaver-Plugin-AppendPrepend>.
166              
167             =head1 SOURCE
168              
169             Source repository is at L<https://github.com/perlancar/perl-Pod-Weaver-Plugin-AppendPrepend>.
170              
171             =head1 AUTHOR
172              
173             perlancar <perlancar@cpan.org>
174              
175             =head1 CONTRIBUTING
176              
177              
178             To contribute, you can send patches by email/via RT, or send pull requests on
179             GitHub.
180              
181             Most of the time, you don't need to build the distribution yourself. You can
182             simply modify the code, then test via:
183              
184             % prove -l
185              
186             If you want to build the distribution (e.g. to try to install it locally on your
187             system), you can install L<Dist::Zilla>,
188             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
189             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
190             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
191             that are considered a bug and can be reported to me.
192              
193             =head1 COPYRIGHT AND LICENSE
194              
195             This software is copyright (c) 2024, 2015 by perlancar <perlancar@cpan.org>.
196              
197             This is free software; you can redistribute it and/or modify it under
198             the same terms as the Perl 5 programming language system itself.
199              
200             =head1 BUGS
201              
202             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Pod-Weaver-Plugin-AppendPrepend>
203              
204             When submitting a bug or request, please include a test-file or a
205             patch to an existing test-file that illustrates the bug or desired
206             feature.
207              
208             =cut