charset: page with latin1 character table
[sheet.git] / charset.plp
1 <:
2 use utf8;
3 use strict;
4 use warnings;
5 use open IO => ':utf8';
6
7 our $VERSION = '1.0';
8
9 $header{content_type} = 'text/html; charset=utf-8';
10
11 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
12  "http://www.w3.org/TR/html4/loose.dtd">
13 <html>
14
15 <head>
16 <title>charset cheat sheet</title>
17 <meta http-equiv="content-type" content="utf-8">
18 <link rel="stylesheet" type="text/css" media="all" href="/base.css">
19 </head>
20
21 <body>
22 <h1>Character encoding</h1>
23
24 <:
25 my $diinfo = do 'digraphs.inc.pl';
26 my %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
27         keys %$diinfo;
28
29 use Encode qw(decode);
30 # generate character table(s)
31 # (~16x faster than decoding in loop;
32 #  substr strings is twice as fast as splitting to an array)
33 my @tables = map { decode($_, pack 'C*', 0..255) } 'iso-8859-1';
34 my $NOCHAR = chr 0xFFFD;
35
36 sub quote {
37         local $_ = shift;
38         s/"/&quot;/g;
39         s/</&lt;/g;
40         s/>/&gt;/g;
41         return $_;
42 }
43
44 my @nibble = (0..9, 'A'..'F');
45 for my $table (@tables) {
46         print '<table class="glyphs"><col>';
47         for my $section (qw{thead tfoot}) {
48                 print "<$section><tr><th>↳";
49                 print '<th>', $_ for @nibble;
50                 print "<th>&nbsp;\n";
51         }
52         print '<tbody>';
53         for my $msb (0 .. $#nibble) {
54                 print '<tr><th>', $nibble[$msb];
55                 for my $lsb (0 .. $#nibble) {
56                         my $glyph = substr $table, ($msb<<4) + $lsb, 1;
57                         if ($glyph eq $NOCHAR) {
58                                 print '<td>';
59                                 next;
60                         }
61                         my $info = [ord $glyph];
62                         if (defined (my $mnem = $di{ord $glyph})) {
63                                 $info = $diinfo->{$mnem};
64                         }
65                         my ($codepoint, $name, $prop, $script, $string) = @$info;
66
67                         $glyph = quote($string || $glyph);
68                         my $desc = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
69                         my @class = ('X', grep {$_} $prop, $script);
70
71                         $glyph = "<span>$glyph</span>" if $prop eq 'Zs';
72
73                         printf "\n".'<td class="%s" title="%s">%s',
74                                 join(' ', @class), quote($desc), $glyph;
75                 }
76                 print "\n<th>", $nibble[$msb], "\n";
77         }
78         print "</table>\n";
79 }
80