blib/lib/Pod/Weaver/Role/AddTextToSection.pm

filter: Statementstmt 22.6% 17 / 75 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 18.4% 23 / 125 CC 22 SCAR 60.2
CC
22
Cov
15%
add_text_to_section
506.0
BEGIN
1.0
BEGIN
1.0
CRAP 412.5
↑ Summary
1 package Pod::Weaver::Role::AddTextToSection;
2  
3 1 our $DATE = '2017-01-14'; # DATE
4 1 our $VERSION = '0.06'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Encode qw(decode encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use Pod::Elemental;
11 1 ▶use Pod::Elemental::Element::Nested;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub add_text_to_section {
Subroutine add_text_to_section: not called CC=22 SCAR=62.3
Pod documented
14 0 my ($self, $document, $text, $section, $opts) = @_;
15  
16 0 ▶ $opts //= {};
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
17 0 ▶ $opts->{create} //= 1;
Condition0%0/2
$opts->{"create"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"create"} //= 1
$opts->{"create"}
Truth table0%0/2
A
A= $opts->{"create"}
Aresult
11
00
18 0 ▶ $opts->{ignore} //= 0;
Condition0%0/2
$opts->{"ignore"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"ignore"} //= 0
$opts->{"ignore"}
Truth table0%0/2
A
A= $opts->{"ignore"}
Aresult
11
00
19 0 ▶ $opts->{top} //= 0;
Condition0%0/2
$opts->{"top"} //= 0
l!l
00
MC/DC0%0/1
$opts->{"top"} //= 0
$opts->{"top"}
Truth table0%0/2
A
A= $opts->{"top"}
Aresult
11
00
20  
21 # convert characters to bytes, which is expected by read_string()
22 0 $text = encode('UTF-8', $text, Encode::FB_CROAK);
23  
24 0 my $text_elem = Pod::Elemental->read_string($text);
25  
26 # dump document
27 #use DD; dd $document->children;
28 #say $document->as_debug_string;
29 #say $document->as_pod_string;
30  
31 # get list of head1 commands with their position in document
32 0 my %headlines_pos;
33 {
34 0 my $i = -1;
35 0 for (@{ $document->children }) {
36 0 $i++;
37 0 ▶ next unless $_->can('command') && $_->command eq 'head1';
Branch: 1 branch
BranchTrueFalseTotal
unless $_->can("command") and $_->command eq "head1" 0 0 0
Condition0%0/3
$_->can("command") and $_->command eq "head1"
!ll&&!rl&&r
000
MC/DC0%0/2
$_->can("command") and $_->command eq "head1"
$_->can("command") $_->command eq "head1"
Truth table0%0/3
A and B
A= $_->can("command")B= $_->command eq "head1"
ABresult
0X0
100
111
38 0 my $name = $_->{content};
39 0 ▶ next if defined $headlines_pos{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $headlines_pos{$name} 0 0 0
40 0 $headlines_pos{$name} = $i;
41 }
42 }
43 #$self->log_debug(["current headlines in the document: %s", \%headlines_pos]);
44  
45 0 my $section_elem;
46 $section_elem = $document->children->[ $headlines_pos{$section} ]
47 0 ▶ if defined $headlines_pos{$section};
Branch: 1 branch
BranchTrueFalseTotal
if defined $headlines_pos{$section} 0 0 0
48  
49 # this comment is from the old code, i'm keeping it here in case i need it
50  
51 # sometimes we get a Pod::Elemental::Element::Pod5::Command (e.g. empty
52 # "=head1 DESCRIPTION") instead of a Pod::Elemental::Element::Nested. in
53 # that case, just ignore it.
54  
55 FIND_OR_CREATE_SECTION:
56 {
57 0 ▶ if (!$section_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $section_elem) { } 0 0 0
58 0 ▶ if ($opts->{create}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'create'}) { } 0 0 0
59 0 $self->log_debug(["Creating section $section"]);
60 0 $section_elem = Pod::Elemental::Element::Nested->new({
61 command => 'head1',
62 content => $section,
63 });
64 0 ▶ if ($opts->{after_section}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"after_section"}) 0 0 0
65 my @sections = ref($opts->{after_section}) eq 'ARRAY' ?
66 0 ▶ @{ $opts->{after_section} } : ($opts->{after_section});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'after_section'} eq 'ARRAY' ? : 0 0 0
67 0 for (@sections) {
68 0 ▶ if (defined $headlines_pos{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headlines_pos{$_}) 0 0 0
69 0 $self->log_debug(["Putting newly created section after section '%s'", $_]);
70 0 splice @{ $document->children }, $headlines_pos{$_}+1, 0, $section_elem;
71 0 last FIND_OR_CREATE_SECTION;
72 }
73 }
74 }
75 0 ▶ if ($opts->{before_section}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"before_section"}) 0 0 0
76 my @sections = ref($opts->{before_section}) eq 'ARRAY' ?
77 0 ▶ @{ $opts->{before_section} } : ($opts->{before_section});
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'before_section'} eq 'ARRAY' ? : 0 0 0
78 0 for (@sections) {
79 0 ▶ if (defined $headlines_pos{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headlines_pos{$_}) 0 0 0
80 0 $self->log_debug(["Putting newly created section before section '%s'", $_]);
81 0 splice @{ $document->children }, $headlines_pos{$_}, 0, $section_elem;
82 0 last FIND_OR_CREATE_SECTION;
83 }
84 }
85 }
86 0 push @{ $document->children }, $section_elem;
87 } else {
88 0 die "Can't find section named '$section' in POD document";
89 }
90 } else {
91 0 $self->log_debug(["Skipping adding text because section $section already exists"]);
92 0 ▶ return 0 if $opts->{ignore};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"ignore"} 0 0 0
93 }
94 }
95  
96 ADD_TEXT:
97 {
98 0 ▶ if ($opts->{top}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'top'}) { } 0 0 0
99 0 $self->log_debug(["Adding text at the top of section $section"]);
100 0 unshift @{ $section_elem->children }, @{ $text_elem->children };
101 } else {
102 0 $self->log_debug(["Adding text at the bottom of section $section"]);
103 0 push @{ $section_elem->children }, @{ $text_elem->children };
104 }
105 }
106 0 return 1;
107 }
108  
109 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 1;
111 # ABSTRACT: Add text to a section
112  
113 __END__
↑ Summary