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.018;
2             # ABSTRACT: ensure that there is exactly one =encoding of known value
3              
4 5     5   30495 use Moose;
  5         14  
  5         46  
5             with(
6             'Pod::Weaver::Role::Dialect',
7             'Pod::Weaver::Role::Finalizer',
8             );
9              
10             # BEGIN BOILERPLATE
11 5     5   33649 use v5.20.0;
  5         18  
12 5     5   31 use warnings;
  5         10  
  5         203  
13 5     5   34 use utf8;
  5         11  
  5         39  
14 5     5   153 no feature 'switch';
  5         10  
  5         620  
15 5     5   37 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  5         18  
  5         57  
16             # END BOILERPLATE
17              
18 5     5   564 use namespace::autoclean;
  5         12  
  5         52  
19              
20 5     5   472 use Pod::Elemental::Selectors -all;
  5         20  
  5         60  
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 50 my ($self, $document) = @_;
53              
54 7         24 my $want;
55 7 50       310 $want = $self->encoding if $self->_has_encoding;
56 7 50       32 if ($want) {
57 0         0 $self->log_debug("enforcing encoding of $want in all pod");
58             }
59              
60 7         183 my $childs = $document->children;
61 7         117 my $is_enc = s_command([ qw(encoding) ]);
62              
63 7         107 for (reverse 0 .. $#$childs) {
64 105 100       6235 next unless $is_enc->( $childs->[ $_ ] );
65 2         200 my $have = $childs->[$_]->content;
66 2         18 $have =~ s/\s+\z//;
67              
68 2 100       6 if (defined $want) {
69 1   33     22 my $ok = lc $have eq lc $want
70             || lc $have eq 'utf8' && lc $want eq 'utf-8';
71 1 50       4 confess "expected only $want encoding but found $have" unless $ok;
72             } else {
73 1 50       5 $have = 'UTF-8' if lc $have eq 'utf8';
74 1         45 $self->_set_encoding($have);
75 1         2 $want = $have;
76             }
77              
78 2         94 splice @$childs, $_, 1;
79             }
80              
81 7         446 return;
82             }
83              
84             sub finalize_document {
85 5     5 0 22 my ($self, $document, $input) = @_;
86              
87 5         189 my $encoding = Pod::Elemental::Element::Pod5::Command->new({
88             command => 'encoding',
89             content => $self->encoding,
90             });
91              
92 5         991 my $childs = $document->children;
93 5         71 my $is_pod = s_command([ qw(pod) ]); # ??
94 5         92 for (0 .. $#$childs) {
95 5 50       24 next if $is_pod->( $childs->[ $_ ] );
96 5         789 $self->log_debug('setting =encoding to ' . $self->encoding);
97 5         149 splice @$childs, $_, 0, $encoding;
98 5         17 last;
99             }
100              
101 5         39 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.018
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 SUPPORT
140              
141             This module has the same support period as perl itself: it supports the two
142             most recent versions of perl. (That is, if the most recently released version
143             is v5.40, then this module should work on both v5.40 and v5.38.)
144              
145             Although it may work on older versions of perl, no guarantee is made that the
146             minimum required version will not be increased. The version may be increased
147             for any reason, and there is no promise that patches will be accepted to lower
148             the minimum required perl.
149              
150             =head1 AUTHOR
151              
152             Ricardo SIGNES <rjbs@semiotic.systems>
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is copyright (c) 2021 by Ricardo SIGNES.
157              
158             This is free software; you can redistribute it and/or modify it under
159             the same terms as the Perl 5 programming language system itself.
160              
161             =cut