index: release v1.18 with only altgr index linked
[sheet.git] / Shiar_Sheet / FormatChar.pm
1 package Shiar_Sheet::FormatChar;
2
3 use 5.010;
4 use strict;
5 use warnings;
6 use utf8;
7
8 use Data::Dump 'pp';
9 use PLP::Functions 'EscapeHTML';
10
11 our $VERSION = '1.10';
12
13 our $uc = do 'data/unicode-char.inc.pl';
14
15 sub new {
16         my ($class) = @_;
17         bless { anno => ['di', 0], style => 'di' }, $class;
18 }
19
20 sub glyph_mkinfo {
21         my ($self, $codepoint) = @_;
22         # attempt to get unicode character information
23         my $info = eval {
24                 require Unicode::UCD;
25                 Unicode::UCD::charinfo($codepoint)
26                         || { category => 'Xn', name => '' };
27         } or return;
28         my $string;
29         if ($info->{combining}) {
30                 # overlay combining diacritics
31                 $string = chr(9676) . chr($codepoint);
32         }
33         return [@$info{qw( category name )}, undef, $string];
34 }
35
36 sub glyph_info {
37         my ($self, $codepoint) = @_;
38         return $uc->{chr $codepoint} || $self->glyph_mkinfo($codepoint) || [];
39 }
40
41 sub glyph_html {
42         my ($self, $char) = @_;
43         my $codepoint = ord $char;
44         my $info = $self->glyph_info($codepoint);
45         my ($class, $name, $mnem, $entity, $string) = @$info;
46
47         my $cell = EscapeHTML($string || $char);
48         my $title = sprintf 'U+%04X%s', $codepoint, !!$name && " $name";
49
50         $cell = "<span>$cell</span>" if $class and $class =~ /\bZs\b/;
51         $cell = '&nbsp;' if $cell eq '';
52
53         return ($cell, EscapeHTML($title), !!$class && "X $class", $mnem, $entity);
54 }
55
56 sub glyphs_html {
57         my $self = shift;
58
59         return $self->glyph_html(@_) if length $_[0] <= 1;
60
61         my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
62         return (
63                 EscapeHTML($_[0]), # cell
64                 join(' | ', map { $_->[1] } @chars), # title
65                 $chars[0][2], # class
66                 join(' ',  map { $_->[3] // '…' } @chars), # digraph
67         );
68 }
69
70 sub glyph_cell {
71         my ($self, $char) = @_;
72         return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
73 }
74
75 sub glyph_level_univer {
76         my ($self, $input) = @_;
77         if ($input =~ /\p{age=unassigned}/) {
78                 # check include for assignments after unicode 6.0 (perl v5.14)
79                 state $agemap = do 'data/unicode-age.inc.pl';
80                 my $version = $agemap->{ord $input};
81                 return $version ? 'l2' : 'l1';
82         }
83         elsif ($input =~ /^\p{in=1.1}*$/) {
84                 return 'l5';  # first release 1993
85         }
86         elsif ($input =~ /^\p{in=3.0}*$/) {
87                 return 'l4';  # 20th century
88         }
89         elsif ($input =~ /^\p{in=4.1}*$/) {
90                 return 'l4';  # over 10 years ago
91         }
92         elsif ($input =~ /^\p{in=6.0}*$/) {
93                 return 'l3';  # before 2012
94         }
95         else {
96                 return 'l2';  # more recent
97         }
98 }
99
100 sub cell {
101         my ($self, $input, $html) = @_;
102         my (@class, $title, $cell, $mnem, $entity);
103
104         if ($input eq '-') {
105                 $cell = '';
106         }
107         elsif ($input eq '=') {
108                 push @class, 'u-invalid';
109                 $cell = '';
110         }
111         else {{
112                 push @class, 'X';
113
114                 if ($input =~ s/^-//) {
115                         push @class, 'ex'; # discouraged
116                 }
117
118                 $input =~ s/^\\//;  # escaped char
119                 ($cell, $title, my $class, $mnem, $entity) = $self->glyphs_html($input);
120
121                 if ($self->{style} eq 'univer') {
122                         push @class, $self->glyph_level_univer($input);
123                         next;
124                 }
125
126                 my $codepoint = ord $input;
127                 if ($self->{style} eq 'di') {
128                         if ($mnem and $mnem =~ /…/) {
129                                 # incomplete representation, usually partial
130                         }
131                         elsif ($class =~ /\bu-di\b/) {
132                                 push @class, ('l4', 'u-di'); # standard digraph
133                         }
134                         elsif ($class =~ /\bu-prop\b/) {
135                                 push @class, ('l3', 'u-prop'); # unofficial
136                         }
137                 }
138                 elsif ($self->{style} eq 'html') {
139                         if (defined $entity) {
140                                 push @class, ($codepoint <= 0xFF ? 'l4' : 'l3', 'u-html');
141                         }
142                 }
143                 else {
144                         if ($codepoint <= 0xFF) {
145                                 push @class, 'l4', 'u-lat1';  # latin1
146                         }
147                         elsif ($codepoint <= 0xD7FF) {
148                                 push @class, 'l3', 'u-bmp';  # bmp
149                         }
150                 }
151
152                 if ($input =~ /[ -~]/) {
153                         push @class, 'l5', 'u-ascii'; # ascii
154                 }
155                 elsif ($input =~ /^\p{in=6.0}+$/ and $input !~ /\p{Co}/) {
156                         push @class, 'l2'; # in unicode 6.0
157                 }
158                 else {
159                         push @class, 'l1'; # any unicode
160                 }
161         }}
162
163         my $anno = '';
164         if ($cell ne '') {
165                 for (@{ $self->{anno} }) {
166                         if (/html$/) {
167                                 if (defined $entity) {
168                                         $entity = "&$entity;" if /^&/;
169                                         $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
170                                         last;
171                                 }
172                         }
173                         elsif ($_ eq 'xml') {
174                                 $anno = sprintf(' <small class="digraph">%s</small>',
175                                         sprintf '#%d', ord($cell)
176                                 );
177                                 last;
178                         }
179                         elsif ($_ eq '&xml') {
180                                 $anno = sprintf(' <small class="digraph">%s</small>',
181                                         sprintf '&amp;#%d;', ord($cell)
182                                 );
183                                 last;
184                         }
185                         elsif ($_ eq 'di') {
186                                 if (defined $mnem and length $mnem) {
187                                         $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
188                                         last;
189                                 }
190                         }
191                         else {
192                                 if ($_ eq 'hex' or $input =~ /^[^a-zA-Z]$/) {
193                                         $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $input);
194                                         last;
195                                 }
196                         }
197                 }
198         }
199
200         return sprintf('<%s>%s%s',
201                 join(' ', 'td',
202                         defined $title ? qq{title="$title"}  : (),
203                         @class ? sprintf('class="%s"', join ' ', @class) : (),
204                         $html || (),
205                 ),
206                 $cell eq '' ? '&nbsp;' : $cell,
207                 $anno,
208         );
209 }
210
211 sub row {
212         my ($self, $cells) = @_;
213         my @html;
214
215         my $colspan = 1;
216         for my $cell (@{$cells}) {
217                 if ($cell =~ s/^\.//) {
218                         # dot indicates start of a new row
219                         push @html, '<tr>';
220                         if ($cell =~ s/^>//) {
221                                 # header cell text follows
222                                 $cell =~ s/_/ /g;  # underscores may be used instead of whitespace (for qw//ability)
223                                 my $class = $cell =~ s/^-// && ' class="ex"';
224                                 $html[-1] .= "<th$class>".($cell || '&nbsp;');
225                         }
226                         next;
227                 }
228                 elsif ($cell eq '>') {
229                         # merge this cell to the next column
230                         $colspan++;
231                         next;
232                 }
233                 elsif ($cell eq '>-') {
234                         $html[-1] .= '<th>';
235                         next;
236                 }
237                 elsif ($cell =~ m/^</) {
238                         $html[-1] .= '<td>'.$cell;
239                         next;
240                 }
241
242                 $html[-1] .= $self->cell($cell,
243                         $colspan > 1 && qq{colspan="$colspan"},
244                 );
245
246                 $colspan = 1;
247         }
248
249         return @html;
250 }
251
252 sub tabletag {
253         my ($self) = @_;
254         my $class = 'glyphs';
255         $class .= ' dilabel' if @{ $self->{anno} };
256         return sprintf '<table class="%s">', $class;
257 }
258
259 sub table {
260         my ($self, $digraphs) = @_;
261
262         my @rows;
263
264         my @colheads;
265         while ($digraphs->[0] !~ /^\./) {
266                 my $cell = shift @$digraphs or last;
267                 if ($cell eq '>') {
268                         push @colheads, '<tr>';
269                         next;
270                 }
271                 push @colheads, join('',
272                         '<',
273                         $cell =~ s/^-// ? 'td' : 'th',
274                         $cell =~ s/:(.*)// && qq{ title="$1"},
275                         $cell =~ s/^(>+)// && ' colspan='.(length($1) + 1),
276                         '>',
277                         $cell eq '_' ? '&nbsp;' : $cell
278                 );
279         }
280         push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
281         push @rows, $self->row($digraphs);
282
283         return join '', map {"$_\n"} $self->tabletag, @rows, '</table>';
284 }
285
286 sub print {
287         my $self = shift;
288         while (@_) {
289                 print '<div class="section">';
290                 printf '<h2>%s</h2>', shift unless ref $_[0];
291                 print "\n\n";
292                 while (ref $_[0] and $_ = shift) {
293                         print $self->table($_);
294                 }
295                 print "\n</div>";
296         }
297 }
298
299 sub legend {
300         my $self = shift;
301         my @classes = $self->{style} eq 'univer' ? (
302                 [l5 => 'unicode 1.1'],
303                 [l4 => '20th century'],
304                 [l3 => 'in 6.0 (2010)'],
305                 [l2 => 'recent assignments'],
306                 [l1 => 'proposed'],
307                 [ex => 'irregular'],
308         ) : (
309                 [l5 => 'ascii'],
310                 [l4 => $self->{style} eq 'di' ? 'digraph' : 'latin1'],
311                 [l3 => $self->{style} eq 'di' ? 'proposed' : 'HTML4'],
312                 [l2 => 'unicode ≤6.0'],
313                 [l1 => 'other unicode'],
314                 [ex => 'discouraged'],
315         );
316
317         return (
318                 '<div class="legend"><table class="glyphs"><tr>',
319                 (map { sprintf '<td class="X %s">%s', @{$_} } @classes),
320                 '</table></div>',
321         );
322 }
323
324 1;
325