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