File Coverage

blib/lib/Pod/Escapes.pm
Criterion Covered Total %
statement 28 31 90.3
branch 16 22 72.7
condition 3 12 25.0
subroutine 6 6 100.0
pod 2 2 100.0
total 55 73 75.3


line stmt bran cond sub pod time code
1             package Pod::Escapes;
2 3     3   162543 use strict;
  3         18  
  3         70  
3 3     3   13 use warnings;
  3         4  
  3         61  
4 3     3   48 use 5.006;
  3         8  
5              
6             our %Code2USASCII;
7             our %Name2character;
8             our %Name2character_number;
9             our %Latin1Code_to_fallback;
10             our %Latin1Char_to_fallback;
11              
12 3     3   16 use Exporter ();
  3         9  
  3         3916  
13             our @ISA = ('Exporter');
14             our $VERSION = '1.07_01';
15             our @EXPORT_OK = qw(
16             %Code2USASCII
17             %Name2character
18             %Name2character_number
19             %Latin1Code_to_fallback
20             %Latin1Char_to_fallback
21             e2char
22             e2charnum
23             );
24             our %EXPORT_TAGS = ('ALL' => \@EXPORT_OK);
25              
26             #==========================================================================
27              
28             our $FAR_CHAR = "?" unless defined $FAR_CHAR;
29             our $FAR_CHAR_NUMBER = ord($FAR_CHAR) unless defined $FAR_CHAR_NUMBER;
30              
31             our $NOT_ASCII = 'A' ne chr(65) unless defined $NOT_ASCII;
32              
33             #--------------------------------------------------------------------------
34             sub e2char {
35 61     61 1 2694 my $in = $_[0];
36 61 50 33     192 return undef unless defined $in and length $in;
37              
38             # Convert to decimal:
39 61 100       232 if($in =~ m/^(0[0-7]*)$/s ) {
    100          
40 12         21 $in = oct $in;
41             } elsif($in =~ m/^0?x([0-9a-fA-F]+)$/s ) {
42 23         54 $in = hex $1;
43             } # else it's decimal, or named
44              
45 61 100       152 if($in =~ m/^\d+$/s) {
46 40 50 33     102 if($] < 5.007 and $in > 255) { # can't be trusted with Unicode
    50          
    0          
47 0         0 return $FAR_CHAR;
48             } elsif ($] >= 5.007003) {
49 40         148 return chr(utf8::unicode_to_native($in));
50             } elsif ($NOT_ASCII) {
51             return $Code2USASCII{$in} # so "65" => "A" everywhere
52 0   0     0 || $Latin1Code_to_fallback{$in} # Fallback.
53             || $FAR_CHAR; # Fall further back
54             } else {
55 0         0 return chr($in);
56             }
57             } else {
58 21         80 return $Name2character{$in}; # returns undef if unknown
59             }
60             }
61              
62             #--------------------------------------------------------------------------
63             sub e2charnum {
64 56     56 1 235 my $in = $_[0];
65 56 50 33     175 return undef unless defined $in and length $in;
66              
67             # Convert to decimal:
68 56 100       217 if($in =~ m/^(0[0-7]*)$/s ) {
    100          
69 12         21 $in = oct $in;
70             } elsif($in =~ m/^0?x([0-9a-fA-F]+)$/s ) {
71 17         43 $in = hex $1;
72             } # else it's decimal, or named
73              
74 56 100       138 if($in =~ m/^[0-9]+$/s) {
75 34         121 return 0 + $in;
76             } else {
77 22         82 return $Name2character_number{$in}; # returns undef if unknown
78             }
79             }
80              
81             #--------------------------------------------------------------------------
82              
83             %Code2USASCII = (
84             # mostly generated by
85             # perl -e "printf qq{ \x25 3s, '\x25s',\n}, $_, chr($_) foreach (32 .. 126)"
86             32, ' ',
87             33, '!',
88             34, '"',
89             35, '#',
90             36, '$',
91             37, '%',
92             38, '&',
93             39, "'", #!
94             40, '(',
95             41, ')',
96             42, '*',
97             43, '+',
98             44, ',',
99             45, '-',
100             46, '.',
101             47, '/',
102             48, '0',
103             49, '1',
104             50, '2',
105             51, '3',
106             52, '4',
107             53, '5',
108             54, '6',
109             55, '7',
110             56, '8',
111             57, '9',
112             58, ':',
113             59, ';',
114             60, '<',
115             61, '=',
116             62, '>',
117             63, '?',
118             64, '@',
119             65, 'A',
120             66, 'B',
121             67, 'C',
122             68, 'D',
123             69, 'E',
124             70, 'F',
125             71, 'G',
126             72, 'H',
127             73, 'I',
128             74, 'J',
129             75, 'K',
130             76, 'L',
131             77, 'M',
132             78, 'N',
133             79, 'O',
134             80, 'P',
135             81, 'Q',
136             82, 'R',
137             83, 'S',
138             84, 'T',
139             85, 'U',
140             86, 'V',
141             87, 'W',
142             88, 'X',
143             89, 'Y',
144             90, 'Z',
145             91, '[',
146             92, "\\", #!
147             93, ']',
148             94, '^',
149             95, '_',
150             96, '`',
151             97, 'a',
152             98, 'b',
153             99, 'c',
154             100, 'd',
155             101, 'e',
156             102, 'f',
157             103, 'g',
158             104, 'h',
159             105, 'i',
160             106, 'j',
161             107, 'k',
162             108, 'l',
163             109, 'm',
164             110, 'n',
165             111, 'o',
166             112, 'p',
167             113, 'q',
168             114, 'r',
169             115, 's',
170             116, 't',
171             117, 'u',
172             118, 'v',
173             119, 'w',
174             120, 'x',
175             121, 'y',
176             122, 'z',
177             123, '{',
178             124, '|',
179             125, '}',
180             126, '~',
181             );
182              
183             #--------------------------------------------------------------------------
184              
185             %Latin1Code_to_fallback = ();
186             @Latin1Code_to_fallback{0xA0 .. 0xFF} = (
187             # Copied from Text/Unidecode/x00.pm:
188              
189             ' ', qq{!}, qq{C/}, 'PS', qq{\$?}, qq{Y=}, qq{|}, 'SS', qq{"}, qq{(c)}, 'a', qq{<<}, qq{!}, "", qq{(r)}, qq{-},
190             'deg', qq{+-}, '2', '3', qq{'}, 'u', 'P', qq{*}, qq{,}, '1', 'o', qq{>>}, qq{1/4}, qq{1/2}, qq{3/4}, qq{?},
191             'A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I',
192             'D', 'N', 'O', 'O', 'O', 'O', 'O', 'x', 'O', 'U', 'U', 'U', 'U', 'U', 'Th', 'ss',
193             'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i',
194             'd', 'n', 'o', 'o', 'o', 'o', 'o', qq{/}, 'o', 'u', 'u', 'u', 'u', 'y', 'th', 'y',
195              
196             );
197              
198             {
199             # Now stuff %Latin1Char_to_fallback:
200             %Latin1Char_to_fallback = ();
201             my($k,$v);
202             while( ($k,$v) = each %Latin1Code_to_fallback) {
203             $Latin1Char_to_fallback{chr $k} = $v;
204             #print chr($k), ' => ', $v, "\n";
205             }
206             }
207              
208             #--------------------------------------------------------------------------
209              
210             %Name2character_number = (
211             # General XML/XHTML:
212             'lt' => 60,
213             'gt' => 62,
214             'quot' => 34,
215             'amp' => 38,
216             'apos' => 39,
217              
218             # POD-specific:
219             'sol' => 47,
220             'verbar' => 124,
221              
222             'lchevron' => 171, # legacy for laquo
223             'rchevron' => 187, # legacy for raquo
224              
225             # Remember, grave looks like \ (as in virtu\)
226             # acute looks like / (as in re/sume/)
227             # circumflex looks like ^ (as in papier ma^che/)
228             # umlaut/dieresis looks like " (as in nai"ve, Chloe")
229              
230             # From the XHTML 1 .ent files:
231             'nbsp' , 160,
232             'iexcl' , 161,
233             'cent' , 162,
234             'pound' , 163,
235             'curren' , 164,
236             'yen' , 165,
237             'brvbar' , 166,
238             'sect' , 167,
239             'uml' , 168,
240             'copy' , 169,
241             'ordf' , 170,
242             'laquo' , 171,
243             'not' , 172,
244             'shy' , 173,
245             'reg' , 174,
246             'macr' , 175,
247             'deg' , 176,
248             'plusmn' , 177,
249             'sup2' , 178,
250             'sup3' , 179,
251             'acute' , 180,
252             'micro' , 181,
253             'para' , 182,
254             'middot' , 183,
255             'cedil' , 184,
256             'sup1' , 185,
257             'ordm' , 186,
258             'raquo' , 187,
259             'frac14' , 188,
260             'frac12' , 189,
261             'frac34' , 190,
262             'iquest' , 191,
263             'Agrave' , 192,
264             'Aacute' , 193,
265             'Acirc' , 194,
266             'Atilde' , 195,
267             'Auml' , 196,
268             'Aring' , 197,
269             'AElig' , 198,
270             'Ccedil' , 199,
271             'Egrave' , 200,
272             'Eacute' , 201,
273             'Ecirc' , 202,
274             'Euml' , 203,
275             'Igrave' , 204,
276             'Iacute' , 205,
277             'Icirc' , 206,
278             'Iuml' , 207,
279             'ETH' , 208,
280             'Ntilde' , 209,
281             'Ograve' , 210,
282             'Oacute' , 211,
283             'Ocirc' , 212,
284             'Otilde' , 213,
285             'Ouml' , 214,
286             'times' , 215,
287             'Oslash' , 216,
288             'Ugrave' , 217,
289             'Uacute' , 218,
290             'Ucirc' , 219,
291             'Uuml' , 220,
292             'Yacute' , 221,
293             'THORN' , 222,
294             'szlig' , 223,
295             'agrave' , 224,
296             'aacute' , 225,
297             'acirc' , 226,
298             'atilde' , 227,
299             'auml' , 228,
300             'aring' , 229,
301             'aelig' , 230,
302             'ccedil' , 231,
303             'egrave' , 232,
304             'eacute' , 233,
305             'ecirc' , 234,
306             'euml' , 235,
307             'igrave' , 236,
308             'iacute' , 237,
309             'icirc' , 238,
310             'iuml' , 239,
311             'eth' , 240,
312             'ntilde' , 241,
313             'ograve' , 242,
314             'oacute' , 243,
315             'ocirc' , 244,
316             'otilde' , 245,
317             'ouml' , 246,
318             'divide' , 247,
319             'oslash' , 248,
320             'ugrave' , 249,
321             'uacute' , 250,
322             'ucirc' , 251,
323             'uuml' , 252,
324             'yacute' , 253,
325             'thorn' , 254,
326             'yuml' , 255,
327              
328             'fnof' , 402,
329             'Alpha' , 913,
330             'Beta' , 914,
331             'Gamma' , 915,
332             'Delta' , 916,
333             'Epsilon' , 917,
334             'Zeta' , 918,
335             'Eta' , 919,
336             'Theta' , 920,
337             'Iota' , 921,
338             'Kappa' , 922,
339             'Lambda' , 923,
340             'Mu' , 924,
341             'Nu' , 925,
342             'Xi' , 926,
343             'Omicron' , 927,
344             'Pi' , 928,
345             'Rho' , 929,
346             'Sigma' , 931,
347             'Tau' , 932,
348             'Upsilon' , 933,
349             'Phi' , 934,
350             'Chi' , 935,
351             'Psi' , 936,
352             'Omega' , 937,
353             'alpha' , 945,
354             'beta' , 946,
355             'gamma' , 947,
356             'delta' , 948,
357             'epsilon' , 949,
358             'zeta' , 950,
359             'eta' , 951,
360             'theta' , 952,
361             'iota' , 953,
362             'kappa' , 954,
363             'lambda' , 955,
364             'mu' , 956,
365             'nu' , 957,
366             'xi' , 958,
367             'omicron' , 959,
368             'pi' , 960,
369             'rho' , 961,
370             'sigmaf' , 962,
371             'sigma' , 963,
372             'tau' , 964,
373             'upsilon' , 965,
374             'phi' , 966,
375             'chi' , 967,
376             'psi' , 968,
377             'omega' , 969,
378             'thetasym' , 977,
379             'upsih' , 978,
380             'piv' , 982,
381             'bull' , 8226,
382             'hellip' , 8230,
383             'prime' , 8242,
384             'Prime' , 8243,
385             'oline' , 8254,
386             'frasl' , 8260,
387             'weierp' , 8472,
388             'image' , 8465,
389             'real' , 8476,
390             'trade' , 8482,
391             'alefsym' , 8501,
392             'larr' , 8592,
393             'uarr' , 8593,
394             'rarr' , 8594,
395             'darr' , 8595,
396             'harr' , 8596,
397             'crarr' , 8629,
398             'lArr' , 8656,
399             'uArr' , 8657,
400             'rArr' , 8658,
401             'dArr' , 8659,
402             'hArr' , 8660,
403             'forall' , 8704,
404             'part' , 8706,
405             'exist' , 8707,
406             'empty' , 8709,
407             'nabla' , 8711,
408             'isin' , 8712,
409             'notin' , 8713,
410             'ni' , 8715,
411             'prod' , 8719,
412             'sum' , 8721,
413             'minus' , 8722,
414             'lowast' , 8727,
415             'radic' , 8730,
416             'prop' , 8733,
417             'infin' , 8734,
418             'ang' , 8736,
419             'and' , 8743,
420             'or' , 8744,
421             'cap' , 8745,
422             'cup' , 8746,
423             'int' , 8747,
424             'there4' , 8756,
425             'sim' , 8764,
426             'cong' , 8773,
427             'asymp' , 8776,
428             'ne' , 8800,
429             'equiv' , 8801,
430             'le' , 8804,
431             'ge' , 8805,
432             'sub' , 8834,
433             'sup' , 8835,
434             'nsub' , 8836,
435             'sube' , 8838,
436             'supe' , 8839,
437             'oplus' , 8853,
438             'otimes' , 8855,
439             'perp' , 8869,
440             'sdot' , 8901,
441             'lceil' , 8968,
442             'rceil' , 8969,
443             'lfloor' , 8970,
444             'rfloor' , 8971,
445             'lang' , 9001,
446             'rang' , 9002,
447             'loz' , 9674,
448             'spades' , 9824,
449             'clubs' , 9827,
450             'hearts' , 9829,
451             'diams' , 9830,
452             'OElig' , 338,
453             'oelig' , 339,
454             'Scaron' , 352,
455             'scaron' , 353,
456             'Yuml' , 376,
457             'circ' , 710,
458             'tilde' , 732,
459             'ensp' , 8194,
460             'emsp' , 8195,
461             'thinsp' , 8201,
462             'zwnj' , 8204,
463             'zwj' , 8205,
464             'lrm' , 8206,
465             'rlm' , 8207,
466             'ndash' , 8211,
467             'mdash' , 8212,
468             'lsquo' , 8216,
469             'rsquo' , 8217,
470             'sbquo' , 8218,
471             'ldquo' , 8220,
472             'rdquo' , 8221,
473             'bdquo' , 8222,
474             'dagger' , 8224,
475             'Dagger' , 8225,
476             'permil' , 8240,
477             'lsaquo' , 8249,
478             'rsaquo' , 8250,
479             'euro' , 8364,
480             );
481              
482              
483             # Fill out %Name2character...
484             {
485             %Name2character = ();
486             my($name, $number);
487             while( ($name, $number) = each %Name2character_number) {
488             if($] < 5.007 and $number > 255) {
489             $Name2character{$name} = $FAR_CHAR;
490             # substitute for Unicode characters, for perls
491             # that can't reliably handle them
492             } elsif ($] >= 5.007003) {
493             $Name2character{$name} = chr utf8::unicode_to_native($number);
494             # normal case for more recent Perls where we can translate from Unicode
495             # to the native character set.
496             }
497             elsif (exists $Code2USASCII{$number}) {
498             $Name2character{$name} = $Code2USASCII{$number};
499             # on older Perls, we can use the translations we have hard-coded in this
500             # file, but these don't include the non-ASCII-range characters
501             }
502             elsif ($NOT_ASCII && $number > 127 && $number < 256) {
503             # this range on old non-ASCII-platform perls is wrong
504             if (exists $Latin1Code_to_fallback{$number}) {
505             $Name2character{$name} = $Latin1Code_to_fallback{$number};
506             } else {
507             $Name2character{$name} = $FAR_CHAR;
508             }
509             } else {
510             $Name2character{$name} = chr $number;
511             }
512             }
513             }
514              
515             #--------------------------------------------------------------------------
516             1;
517             __END__