c83508b71707775d01840105fc0cf3f424af4d25
[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 @request = ('iso-8859-1', 'cp437');
34 my @tables = map { decode($_, pack 'C*', 0..255) } @request;
35 my $NOCHAR = chr 0xFFFD;
36
37 for my $cp437 (grep {$request[$_] eq 'cp437'} 0 .. $#request) {
38         substr($tables[$cp437], 237, 1) = pack 'U*', 0x3D5; # phi sign
39         substr($tables[$cp437], 0, 32) = pack 'U*', map {hex} qw(
40                 2007 263A 263B 2665 2666 2663 2660 2022 25D8 25CB 25D9 2642 2640 266A 266B 263C
41                 25BA 25C4 2195 203C 00B6 00A7 25AC 21A8 2191 2193 2192 2190 221F 2194 25B2 25BC
42         );
43 }
44
45 sub quote {
46         local $_ = shift;
47         s/"/&quot;/g;
48         s/</&lt;/g;
49         s/>/&gt;/g;
50         return $_;
51 }
52
53 print "<ul>\n";
54
55 my @nibble = (0..9, 'A'..'F');
56 for my $tablenum (0 .. $#tables) {
57         print '<li><table class="glyphs">';
58         printf '<caption>%s</caption>', $request[$tablenum];
59         print '<col>';
60         for my $section (qw{thead}) {
61                 print "<$section><tr><th>↱";
62                 print '<th>', $_ for @nibble;
63                 print "\n";
64         }
65         print '<tbody>';
66         for my $msb (0 .. $#nibble) {
67                 print '<tr><th>', $nibble[$msb];
68                 for my $lsb (0 .. $#nibble) {
69                         my $glyph = substr $tables[$tablenum], ($msb<<4) + $lsb, 1;
70                         if ($glyph eq $NOCHAR) {
71                                 print '<td>';
72                                 next;
73                         }
74                         my $info = [ord $glyph];
75                         if (defined (my $mnem = $di{ord $glyph})) {
76                                 $info = $diinfo->{$mnem};
77                         }
78                         my ($codepoint, $name, $prop, $script, $string) = @$info;
79
80                         $glyph = quote($string || $glyph);
81                         my $desc = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
82                         my @class = ('X', grep {$_} $prop, $script);
83
84                         $glyph = "<span>$glyph</span>" if $prop eq 'Zs';
85
86                         printf "\n".'<td class="%s" title="%s">%s',
87                                 join(' ', @class), quote($desc), $glyph;
88                 }
89                 print "\n";
90         }
91         print "</table>\n";
92 }
93
94 print "</ul>\n";
95
96 :>
97 <hr>
98
99 <p class="footer">
100         <a href="http://sheet.shiar.nl/" rel="home">sheet.shiar.nl</a>/charset
101         <a href="git://git.shiar.nl/sheet" rel="vcs-git" title="Git repository"><:= "v$VERSION" :></a>
102         created by <a href="http://shiar.nl/" rel="author">Shiar</a> •
103         <a title="Licensed under the GNU Affero General Public License, version 3" rel="copyright"
104            href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">AGPLv3</a> •
105         last update <:
106                 use Time::Format qw(time_format);
107                 print time_format('yyyy-mm-dd', (stat $ENV{SCRIPT_FILENAME})[9]);
108         :>
109 </p>
110
111 </html>