formatchar: annotation fallback
[sheet.git] / Shiar_Sheet / FormatChar.pm
1 package Shiar_Sheet::FormatChar;
2
3 use strict;
4 use warnings;
5
6 use Data::Dump 'pp';
7 use PLP::Functions 'EscapeHTML';
8
9 our $VERSION = '1.04';
10
11 our $diinfo = do 'digraphs.inc.pl';
12 our %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
13         sort { length $a <=> length $b } keys %$diinfo;
14
15 sub new {
16         my ($class) = @_;
17         bless { anno => ['di', 0], style => 'di' }, $class;
18 }
19
20 sub glyph_info {
21         my ($self, $codepoint) = @_;
22         if (defined (my $mnem = $di{$codepoint})) {
23                 return ($diinfo->{$mnem}, length $mnem == 2 ? $mnem : undef);
24         }
25         require Unicode::UCD;
26         if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
27                 return [$codepoint, @$fullinfo{qw/name category script string/}];
28         }
29         return [$codepoint];
30 }
31
32 sub glyph_html {
33         my ($self, $char) = @_;
34         my ($info, $mnem) = $self->glyph_info(ord $char);
35         my ($codepoint, $name, $prop, $script, $string) = @$info;
36
37         my $cell = EscapeHTML($string || $char);
38         my $title = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
39         my @class = ('X', grep {$_} $prop, $script);
40
41         $cell = "<span>$cell</span>" if $prop and $prop eq 'Zs';
42         $cell = '&nbsp;' if $cell eq '';
43
44         return ($cell, EscapeHTML($title), join(' ', @class), $mnem);
45 }
46
47 sub glyphs_html {
48         my $self = shift;
49
50         return $self->glyph_html(@_) if length $_[0] <= 1;
51
52         my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
53         return (
54                 EscapeHTML($_[0]), # cell
55                 join(' | ', map { $_->[1] } @chars), # title
56                 $chars[0][2], # class
57                 join(' ',  grep { defined } map { $_->[3] } @chars), # digraph
58         );
59 }
60
61 sub glyph_cell {
62         my ($self, $char) = @_;
63         return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
64 }
65
66 sub cell {
67         my ($self, $input, $html) = @_;
68         my (@class, $title, $cell, $mnem);
69
70         if ($input eq '-') {
71                 $cell = '';
72         }
73         elsif ($input eq '=') {
74                 push @class, 'u-invalid';
75                 $cell = '';
76         }
77         else {
78                 push @class, 'X';
79
80                 if ($input =~ s/^-//) {
81                         push @class, 'ex'; # discouraged
82                 }
83
84                 ($cell, $title, my $class, $mnem) = $self->glyphs_html($input);
85
86                 if ($self->{style} = 'di') {
87                         if (defined $mnem) {
88                                 push @class, $class =~ /\bXz\b/ ? ('l2', 'u-prop') # unofficial
89                                         : ('l3', 'u-di'); # standard digraph
90                         }
91                 }
92                 else {
93                         my $codepoint = ord(substr $input, 0, 1);
94                         if ($codepoint <= 0xFF) {
95                                 push @class, 'l3', 'u-lat1';  # latin1
96                         }
97                         elsif ($codepoint <= 0xD7FF) {
98                                 push @class, 'l2', 'u-bmp';  # bmp
99                         }
100                 }
101
102                 if ($input =~ /[ -~]/) {
103                         push @class, 'l4', 'u-ascii'; # ascii
104                 }
105                 else {
106                         push @class, 'l1'; # basic unicode
107                 }
108         }
109
110         my $anno = '';
111         for (@{ $self->{anno} }) {
112                 if ($_ eq 'di') {
113                         if (defined $mnem and length $mnem) {
114                                 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
115                                 last;
116                         }
117                 }
118                 else {
119                         if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
120                                 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
121                                 last;
122                         }
123                 }
124         }
125
126         return sprintf('<td%s%s%s>%s%s',
127                 defined $title  ? qq{ title="$title"}  : '',
128                 @class ? sprintf(' class="%s"', join ' ', @class) : '',
129                 $html || '',
130                 $cell eq '' ? '&nbsp;' : $cell,
131                 $anno,
132         );
133 }
134
135 sub table {
136         my ($self, $digraphs) = @_;
137
138         my @rows;
139
140         my @colheads;
141         while ($digraphs->[0] !~ /^\./) {
142                 my $cell = shift @$digraphs or last;
143                 push @colheads, sprintf(
144                         '<%s%s>%s',
145                         $cell =~ s/^-// ? 'td' : 'th',
146                         $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
147                         $cell eq '_' ? '&nbsp;' : $cell
148                 );
149         }
150         push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
151
152         my $colspan = 1;
153         for my $cell (@$digraphs) {
154                 if ($cell =~ s/^\.//) {
155                         # dot indicates start of a new row
156                         push @rows, '<tr>';
157                         if ($cell =~ s/^>//) {
158                                 # header cell text follows
159                                 $cell =~ s/_/ /g;  # underscores may be used instead of whitespace (for qw//ability)
160                                 $rows[-1] .= '<th>'.($cell || '&nbsp;');
161                         }
162                         next;
163                 }
164                 elsif ($cell eq '>') {
165                         # merge this cell to the next column
166                         $colspan++;
167                         next;
168                 }
169
170                 $rows[-1] .= $self->cell($cell,
171                         $colspan > 1 && qq{ colspan="$colspan"},
172                 );
173
174                 $colspan = 1;
175         }
176
177         return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
178                 @{ $self->{anno} } ? ' dilabel' : '',
179                 join '', map {"$_\n"} @rows;
180 }
181
182 sub print {
183         my $self = shift;
184         while (@_) {
185                 printf '<div class="section"><h2>%s</h2>'."\n\n", shift;
186                 while (ref $_[0] and $_ = shift) {
187                         print $self->table($_);
188                 }
189                 print '</div>';
190         }
191 }
192
193 1;
194