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.020;
2             # ABSTRACT: ensure that there is exactly one =encoding of known value
3              
4 5     5   43496 use Moose;
  5         88  
  5         53  
5             with(
6             'Pod::Weaver::Role::Dialect',
7             'Pod::Weaver::Role::Finalizer',
8             );
9              
10             # BEGIN BOILERPLATE
11 5     5   44337 use v5.20.0;
  5         20  
12 5     5   32 use warnings;
  5         13  
  5         353  
13 5     5   38 use utf8;
  5         9  
  5         49  
14 5     5   270 no feature 'switch';
  5         23  
  5         1045  
15 5     5   115 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  5         18  
  5         56  
16             # END BOILERPLATE
17              
18 5     5   629 use namespace::autoclean;
  5         20  
  5         62  
19              
20 5     5   618 use Pod::Elemental::Selectors -all;
  5         12  
  5         72  
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 27 my ($self, $document) = @_;
53              
54 7         18 my $want;
55 7 50       497 $want = $self->encoding if $self->_has_encoding;
56 7 50       49 if ($want) {
57 0         0 $self->log_debug("enforcing encoding of $want in all pod");
58             }
59              
60 7         330 my $childs = $document->children;
61 7         95 my $is_enc = s_command([ qw(encoding) ]);
62              
63 7         123 for (reverse 0 .. $#$childs) {
64 105 100       7730 next unless $is_enc->( $childs->[ $_ ] );
65 2         317 my $have = $childs->[$_]->content;
66 2         22 $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       6 $have = 'UTF-8' if lc $have eq 'utf8';
74 1         46 $self->_set_encoding($have);
75 1         2 $want = $have;
76             }
77              
78 2         97 splice @$childs, $_, 1;
79             }
80              
81 7         495 return;
82             }
83              
84             sub finalize_document {
85 5     5 0 22 my ($self, $document, $input) = @_;
86              
87 5         317 my $encoding = Pod::Elemental::Element::Pod5::Command->new({
88             command => 'encoding',
89             content => $self->encoding,
90             });
91              
92 5         1358 my $childs = $document->children;
93 5         68 my $is_pod = s_command([ qw(pod) ]); # ??
94 5         132 for (0 .. $#$childs) {
95 5 50       25 next if $is_pod->( $childs->[ $_ ] );
96 5         1020 $self->log_debug('setting =encoding to ' . $self->encoding);
97 5         227 splice @$childs, $_, 0, $encoding;
98 5         15 last;
99             }
100              
101 5         51 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.020
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
143             released in the last two to three years. (That is, if the most recently
144             released version is v5.40, then this module should work on both v5.40 and
145             v5.38.)
146              
147             Although it may work on older versions of perl, no guarantee is made that the
148             minimum required version will not be increased. The version may be increased
149             for any reason, and there is no promise that patches will be accepted to
150             lower the minimum required perl.
151              
152             =head1 AUTHOR
153              
154             Ricardo SIGNES <cpan@semiotic.systems>
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2024 by Ricardo SIGNES.
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut