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