1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# The encoding detection heuristic will choose UTF8 or Latin-1. The current
# implementation will usually treat CP1252 (aka "Win-Latin-1") as Latin-1 but
# can be fooled into seeing it as UTF8.
#
# Note 1: Neither guess is 'correct' since even if we choose Latin-1, all the
# smart quote symbols will be rendered as control characters
#
# Note 2: the guess is only applied if the source POD omits =encoding, so
# CP1252 source will render correctly if properly declared
#
BEGIN {
if($ENV{PERL_CORE}) {
chdir 't';
@INC = '../lib';
}
}
use strict;
use Test;
BEGIN { plan tests => 5 };
ok 1;
use Pod::Simple::DumpAsXML;
use Pod::Simple::XMLOutStream;
# Initial, isolated, non-ASCII byte triggers Latin-1 guess and later
# multi-byte sequence is not considered by heuristic.
my @output_lines = split m/[\cm\cj]+/, Pod::Simple::XMLOutStream->_out( qq{
=head1 NAME
Em::Dash \x97 \x91CAF\xC9\x92
=cut
} );
my($guess) = "@output_lines" =~ m{Non-ASCII.*?Assuming ([\w-]+)};
if( $guess ) {
if( $guess eq 'ISO8859-1' ) {
if( grep m{Dash (\x97|—|—)}, @output_lines ) {
ok 1;
} else {
ok 0;
print "# failed to find expected control character in output\n"
}
} else {
ok 0;
print "# parser guessed wrong encoding expected 'ISO8859-1' got '$guess'\n";
}
} else {
ok 0;
print "# parser failed to detect non-ASCII bytes in input\n";
}
# Initial smart-quote character triggers Latin-1 guess as expected
@output_lines = split m/[\cm\cj]+/, Pod::Simple::XMLOutStream->_out( qq{
=head1 NAME
Smart::Quote - \x91FUT\xC9\x92
=cut
} );
($guess) = "@output_lines" =~ m{Non-ASCII.*?Assuming ([\w-]+)};
if( $guess ) {
if( $guess eq 'ISO8859-1' ) {
ok 1;
} else {
ok 0;
print "# parser guessed wrong encoding expected 'ISO8859-1' got '$guess'\n";
}
} else {
ok 0;
print "# parser failed to detect non-ASCII bytes in input\n";
}
# Initial accented character followed by 'smart' apostrophe causes heuristic
# to choose UTF8 (a rather contrived example)
@output_lines = split m/[\cm\cj]+/, Pod::Simple::XMLOutStream->_out( qq{
=head1 NAME
Smart::Apostrophe::Fail - L\xC9\x92STRANGE
=cut
} );
($guess) = "@output_lines" =~ m{Non-ASCII.*?Assuming ([\w-]+)};
if( $guess ) {
if( $guess eq 'UTF-8' ) {
ok 1;
} else {
ok 0;
print "# parser guessed wrong encoding expected 'UTF-8' got '$guess'\n";
}
} else {
ok 0;
print "# parser failed to detect non-ASCII bytes in input\n";
}
# The previous example used a CP1252 byte sequence that also happened to be a
# valid UTF8 byte sequence. In this example the heuristic also guesses 'wrong'
# despite the byte sequence not being valid UTF8 (it's too short). This could
# arguably be 'fixed' by using a less naive regex.
@output_lines = split m/[\cm\cj]+/, Pod::Simple::XMLOutStream->_out( qq{
=head1 NAME
Smart::Apostrophe::Fail - L\xE9\x92Strange
=cut
} );
($guess) = "@output_lines" =~ m{Non-ASCII.*?Assuming ([\w-]+)};
if( $guess ) {
if( $guess eq 'UTF-8' ) {
ok 1;
} else {
ok 0;
print "# parser guessed wrong encoding expected 'UTF-8' got '$guess'\n";
}
} else {
ok 0;
print "# parser failed to detect non-ASCII bytes in input\n";
}
exit;
|