| 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 //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | $opts->{create} //= 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"create"} //= 1
MC/DC0%0/1
$opts->{"create"} //= 1
$opts->{"create"}
Truth table0%0/2
A
A= $opts->{"create"}
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $opts->{ignore} //= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"ignore"} //= 0
MC/DC0%0/1
$opts->{"ignore"} //= 0
$opts->{"ignore"}
Truth table0%0/2
A
A= $opts->{"ignore"}
| |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $opts->{top} //= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"top"} //= 0
MC/DC0%0/1
$opts->{"top"} //= 0
$opts->{"top"}
Truth table0%0/2
A
A= $opts->{"top"}
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$_->can("command") and $_->command eq "head1"
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"
| |||||||||||||||||||||||||||||
| 38 | 0 | my $name = $_->{content}; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | next if defined $headlines_pos{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($opts->{create}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | my @sections = ref($opts->{after_section}) eq 'ARRAY' ? | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | @{ $opts->{after_section} } : ($opts->{after_section}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | for (@sections) { | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (defined $headlines_pos{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 76 | my @sections = ref($opts->{before_section}) eq 'ARRAY' ? | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | @{ $opts->{before_section} } : ($opts->{before_section}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | for (@sections) { | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (defined $headlines_pos{$_}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ADD_TEXT: | ||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($opts->{top}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||