Skip to content

Commit cb3f1a9

Browse files
Merge pull request #32 from Perl-Toolchain-Gang/decode_pod
Add a decode_pod option for automatic =encoding handling
2 parents a5cd764 + 78cfe34 commit cb3f1a9

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Revision history for Module-Metadata
22

33
{{$NEXT}}
4+
- add decode_pod option for automatic =encoding handling
45

56
1.000036 2019-04-18 18:25:15Z
67
- properly clean up temp dirs after testing

lib/Module/Metadata.pm

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ sub _init {
383383

384384
my $handle = delete $props{handle};
385385
my( %valid_props, @valid_props );
386-
@valid_props = qw( collect_pod inc );
386+
@valid_props = qw( collect_pod inc decode_pod );
387387
@valid_props{@valid_props} = delete( @props{@valid_props} );
388388
warn "Unknown properties: @{[keys %props]}\n" if scalar( %props );
389389

@@ -542,6 +542,7 @@ sub _parse_fh {
542542
my $pod_sect = '';
543543
my $pod_data = '';
544544
my $in_end = 0;
545+
my $encoding = '';
545546

546547
while (defined( my $line = <$fh> )) {
547548
my $line_num = $.;
@@ -570,6 +571,9 @@ sub _parse_fh {
570571
$pod_sect = $1;
571572
}
572573
elsif ( $self->{collect_pod} ) {
574+
if ( $self->{decode_pod} && $line =~ /^=encoding ([\w-]+)/ ) {
575+
$encoding = $1;
576+
}
573577
$pod_data .= "$line\n";
574578
}
575579
next;
@@ -658,6 +662,11 @@ sub _parse_fh {
658662
$pod{$pod_sect} = $pod_data;
659663
}
660664

665+
if ( $self->{decode_pod} && $encoding ) {
666+
require Encode;
667+
$_ = Encode::decode( $encoding, $_ ) for values %pod;
668+
}
669+
661670
$self->{versions} = \%vers;
662671
$self->{packages} = \@packages;
663672
$self->{pod} = \%pod;
@@ -854,7 +863,7 @@ in the CPAN toolchain.
854863
855864
=head1 CLASS METHODS
856865
857-
=head2 C<< new_from_file($filename, collect_pod => 1) >>
866+
=head2 C<< new_from_file($filename, collect_pod => 1, decode_pod => 1) >>
858867
859868
Constructs a C<Module::Metadata> object given the path to a file. Returns
860869
undef if the filename does not exist.
@@ -867,7 +876,10 @@ If the file begins by an UTF-8, UTF-16BE or UTF-16LE byte-order mark, then
867876
it is skipped before processing, and the content of the file is also decoded
868877
appropriately starting from perl 5.8.
869878
870-
=head2 C<< new_from_handle($handle, $filename, collect_pod => 1) >>
879+
Alternatively, if C<decode_pod> is set, it will decode the collected pod
880+
sections according to the C<=encoding> declaration.
881+
882+
=head2 C<< new_from_handle($handle, $filename, collect_pod => 1, decode_pod => 1) >>
871883
872884
This works just like C<new_from_file>, except that a handle can be provided
873885
as the first argument.
@@ -880,15 +892,15 @@ mandatory or undef will be returned.
880892
You are responsible for setting the decoding layers on C<$handle> if
881893
required.
882894
883-
=head2 C<< new_from_module($module, collect_pod => 1, inc => \@dirs) >>
895+
=head2 C<< new_from_module($module, collect_pod => 1, inc => \@dirs, decode_pod => 1) >>
884896
885897
Constructs a C<Module::Metadata> object given a module or package name.
886898
Returns undef if the module cannot be found.
887899
888-
In addition to accepting the C<collect_pod> argument as described above,
889-
this method accepts a C<inc> argument which is a reference to an array of
890-
directories to search for the module. If none are given, the default is
891-
@INC.
900+
In addition to accepting the C<collect_pod> and C<decode_pod> arguments as
901+
described above, this method accepts a C<inc> argument which is a reference to
902+
an array of directories to search for the module. If none are given, the
903+
default is @INC.
892904
893905
If the file that contains the module begins by an UTF-8, UTF-16BE or
894906
UTF-16LE byte-order mark, then it is skipped before processing, and the

t/metadata.t

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use strict;
55
use warnings;
6+
use Encode 'decode';
67
use Test::More 0.82;
78
use IO::File;
89
use File::Spec;
@@ -16,7 +17,7 @@ use GeneratePackage;
1617

1718
my $tmpdir = GeneratePackage::tmpdir();
1819

19-
plan tests => 71;
20+
plan tests => 72;
2021

2122
require_ok('Module::Metadata');
2223

@@ -209,13 +210,15 @@ $VERSION = '0.01';
209210
package Simple::Ex;
210211
$VERSION = '0.02';
211212
213+
=encoding UTF-8
214+
212215
=head1 NAME
213216
214217
Simple - It's easy.
215218
216219
=head1 AUTHOR
217220
218-
Simple Simon
221+
Símple Simon
219222
220223
You can find me on the IRC channel
221224
#simon on irc.perl.org.
@@ -270,7 +273,7 @@ You can find me on the IRC channel
270273
my %expected = (
271274
NAME => q|Simple - It's easy.|,
272275
AUTHOR => <<'EXPECTED'
273-
Simple Simon
276+
Símple Simon
274277
275278
You can find me on the IRC channel
276279
#simon on irc.perl.org.
@@ -282,6 +285,13 @@ EXPECTED
282285
}
283286
is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
284287
is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
288+
289+
my $pm_info2 = Module::Metadata->new_from_module(
290+
'Simple', inc => [ 'lib', @INC ], collect_pod => 1, decode_pod => 1 );
291+
my $author = $pm_info2->pod( 'AUTHOR' );
292+
$author =~ s/^\s+//;
293+
$author =~ s/\s+$//;
294+
is( $author, decode('UTF-8', $expected{AUTHOR} ), 'collected AUTHOR pod section in UTF-8' );
285295
}
286296

287297
{

0 commit comments

Comments
 (0)