| 1 | package Pod::Weaver::Plugin::SingleEncoding 4.022; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: ensure that there is exactly one =encoding of known value | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 5 | with( | |||||||||||||||||||||||||||||||||
| 6 | 'Pod::Weaver::Role::Dialect', | ||||||||||||||||||||||||||||||||||
| 7 | 'Pod::Weaver::Role::Finalizer', | ||||||||||||||||||||||||||||||||||
| 8 | ); | ||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | # BEGIN BOILERPLATE | ||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use v5.22.0; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | no feature 'switch'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use experimental qw(postderef postderef_qq); # This experiment gets mainlined. | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | # END BOILERPLATE | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Pod::Elemental::Selectors -all; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | #pod =head1 OVERVIEW | ||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||
| 24 | #pod The SingleEncoding plugin is a Dialect and a Finalizer. | ||||||||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||||||||
| 26 | #pod During dialect translation, it will look for C<=encoding> directives. If it | ||||||||||||||||||||||||||||||||||
| 27 | #pod finds them, it will ensure that they all agree on one encoding and remove them. | ||||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||||
| 29 | #pod During document finalization, it will insert an C<=encoding> directive at the | ||||||||||||||||||||||||||||||||||
| 30 | #pod top of the output, using the encoding previously detected. If no encoding was | ||||||||||||||||||||||||||||||||||
| 31 | #pod detected, the plugin's C<encoding> attribute will be used instead. That | ||||||||||||||||||||||||||||||||||
| 32 | #pod defaults to UTF-8. | ||||||||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||||||||
| 34 | #pod If you want to reject any C<=encoding> directive that doesn't match your | ||||||||||||||||||||||||||||||||||
| 35 | #pod expectations, set the C<encoding> attribute by hand. | ||||||||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||||||||
| 37 | #pod No actual validation of the encoding is done. Pod::Weaver, after all, deals in | ||||||||||||||||||||||||||||||||||
| 38 | #pod text rather than bytes. | ||||||||||||||||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||||||||||||||||
| 40 | #pod =cut | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | 5 | has encoding => ( | |||||||||||||||||||||||||||||||||
| 43 | reader => 'encoding', | ||||||||||||||||||||||||||||||||||
| 44 | writer => '_set_encoding', | ||||||||||||||||||||||||||||||||||
| 45 | isa => 'Str', | ||||||||||||||||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||||||||||||||||
| 47 | default => 'UTF-8', | ||||||||||||||||||||||||||||||||||
| 48 | predicate => '_has_encoding', | ||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | sub translate_dialect { | ||||||||||||||||||||||||||||||||
|
Subroutine
translate_dialect: called CC=10 SCAR=24.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 52 | 7 | my ($self, $document) = @_; | |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | 7 | my $want; | |||||||||||||||||||||||||||||||||
| 55 | 7 | ▶ | $want = $self->encoding if $self->_has_encoding; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if ($want) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->log_debug("enforcing encoding of $want in all pod"); | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 7 | my $childs = $document->children; | |||||||||||||||||||||||||||||||||
| 61 | 7 | my $is_enc = s_command([ qw(encoding) ]); | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 7 | for (reverse 0 .. $#$childs) { | |||||||||||||||||||||||||||||||||
| 64 | 105 | ▶ | next unless $is_enc->( $childs->[ $_ ] ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 65 | 2 | my $have = $childs->[$_]->content; | |||||||||||||||||||||||||||||||||
| 66 | 2 | $have =~ s/\s+\z//; | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if (defined $want) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | my $ok = lc $have eq lc $want | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
lc $have eq lc $want || lc $have eq 'utf8' && lc $want eq 'utf-8'
lc $have eq "utf8" and lc $want eq "utf-8"
MC/DC0%0/3
lc $have eq lc $want || lc $have eq 'utf8' && lc $want eq 'utf-8'
lc $have eq lc $want
lc $have eq "utf8"
lc $want eq "utf-8"
Truth table25%1/4
A || B and C
A= lc $have eq lc $wantB= lc $have eq "utf8"C= lc $want eq "utf-8"
| |||||||||||||||||||||||||||||||||||
| 70 | || lc $have eq 'utf8' && lc $want eq 'utf-8'; | ||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | confess "expected only $want encoding but found $have" unless $ok; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | $have = 'UTF-8' if lc $have eq 'utf8'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 1 | $self->_set_encoding($have); | |||||||||||||||||||||||||||||||||
| 75 | 1 | $want = $have; | |||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 2 | splice @$childs, $_, 1; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 7 | return; | |||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | sub finalize_document { | ||||||||||||||||||||||||||||||||
|
Subroutine
finalize_document: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 85 | 5 | my ($self, $document, $input) = @_; | |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 5 | my $encoding = Pod::Elemental::Element::Pod5::Command->new({ | |||||||||||||||||||||||||||||||||
| 88 | command => 'encoding', | ||||||||||||||||||||||||||||||||||
| 89 | content => $self->encoding, | ||||||||||||||||||||||||||||||||||
| 90 | }); | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 5 | my $childs = $document->children; | |||||||||||||||||||||||||||||||||
| 93 | 5 | my $is_pod = s_command([ qw(pod) ]); # ?? | |||||||||||||||||||||||||||||||||
| 94 | 5 | for (0 .. $#$childs) { | |||||||||||||||||||||||||||||||||
| 95 | 5 | ▶ | next if $is_pod->( $childs->[ $_ ] ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 96 | 5 | $self->log_debug('setting =encoding to ' . $self->encoding); | |||||||||||||||||||||||||||||||||
| 97 | 5 | splice @$childs, $_, 0, $encoding; | |||||||||||||||||||||||||||||||||
| 98 | 5 | last; | |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | 5 | return; | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 105 | 5 | 1; | |||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||||||||