This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Show diagnostic link format in perldelta template
[perl5.git] / Porting / checkpodencoding.pl
1 #!/usr/bin/env perl
2 use 5.010;
3 use open qw< :encoding(utf8) :std >;
4 use autodie;
5 use strict;
6 use File::Find;
7 use Encode::Guess;
8
9 # Check if POD files contain non-ASCII without specifying
10 # =encoding. Run it as:
11
12 ## perl Porting/checkpodencoding.pl
13
14 find(
15     {
16         wanted => \&finder,
17         no_chdir => 1,
18     },
19     '.'
20 );
21
22 sub finder {
23     my $file = $_;
24
25     return if -B $file;
26
27     open my $fh, '<', $file;
28
29     #say STDERR "Checking $file";
30
31     next if
32         # Test cases
33         $file ~~ m[Pod-Simple/t];
34
35     my ($in_pod, $has_encoding, @non_ascii);
36
37     FILE: while (my $line = <$fh>) {
38         chomp $line;
39         if ($line ~~ /^=[a-z]+/) {
40             $in_pod = 1;
41         }
42
43         if ($in_pod) {
44             if ($line ~~ /^=encoding (\S+)/) {
45                 $has_encoding = 1;
46                 last FILE;
47             } elsif ($line ~~ /[^[:ascii:]]/) {
48                 my $encoding = guess_encoding($line);
49                 push @non_ascii => {
50                     num => $.,
51                     line => $line,
52                     encoding => (ref $encoding ? "$encoding->{Name}?" : 'unknown!'),
53                 };
54             }
55         }
56
57         if ($line ~~ /^=cut/) {
58             $in_pod = 0;
59         }
60     }
61
62     if (@non_ascii and not $has_encoding) {
63         say "$file:";
64         $DB::single = 1;
65         for (@non_ascii) {
66             say "    $_->{num} ($_->{encoding}): $_->{line}";
67         }
68     }
69 }