| 1 | package Catalyst::Plugin::Params::Demoronize; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | $Catalyst::Plugin::Params::Demoronize::VERSION = '1.14'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Catalyst::Plugin::Params::Demoronize - convert common UTF-8 and Windows-1252 characters to their ASCII equivalents | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Be sure and use the Unicode plugin if you want to handle Unicode | ||||||||||||||||||||||||||||
| 17 | # replacement. | ||||||||||||||||||||||||||||
| 18 | use Catalyst qw(Unicode Demoronize); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Optionally enable replacement of common unicode "smart" characters. | ||||||||||||||||||||||||||||
| 21 | MyApp->config->{demoronize} = { replace_unicode => 1 } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | to borrow a few passages from the documentation packaged | ||||||||||||||||||||||||||||
| 26 | with john walker's demoronizer.pl: | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | ...as is usually the case when you encounter something | ||||||||||||||||||||||||||||
| 31 | shoddy in the vicinity of a computer, Microsoft incompetence | ||||||||||||||||||||||||||||
| 32 | and gratuitous incompatibility were to blame. Western | ||||||||||||||||||||||||||||
| 33 | language HTML documents are written in the ISO 8859-1 | ||||||||||||||||||||||||||||
| 34 | Latin-1 character set, with a specified set of escapes for | ||||||||||||||||||||||||||||
| 35 | special characters. Blithely ignoring this prescription, as | ||||||||||||||||||||||||||||
| 36 | usual, Microsoft use their own "extension" to Latin-1, in | ||||||||||||||||||||||||||||
| 37 | which a variety of characters which do not appear in Latin-1 | ||||||||||||||||||||||||||||
| 38 | are inserted in the range 0x82 through 0x95--this having the | ||||||||||||||||||||||||||||
| 39 | merit of being incompatible with both Latin-1 and Unicode, | ||||||||||||||||||||||||||||
| 40 | which reserve this region for additional control | ||||||||||||||||||||||||||||
| 41 | characters. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | These characters include open and close single and double | ||||||||||||||||||||||||||||
| 44 | quotes, em and en dashes, an ellipsis and a variety of other | ||||||||||||||||||||||||||||
| 45 | things you've been dying for, such as a capital Y umlaut and | ||||||||||||||||||||||||||||
| 46 | a florin symbol. Well, okay, you say, if Microsoft want to | ||||||||||||||||||||||||||||
| 47 | have their own little incompatible character set, why not? | ||||||||||||||||||||||||||||
| 48 | Because it doesn't stop there--in their inimitable fashion | ||||||||||||||||||||||||||||
| 49 | (who would want to?)--they aggressively pollute the Web | ||||||||||||||||||||||||||||
| 50 | pages of unknowing and innocent victims worldwide with these | ||||||||||||||||||||||||||||
| 51 | characters, with the result that the owners of these pages | ||||||||||||||||||||||||||||
| 52 | look like semi-literate morons when their pages are viewed | ||||||||||||||||||||||||||||
| 53 | on non-Microsoft platforms (or on Microsoft platforms, for | ||||||||||||||||||||||||||||
| 54 | that matter, if the user has selected as the browser's font | ||||||||||||||||||||||||||||
| 55 | one of the many TrueType fonts which do not include the | ||||||||||||||||||||||||||||
| 56 | incompatible Microsoft characters). | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | You see, "state of the art" Microsoft Office applications | ||||||||||||||||||||||||||||
| 59 | sport a nifty feature called "smart quotes." (Rule of | ||||||||||||||||||||||||||||
| 60 | thumb--every time Microsoft use the word "smart," be on the | ||||||||||||||||||||||||||||
| 61 | lookout for something dumb). This feature is on by default | ||||||||||||||||||||||||||||
| 62 | in both Word and PowerPoint, and can be disabled only by | ||||||||||||||||||||||||||||
| 63 | finding the little box buried among the dozens of | ||||||||||||||||||||||||||||
| 64 | bewildering option panels these products contain. If | ||||||||||||||||||||||||||||
| 65 | enabled, and you type the string, | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | "Halt," he cried, "this is the police!" | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | "smart quotes" transforms the ASCII quote characters | ||||||||||||||||||||||||||||
| 70 | automatically into the incompatible Microsoft opening and | ||||||||||||||||||||||||||||
| 71 | closing quotes. ASCII single and double quotes are | ||||||||||||||||||||||||||||
| 72 | similarly transformed (even though ASCII already contains | ||||||||||||||||||||||||||||
| 73 | apostrophe and single open quote characters), and double | ||||||||||||||||||||||||||||
| 74 | hyphens are replaced by the incompatible em dash symbol. | ||||||||||||||||||||||||||||
| 75 | What other horrors occur, I know not. If the user notices | ||||||||||||||||||||||||||||
| 76 | this happening at all, their reaction might be "Thank you | ||||||||||||||||||||||||||||
| 77 | Billy-boy--that looks ever so much nicer," not knowing | ||||||||||||||||||||||||||||
| 78 | they've been set up to look like a moron to folks all over | ||||||||||||||||||||||||||||
| 79 | the world. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =back | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | these characters are commonly inserted into form elements | ||||||||||||||||||||||||||||
| 84 | via cut and paste operations. in many cases, they are | ||||||||||||||||||||||||||||
| 85 | converted to UTF-8 by the browser. this plugin will replace | ||||||||||||||||||||||||||||
| 86 | both the unicode characters AND the Windows-1252 characters | ||||||||||||||||||||||||||||
| 87 | with sane ASCII equivalents. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 UNICODE | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Demoronize assumes that you are using L<Catalyst::Plugin::Unicode> | ||||||||||||||||||||||||||||
| 92 | to convert incoming parameters into Unicode characters. If you are | ||||||||||||||||||||||||||||
| 93 | not and enable optional C<replace_unicode>, you may have issues. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 CONFIG | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head2 replace_unicode | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | If this flag is enabled (it is off by default) then commonly substituted | ||||||||||||||||||||||||||||
| 100 | Unicode characters will be converted to their ASCII equivalents. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 replace_map | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | A map of Unicode characters and their ASCII equivalents that will be swapped. | ||||||||||||||||||||||||||||
| 105 | This can be overridden, but defaults to: | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | use Encode::ZapCP1252; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 METHODS | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =over 4 | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =item prepare_parameters | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Converts parameters. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub prepare_parameters | ||||||||||||||||||||||||||||
| 123 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prepare_parameters: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 7 | my $c = shift; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 7 | my $retval = $c->maybe::next::method(@_); | |||||||||||||||||||||||||||
| 127 | 7 | my $params = $c->req->params; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 7 | foreach my $key (keys %$params) { | |||||||||||||||||||||||||||
| 130 | 7 | my $ref = \$params->{$key}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 7 | for (ref $$ref) { | |||||||||||||||||||||||||||
| 133 | 7 | ▶ | /^$/ && do { $$ref = $c->_demoronize($$ref) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 7 | ▶ | /^ARRAY$/ && do { $$ref = [ map { $c->_demoronize($_) } @$$ref ] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | sub _demoronize | ||||||||||||||||||||||||||||
| 140 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_demoronize: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 141 | 9 | my $c = shift; | |||||||||||||||||||||||||||
| 142 | 9 | my $str = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 9 | zap_cp1252($str); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | ▶ | my $config = $c->config->{'demoronize'} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$c->config->{"demoronize"} ||= {}
MC/DC100%1/1
$c->config->{"demoronize"} ||= {}
$c->config->{"demoronize"}
Truth table100%2/2
A
A= $c->config->{"demoronize"}
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $config->{replace_map} = { | ||||||||||||||||||||||||||||
| 149 | 9 | '‚' => ',', # 82, SINGLE LOW-9 QUOTATION MARK | |||||||||||||||||||||||||||
| 150 | '„' => ',,', # 84, DOUBLE LOW-9 QUOTATION MARK | ||||||||||||||||||||||||||||
| 151 | '…' => '...', # 85, HORIZONTAL ELLIPSIS | ||||||||||||||||||||||||||||
| 152 | 'ˆ' => '^', # 88, MODIFIER LETTER CIRCUMFLEX ACCENT | ||||||||||||||||||||||||||||
| 153 | '‘' => '`', # 91, LEFT SINGLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 154 | '’' => "'", # 92, RIGHT SINGLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 155 | '“' => '"', # 93, LEFT DOUBLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 156 | '”' => '"', # 94, RIGHT DOUBLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 157 | '•' => '*', # 95, BULLET | ||||||||||||||||||||||||||||
| 158 | '–' => '-', # 96, EN DASH | ||||||||||||||||||||||||||||
| 159 | '—' => '-', # 97, EM DASH | ||||||||||||||||||||||||||||
| 160 | '‹' => '<', # 8B, SINGLE LEFT-POINTING ANGLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 161 | '›' => '>', # 9B, SINGLE RIGHT-POINTING ANGLE QUOTATION MARK | ||||||||||||||||||||||||||||
| 162 | }; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 9 | ▶ | if(exists($config->{'replace_unicode'}) && $config->{'replace_unicode'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $config->{"replace_unicode"} and $config->{"replace_unicode"}
MC/DC50%1/2
exists $config->{"replace_unicode"} and $config->{"replace_unicode"}
exists $config->{"replace_unicode"}
$config->{"replace_unicode"}
Truth table66%2/3
A and B
A= exists $config->{"replace_unicode"}B= $config->{"replace_unicode"}
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 3 | foreach my $replace (keys(%{ $config->{replace_map} })) { | |||||||||||||||||||||||||||
| 167 | 39 | ▶ | next unless defined($str); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 39 | $str =~ s/$replace/$config->{replace_map}->{$replace}/g; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 9 | return $str; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =back | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Mike Eldridge <diz@cpan.org> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 CONTRIBUTORS | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =over 4 | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * Cory Watson <gphat@cpan.org> | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item * Chisel Wright <chisel@cpan.org> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =item * Michele Beltrame <arthas@cpan.org> | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 3 | 1; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||