File Coverage

blib/lib/Pod/Weaver/Plugin/SingleEncoding.pm
Criterion Covered Total %
statement 51 52 98.0
branch 9 14 64.2
condition 1 3 33.3
subroutine 10 10 100.0
pod 0 2 0.0
total 71 81 87.6


line stmt bran cond sub pod time code
1             package Pod::Weaver::Plugin::SingleEncoding 4.019;
2             # ABSTRACT: ensure that there is exactly one =encoding of known value
3              
4 5     5   32813 use Moose;
  5         15  
  5         48  
5             with(
6             'Pod::Weaver::Role::Dialect',
7             'Pod::Weaver::Role::Finalizer',
8             );
9              
10             # BEGIN BOILERPLATE
11 5     5   37251 use v5.20.0;
  5         19  
12 5     5   41 use warnings;
  5         14  
  5         194  
13 5     5   43 use utf8;
  5         14  
  5         48  
14 5     5   205 no feature 'switch';
  5         13  
  5         660  
15 5     5   49 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  5         13  
  5         67  
16             # END BOILERPLATE
17              
18 5     5   664 use namespace::autoclean;
  5         15  
  5         62  
19              
20 5     5   546 use Pod::Elemental::Selectors -all;
  5         13  
  5         101  
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             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             sub translate_dialect {
52 7     7 0 39 my ($self, $document) = @_;
53              
54 7         20 my $want;
55 7 50       338 $want = $self->encoding if $self->_has_encoding;
56 7 50       34 if ($want) {
57 0         0 $self->log_debug("enforcing encoding of $want in all pod");
58             }
59              
60 7         192 my $childs = $document->children;
61 7         103 my $is_enc = s_command([ qw(encoding) ]);
62              
63 7         103 for (reverse 0 .. $#$childs) {
64 105 100       6472 next unless $is_enc->( $childs->[ $_ ] );
65 2         239 my $have = $childs->[$_]->content;
66 2         20 $have =~ s/\s+\z//;
67              
68 2 100       7 if (defined $want) {
69 1   33     12 my $ok = lc $have eq lc $want
70             || lc $have eq 'utf8' && lc $want eq 'utf-8';
71 1 50       5 confess "expected only $want encoding but found $have" unless $ok;
72             } else {
73 1 50       20 $have = 'UTF-8' if lc $have eq 'utf8';
74 1         45 $self->_set_encoding($have);
75 1         3 $want = $have;
76             }
77              
78 2         99 splice @$childs, $_, 1;
79             }
80              
81 7         429 return;
82             }
83              
84             sub finalize_document {
85 5     5 0 20 my ($self, $document, $input) = @_;
86              
87 5         203 my $encoding = Pod::Elemental::Element::Pod5::Command->new({
88             command => 'encoding',
89             content => $self->encoding,
90             });
91              
92 5         1111 my $childs = $document->children;
93 5         82 my $is_pod = s_command([ qw(pod) ]); # ??
94 5         86 for (0 .. $#$childs) {
95 5 50       27 next if $is_pod->( $childs->[ $_ ] );
96 5         764 $self->log_debug('setting =encoding to ' . $self->encoding);
97 5         175 splice @$childs, $_, 0, $encoding;
98 5         31 last;
99             }
100              
101 5         43 return;
102             }
103              
104             __PACKAGE__->meta->make_immutable;
105             1;
106              
107             __END__
108              
109             =pod
110              
111             =encoding UTF-8
112              
113             =head1 NAME
114              
115             Pod::Weaver::Plugin::SingleEncoding - ensure that there is exactly one =encoding of known value
116              
117             =head1 VERSION
118              
119             version 4.019
120              
121             =head1 OVERVIEW
122              
123             The SingleEncoding plugin is a Dialect and a Finalizer.
124              
125             During dialect translation, it will look for C<=encoding> directives. If it
126             finds them, it will ensure that they all agree on one encoding and remove them.
127              
128             During document finalization, it will insert an C<=encoding> directive at the
129             top of the output, using the encoding previously detected. If no encoding was
130             detected, the plugin's C<encoding> attribute will be used instead. That
131             defaults to UTF-8.
132              
133             If you want to reject any C<=encoding> directive that doesn't match your
134             expectations, set the C<encoding> attribute by hand.
135              
136             No actual validation of the encoding is done. Pod::Weaver, after all, deals in
137             text rather than bytes.
138              
139             =head1 PERL VERSION
140              
141             This module should work on any version of perl still receiving updates from
142             the Perl 5 Porters. This means it should work on any version of perl released
143             in the last two to three years. (That is, if the most recently released
144             version is v5.40, then this module should work on both v5.40 and v5.38.)
145              
146             Although it may work on older versions of perl, no guarantee is made that the
147             minimum required version will not be increased. The version may be increased
148             for any reason, and there is no promise that patches will be accepted to lower
149             the minimum required perl.
150              
151             =head1 AUTHOR
152              
153             Ricardo SIGNES <cpan@semiotic.systems>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2023 by Ricardo SIGNES.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut