| 1 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | package Pod::Weaver::Section::AllowOverride; | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright 2012 Christopher J. Madsen | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Author: Christopher J. Madsen <perl@cjmweb.net> | ||||||||||||||||||||||||||||
| 7 | # Created: 31 May 2012 | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 10 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the | ||||||||||||||||||||||||||||
| 15 | # GNU General Public License or the Artistic License for more details. | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # ABSTRACT: Allow POD to override a Pod::Weaver-provided section | ||||||||||||||||||||||||||||
| 18 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | with qw(Pod::Weaver::Role::Transformer Pod::Weaver::Role::Section); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 25 | # This file is part of Pod-Weaver-Section-AllowOverride 0.05 (May 5, 2014) | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use Pod::Elemental::MakeSelector qw(make_selector); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | #===================================================================== | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has header_re => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||||||||||||
| 37 | lazy => 1, | ||||||||||||||||||||||||||||
| 38 | 10 | ▶ | default => sub {'^' . quotemeta(shift->plugin_name) . '$' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 2 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | has action => ( | |||||||||||||||||||||||||||
| 43 | is => 'ro', | ||||||||||||||||||||||||||||
| 44 | isa => enum([ qw(replace prepend append) ]), | ||||||||||||||||||||||||||||
| 45 | default => 'replace', | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | has match_anywhere => ( | |||||||||||||||||||||||||||
| 50 | is => 'ro', | ||||||||||||||||||||||||||||
| 51 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 52 | default => 0, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | has _override_with => ( | |||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||
| 57 | does => 'Pod::Elemental::Node', | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 61 | # Return a sub that matches a node against header_re: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | has _section_matcher => ( | |||||||||||||||||||||||||||
| 64 | is => 'ro', | ||||||||||||||||||||||||||||
| 65 | isa => 'CodeRef', | ||||||||||||||||||||||||||||
| 66 | lazy => 1, | ||||||||||||||||||||||||||||
| 67 | builder => '_build_section_matcher', | ||||||||||||||||||||||||||||
| 68 | init_arg => undef, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub _build_section_matcher | ||||||||||||||||||||||||||||
| 72 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_section_matcher: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 10 | my $header_re = $self->header_re; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 10 | return make_selector(qw(-command head1 -content) => qr/$header_re/); | |||||||||||||||||||||||||||
| 78 | } # end _build_section_matcher | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 81 | # Look for a matching section in the original POD, and remove it temporarily: | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub transform_document | ||||||||||||||||||||||||||||
| 84 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
transform_document: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 10 | my ($self, $document) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 10 | my $match = $self->_section_matcher; | |||||||||||||||||||||||||||
| 88 | 10 | my $children = $document->children; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 10 | for my $i (0 .. $#$children) { | |||||||||||||||||||||||||||
| 91 | 9 | ▶ | if ($match->( $children->[$i] )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | # Found matching section. Store & remove it: | ||||||||||||||||||||||||||||
| 93 | 9 | $self->_override_with( splice @$children, $i, 1 ); | |||||||||||||||||||||||||||
| 94 | 9 | last; | |||||||||||||||||||||||||||
| 95 | } # end if this is the section we're looking for | ||||||||||||||||||||||||||||
| 96 | } # end for $i indexing @$children | ||||||||||||||||||||||||||||
| 97 | } # end transform_document | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | #--------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 100 | # If we found a section in the original POD, | ||||||||||||||||||||||||||||
| 101 | # use it instead of the one now in the document: | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub weave_section | ||||||||||||||||||||||||||||
| 104 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
weave_section: called CC=12 SCAR=25.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 10 | my ($self, $document, $input) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 10 | my $override = $self->_override_with; | |||||||||||||||||||||||||||
| 108 | 10 | ▶ | return unless $override; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 9 | my $section_matcher = $self->_section_matcher; | |||||||||||||||||||||||||||
| 111 | 9 | my $children = $document->children; | |||||||||||||||||||||||||||
| 112 | 9 | my $prev; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 9 | ▶ | if ($self->match_anywhere) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 6 | my $pos = @$children; | |||||||||||||||||||||||||||
| 116 | 6 | while (1) { | |||||||||||||||||||||||||||
| 117 | 9 | ▶ | $self->log_fatal(["No section matching /%s/", $self->header_re]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | unless $pos--; | ||||||||||||||||||||||||||||
| 119 | 9 | ▶ | last if $section_matcher->( $children->[$pos] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 6 | $prev = splice @$children, $pos, 1, $override; | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 3 | ▶ | if (@$children and $section_matcher->( $children->[-1] )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@$children and &$section_matcher($children->[-1])
MC/DC0%0/2
@$children and &$section_matcher($children->[-1])
@$children
&$section_matcher($children->[-1])
Truth table33%1/3
A and B
A= @$childrenB= &$section_matcher($children->[-1])
| |||||||||||||||||||||||||||||
| 124 | 3 | $prev = pop @$children; | |||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||
| 126 | 0 | $self->log(["The previous section did not match /%s/, won't override it", | |||||||||||||||||||||||||||
| 127 | $self->header_re]); | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 3 | push @$children, $override; | |||||||||||||||||||||||||||
| 131 | } # end else must override immediately preceding section | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 9 | for my $action ($self->action) { | |||||||||||||||||||||||||||
| 134 | 9 | ▶ | last if $action eq 'replace' or not $prev; # nothing more to do | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$action eq "replace" or not $prev
MC/DC50%1/2
$action eq "replace" or not $prev
$action eq "replace"
not $prev
Truth table66%2/3
A or not(B)
A= $action eq "replace"B= not $prev
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 6 | my $prev_content = $prev->children; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | ▶ | if ( $action eq 'prepend') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 139 | 3 | push @{ $override->children }, @$prev_content | |||||||||||||||||||||||||||
| 140 | } elsif ($action eq 'append') { | ||||||||||||||||||||||||||||
| 141 | 3 | unshift @{ $override->children }, @$prev_content | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } # end for $self->action | ||||||||||||||||||||||||||||
| 144 | } # end weave_section | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | #===================================================================== | ||||||||||||||||||||||||||||
| 147 | # Package Return Value: | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 150 | 2 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||