common module FormatChar to show character tables
[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.00';
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 {}, $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 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, 'di-invalid';
75                 $cell = '';
76         }
77         else {
78                 push @class, 'X';
79
80                 if ($input =~ s/^-//) {
81                         push @class, 'di-rare'; # discouraged
82                 }
83
84                 ($cell, $title, my $class, $mnem) = $self->glyphs_html($input);
85
86                 if (defined $mnem) {
87                         push @class, 'di-d'; # digraph
88                         push @class, 'di-prop' if $class =~ /\bXz\b/; # unofficial
89                 }
90
91                 if ($input =~ /[ -~]/) {
92                         push @class, 'di-a'; # ascii
93                 }
94                 else {
95                         push @class, 'di-b'; # basic unicode
96                 }
97         }
98
99         return sprintf('<td%s%s%s>%s%s',
100                 defined $title  ? qq{ title="$title"}  : '',
101                 @class ? sprintf(' class="%s"', join ' ', @class) : '',
102                 $html || '',
103                 $cell eq '' ? '&nbsp;' : $cell,
104                 defined $mnem && length $mnem
105                         ? sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem))
106                         : $cell =~ /^[^a-zA-Z]$/
107                                 ? sprintf(' <small class="%s">%04X</small>', 'value', ord $cell)
108                                 : '',
109         );
110 }
111
112 sub table {
113         my ($self, $digraphs) = @_;
114
115         my @rows;
116
117         my @colheads;
118         while ($digraphs->[0] !~ /^\./) {
119                 my $cell = shift @$digraphs or last;
120                 push @colheads, sprintf(
121                         '<%s%s>%s',
122                         $cell =~ s/^-// ? 'td' : 'th',
123                         $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
124                         $cell eq '_' ? '&nbsp;' : $cell
125                 );
126         }
127         push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
128
129         my $colspan = 1;
130         for my $cell (@$digraphs) {
131                 if ($cell =~ s/^\.//) {
132                         # dot indicates start of a new row
133                         push @rows, '';
134                         if ($cell =~ s/^>//) {
135                                 # header cell text follows
136                                 $cell =~ s/_/ /g;  # underscores may be used instead of whitespace (for qw//ability)
137                                 $rows[-1] .= '<th>'.($cell || '&nbsp;');
138                         }
139                         next;
140                 }
141                 elsif ($cell eq '>') {
142                         # merge this cell to the next column
143                         $colspan++;
144                         next;
145                 }
146
147                 $rows[-1] .= $self->cell($cell,
148                         $colspan > 1 && qq{ colspan="$colspan"},
149                 );
150
151                 $colspan = 1;
152         }
153
154         return sprintf qq{<table class="glyphs dilabel">\n%s</table>\n},
155                 join '', map {"<tr>$_\n"} @rows;
156 }
157
158 1;
159