Skip to content

Commit cb42dc9

Browse files
committed
Add none value for Pod::Text encoding option
Pod::Text now supports setting the encoding option to "none" to disable output encoding. This only makes sense when calling output_string() to direct the formatted output to a scalar. It is intended for special cases where the formatted output is held in memory for further processing and will encoded for output at a later step. Thanks, djerius. Fixes #38
1 parent 44d24f5 commit cb42dc9

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

Changes

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Revision history for podlators
22

3-
v6.0.3 - Not Released
3+
v6.1.0 - Not Released
4+
5+
- Pod::Text now supports setting the encoding option to "none" to disable
6+
output encoding. This only makes sense when calling output_string() to
7+
direct the formatted output to a scalar. It is intended for special
8+
cases where the formatted output is held in memory for further
9+
processing and will encoded for output at a later step. Thanks to
10+
djerius for the suggestion. (GitHub #38)
411

512
- Fix Pod::Text quoting heuristics for C<> when the contents contains
613
newlines. Thanks to van-de-bugger for the report. (GitHub #39)

lib/Pod/Text.pm

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,15 @@ sub output {
313313
$$self{ENCODING} = $encoding;
314314
}
315315
}
316-
if ($encoding) {
316+
if ($encoding && $encoding ne 'none') {
317317
my $check = sub {
318318
my ($char) = @_;
319319
my $display = '"\x{' . hex($char) . '}"';
320320
my $error = "$display does not map to $$self{ENCODING}";
321321
$self->whine ($self->line_count(), $error);
322322
return Encode::encode ($$self{ENCODING}, chr($char));
323323
};
324+
print("ENCODING IN $encoding\n");
324325
print { $$self{output_fh} } encode ($encoding, $text, $check);
325326
} else {
326327
print { $$self{output_fh} } $text;
@@ -921,25 +922,32 @@ with the POD rendered and the code left intact.
921922
922923
=item encoding
923924
924-
[5.00] Specifies the encoding of the output. The value must be an encoding
925-
recognized by the L<Encode> module (see L<Encode::Supported>). If the output
926-
contains characters that cannot be represented in this encoding, that is an
927-
error that will be reported as configured by the C<errors> option. If error
928-
handling is other than C<die>, the unrepresentable character will be replaced
929-
with the Encode substitution character (normally C<?>).
925+
[6.1.0] Specifies the encoding of the output. The value must be an encoding
926+
recognized by the L<Encode> module (see L<Encode::Supported>) or the special
927+
value C<none>. If the output contains characters that cannot be represented in
928+
this encoding, that is an error that will be reported as configured by the
929+
C<errors> option. If error handling is other than C<die>, the unrepresentable
930+
character will be replaced with the Encode substitution character (normally
931+
C<?>).
930932
931933
If the output file handle has a PerlIO encoding layer set, this parameter will
932-
be ignored and no encoding will be done by Pod::Man. It will instead rely on
934+
be ignored and no encoding will be done by Pod::Man. It will instead rely on
933935
the encoding layer to make whatever output encoding transformations are
934936
desired.
935937
938+
As a special case, if the encoding is set to C<none>, no encoding will be done
939+
and characters will be output in Perl's internal representation. This option
940+
only makes sense in combination with output_string(). It is intended for
941+
special cases when the results of formatting are kept in memory and will be
942+
encoded for output at some later step.
943+
936944
WARNING: The input encoding of the POD source is independent from the output
937945
encoding, and setting this option does not affect the interpretation of the
938-
POD input. Unless your POD source is US-ASCII, its encoding should be
939-
declared with the C<=encoding> command in the source, as near to the top of
940-
the file as possible. If this is not done, Pod::Simple will will attempt to
941-
guess the encoding and may be successful if it's Latin-1 or UTF-8, but it will
942-
produce warnings. See L<perlpod(1)> for more information.
946+
POD input. Unless your POD source is US-ASCII, its encoding should be declared
947+
with the C<=encoding> command in the source, as near to the top of the file as
948+
possible. If this is not done, Pod::Simple will will attempt to guess the
949+
encoding and may be successful if it's Latin-1 or UTF-8, but it will produce
950+
warnings. See L<perlpod(1)> for more information.
943951
944952
=item errors
945953
@@ -1078,7 +1086,7 @@ to the scalar variable pointed to by REF, rather than C<STDOUT>. For example:
10781086
$man->parse_file('/some/input/file');
10791087
10801088
Be aware that the output in that variable will already be encoded (see
1081-
L</Encoding>).
1089+
L</Encoding>) unless the C<encoding> option to Pod::Text is set to C<none>.
10821090
10831091
=item parse_file(PATH)
10841092
@@ -1211,6 +1219,8 @@ encoding changes. The L<Encode> module is now used for all output encoding
12111219
rather than PerlIO layers, which fixes earlier problems with output to
12121220
scalars.
12131221
1222+
Pod::Text 6.1.0 added support for the C<none> option to C<encoding>.
1223+
12141224
=head1 CAVEATS
12151225
12161226
Line wrapping is done only at ASCII spaces and tabs, rather than using a
@@ -1226,8 +1236,8 @@ Pod::Simple.
12261236
12271237
=head1 COPYRIGHT AND LICENSE
12281238
1229-
Copyright 1999-2002, 2004, 2006, 2008-2009, 2012-2016, 2018-2019, 2022 Russ
1230-
1239+
Copyright 1999-2002, 2004, 2006, 2008-2009, 2012-2016, 2018-2019, 2022,
1240+
2024-2025 Russ Allbery <[email protected]>
12311241
12321242
This program is free software; you may redistribute it and/or modify it
12331243
under the same terms as Perl itself.

t/text/no-encoding.t

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/perl
2+
#
3+
# Test the "none" encoding option of Pod::Text.
4+
#
5+
# This only makes sense in combination with output_string to produce output
6+
# that's still in Perl's internal encoding.
7+
#
8+
# Copyright 2025 Russ Allbery <[email protected]>
9+
#
10+
# This program is free software; you may redistribute it and/or modify it
11+
# under the same terms as Perl itself.
12+
#
13+
# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
14+
15+
use 5.012;
16+
use utf8;
17+
use warnings;
18+
19+
use Encode qw(encode);
20+
use Test::More tests => 4;
21+
22+
BEGIN {
23+
use_ok('Pod::Text');
24+
}
25+
26+
# Set up Pod::Text to output to a string.
27+
my $parser = Pod::Text->new(encoding => 'none');
28+
isa_ok($parser, 'Pod::Text');
29+
my $output;
30+
$parser->output_string(\$output);
31+
32+
# Parse a document containing UTF-8.
33+
my $input = "=encoding utf-8\n\nrésumé\n";
34+
my $result = eval { $parser->parse_string_document($input) };
35+
ok($result, 'Parsed document');
36+
is($output, " résumé\n\n", 'Produced correct output');

0 commit comments

Comments
 (0)