78f2a1934b1bcf8017d61164a776934a6091c495
[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         if ($cell ne '') {
112                 for (@{ $self->{anno} }) {
113                         if (/html$/) {
114                                 require HTML::Entities;
115                                 if (my $entity = $HTML::Entities::char2entity{$cell}) {
116                                         $entity = substr($entity, 1, -1) unless /^&/;
117                                         $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
118                                         last;
119                                 }
120                         }
121                         elsif ($_ eq 'xml') {
122                                 $anno = sprintf(' <small class="digraph">%s</small>',
123                                         sprintf '#%d', ord($cell)
124                                 );
125                                 last;
126                         }
127                         elsif ($_ eq '&xml') {
128                                 $anno = sprintf(' <small class="digraph">%s</small>',
129                                         sprintf '&amp;#%d;', ord($cell)
130                                 );
131                                 last;
132                         }
133                         elsif ($_ eq 'di') {
134                                 if (defined $mnem and length $mnem) {
135                                         $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
136                                         last;
137                                 }
138                         }
139                         else {
140                                 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
141                                         $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
142                                         last;
143                                 }
144                         }
145                 }
146         }
147
148         return sprintf('<td%s%s%s>%s%s',
149                 defined $title  ? qq{ title="$title"}  : '',
150                 @class ? sprintf(' class="%s"', join ' ', @class) : '',
151                 $html || '',
152                 $cell eq '' ? '&nbsp;' : $cell,
153                 $anno,
154         );
155 }
156
157 sub table {
158         my ($self, $digraphs) = @_;
159
160         my @rows;
161
162         my @colheads;
163         while ($digraphs->[0] !~ /^\./) {
164                 my $cell = shift @$digraphs or last;
165                 push @colheads, sprintf(
166                         '<%s%s>%s',
167                         $cell =~ s/^-// ? 'td' : 'th',
168                         $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
169                         $cell eq '_' ? '&nbsp;' : $cell
170                 );
171         }
172         push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
173
174         my $colspan = 1;
175         for my $cell (@$digraphs) {
176                 if ($cell =~ s/^\.//) {
177                         # dot indicates start of a new row
178                         push @rows, '<tr>';
179                         if ($cell =~ s/^>//) {
180                                 # header cell text follows
181                                 $cell =~ s/_/ /g;  # underscores may be used instead of whitespace (for qw//ability)
182                                 $rows[-1] .= '<th>'.($cell || '&nbsp;');
183                         }
184                         next;
185                 }
186                 elsif ($cell eq '>') {
187                         # merge this cell to the next column
188                         $colspan++;
189                         next;
190                 }
191
192                 $rows[-1] .= $self->cell($cell,
193                         $colspan > 1 && qq{ colspan="$colspan"},
194                 );
195
196                 $colspan = 1;
197         }
198
199         return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
200                 @{ $self->{anno} } ? ' dilabel' : '',
201                 join '', map {"$_\n"} @rows;
202 }
203
204 sub print {
205         my $self = shift;
206         while (@_) {
207                 printf '<div class="section"><h2>%s</h2>'."\n\n", shift;
208                 while (ref $_[0] and $_ = shift) {
209                         print $self->table($_);
210                 }
211                 print '</div>';
212         }
213 }
214
215 1;
216