File Coverage

blib/lib/Pod/Weaver/Plugin/SingleEncoding.pm
Criterion Covered Total %
statement 37 38 97.3
branch 9 14 64.2
condition 1 3 33.3
subroutine 5 5 100.0
pod 0 2 0.0
total 52 62 83.8


line stmt bran cond sub pod time code
1             package Pod::Weaver::Plugin::SingleEncoding;
2             # ABSTRACT: ensure that there is exactly one =encoding of known value
3             $Pod::Weaver::Plugin::SingleEncoding::VERSION = '4.017';
4 5     5   33198 use Moose;
  5         12  
  5         37  
5             with(
6             'Pod::Weaver::Role::Dialect',
7             'Pod::Weaver::Role::Finalizer',
8             );
9              
10 5     5   34757 use namespace::autoclean;
  5         13  
  5         46  
11              
12 5     5   405 use Pod::Elemental::Selectors -all;
  5         10  
  5         73  
13              
14             #pod =head1 OVERVIEW
15             #pod
16             #pod The SingleEncoding plugin is a Dialect and a Finalizer.
17             #pod
18             #pod During dialect translation, it will look for C<=encoding> directives. If it
19             #pod finds them, it will ensure that they all agree on one encoding and remove them.
20             #pod
21             #pod During document finalization, it will insert an C<=encoding> directive at the
22             #pod top of the output, using the encoding previously detected. If no encoding was
23             #pod detected, the plugin's C<encoding> attribute will be used instead. That
24             #pod defaults to UTF-8.
25             #pod
26             #pod If you want to reject any C<=encoding> directive that doesn't match your
27             #pod expectations, set the C<encoding> attribute by hand.
28             #pod
29             #pod No actual validation of the encoding is done. Pod::Weaver, after all, deals in
30             #pod text rather than bytes.
31             #pod
32             #pod =cut
33              
34             has encoding => (
35             reader => 'encoding',
36             writer => '_set_encoding',
37             isa => 'Str',
38             lazy => 1,
39             default => 'UTF-8',
40             predicate => '_has_encoding',
41             );
42              
43             sub translate_dialect {
44 7     7 0 25 my ($self, $document) = @_;
45              
46 7         21 my $want;
47 7 50       367 $want = $self->encoding if $self->_has_encoding;
48 7 50       30 if ($want) {
49 0         0 $self->log_debug("enforcing encoding of $want in all pod");
50             }
51              
52 7         204 my $childs = $document->children;
53 7         110 my $is_enc = s_command([ qw(encoding) ]);
54              
55 7         131 for (reverse 0 .. $#$childs) {
56 105 100       6520 next unless $is_enc->( $childs->[ $_ ] );
57 2         238 my $have = $childs->[$_]->content;
58 2         24 $have =~ s/\s+\z//;
59              
60 2 100       6 if (defined $want) {
61 1   33     18 my $ok = lc $have eq lc $want
62             || lc $have eq 'utf8' && lc $want eq 'utf-8';
63 1 50       4 confess "expected only $want encoding but found $have" unless $ok;
64             } else {
65 1 50       8 $have = 'UTF-8' if lc $have eq 'utf8';
66 1         39 $self->_set_encoding($have);
67 1         4 $want = $have;
68             }
69              
70 2         93 splice @$childs, $_, 1;
71             }
72              
73 7         411 return;
74             }
75              
76             sub finalize_document {
77 5     5 0 18 my ($self, $document, $input) = @_;
78              
79 5         201 my $encoding = Pod::Elemental::Element::Pod5::Command->new({
80             command => 'encoding',
81             content => $self->encoding,
82             });
83              
84 5         1017 my $childs = $document->children;
85 5         59 my $is_pod = s_command([ qw(pod) ]); # ??
86 5         76 for (0 .. $#$childs) {
87 5 50       23 next if $is_pod->( $childs->[ $_ ] );
88 5         724 $self->log_debug('setting =encoding to ' . $self->encoding);
89 5         162 splice @$childs, $_, 0, $encoding;
90 5         16 last;
91             }
92              
93 5         40 return;
94             }
95              
96             __PACKAGE__->meta->make_immutable;
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             Pod::Weaver::Plugin::SingleEncoding - ensure that there is exactly one =encoding of known value
108              
109             =head1 VERSION
110              
111             version 4.017
112              
113             =head1 OVERVIEW
114              
115             The SingleEncoding plugin is a Dialect and a Finalizer.
116              
117             During dialect translation, it will look for C<=encoding> directives. If it
118             finds them, it will ensure that they all agree on one encoding and remove them.
119              
120             During document finalization, it will insert an C<=encoding> directive at the
121             top of the output, using the encoding previously detected. If no encoding was
122             detected, the plugin's C<encoding> attribute will be used instead. That
123             defaults to UTF-8.
124              
125             If you want to reject any C<=encoding> directive that doesn't match your
126             expectations, set the C<encoding> attribute by hand.
127              
128             No actual validation of the encoding is done. Pod::Weaver, after all, deals in
129             text rather than bytes.
130              
131             =head1 AUTHOR
132              
133             Ricardo SIGNES <rjbs@cpan.org>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is copyright (c) 2021 by Ricardo SIGNES.
138              
139             This is free software; you can redistribute it and/or modify it under
140             the same terms as the Perl 5 programming language system itself.
141              
142             =cut