+package Shiar_Sheet::FormatChar;
+
+use strict;
+use warnings;
+
+use Data::Dump 'pp';
+use PLP::Functions 'EscapeHTML';
+
+our $VERSION = '1.00';
+
+our $diinfo = do 'digraphs.inc.pl';
+our %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
+ sort { length $a <=> length $b } keys %$diinfo;
+
+sub new {
+ my ($class) = @_;
+ bless {}, $class;
+}
+
+sub glyph_info {
+ my ($self, $codepoint) = @_;
+ if (defined (my $mnem = $di{$codepoint})) {
+ return ($diinfo->{$mnem}, length $mnem == 2 ? $mnem : undef);
+ }
+ require Unicode::UCD;
+ if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
+ return [$codepoint, @$fullinfo{qw/name category script string/}];
+ }
+ return [$codepoint];
+}
+
+sub glyph_html {
+ my ($self, $char) = @_;
+ my ($info, $mnem) = $self->glyph_info(ord $char);
+ my ($codepoint, $name, $prop, $script, $string) = @$info;
+
+ my $cell = EscapeHTML($string || $char);
+ my $title = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
+ my @class = ('X', grep {$_} $prop, $script);
+
+ $cell = "<span>$cell</span>" if $prop eq 'Zs';
+ $cell = ' ' if $cell eq '';
+
+ return ($cell, EscapeHTML($title), join(' ', @class), $mnem);
+}
+
+sub glyphs_html {
+ my $self = shift;
+
+ return $self->glyph_html(@_) if length $_[0] <= 1;
+
+ my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
+ return (
+ EscapeHTML($_[0]), # cell
+ join(' | ', map { $_->[1] } @chars), # title
+ $chars[0][2], # class
+ join(' ', grep { defined } map { $_->[3] } @chars), # digraph
+ );
+}
+
+sub glyph_cell {
+ my ($self, $char) = @_;
+ return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
+}
+
+sub cell {
+ my ($self, $input, $html) = @_;
+ my (@class, $title, $cell, $mnem);
+
+ if ($input eq '-') {
+ $cell = '';
+ }
+ elsif ($input eq '=') {
+ push @class, 'di-invalid';
+ $cell = '';
+ }
+ else {
+ push @class, 'X';
+
+ if ($input =~ s/^-//) {
+ push @class, 'di-rare'; # discouraged
+ }
+
+ ($cell, $title, my $class, $mnem) = $self->glyphs_html($input);
+
+ if (defined $mnem) {
+ push @class, 'di-d'; # digraph
+ push @class, 'di-prop' if $class =~ /\bXz\b/; # unofficial
+ }
+
+ if ($input =~ /[ -~]/) {
+ push @class, 'di-a'; # ascii
+ }
+ else {
+ push @class, 'di-b'; # basic unicode
+ }
+ }
+
+ return sprintf('<td%s%s%s>%s%s',
+ defined $title ? qq{ title="$title"} : '',
+ @class ? sprintf(' class="%s"', join ' ', @class) : '',
+ $html || '',
+ $cell eq '' ? ' ' : $cell,
+ defined $mnem && length $mnem
+ ? sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem))
+ : $cell =~ /^[^a-zA-Z]$/
+ ? sprintf(' <small class="%s">%04X</small>', 'value', ord $cell)
+ : '',
+ );
+}
+
+sub table {
+ my ($self, $digraphs) = @_;
+
+ my @rows;
+
+ my @colheads;
+ while ($digraphs->[0] !~ /^\./) {
+ my $cell = shift @$digraphs or last;
+ push @colheads, sprintf(
+ '<%s%s>%s',
+ $cell =~ s/^-// ? 'td' : 'th',
+ $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
+ $cell eq '_' ? ' ' : $cell
+ );
+ }
+ push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
+
+ my $colspan = 1;
+ for my $cell (@$digraphs) {
+ if ($cell =~ s/^\.//) {
+ # dot indicates start of a new row
+ push @rows, '';
+ if ($cell =~ s/^>//) {
+ # header cell text follows
+ $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
+ $rows[-1] .= '<th>'.($cell || ' ');
+ }
+ next;
+ }
+ elsif ($cell eq '>') {
+ # merge this cell to the next column
+ $colspan++;
+ next;
+ }
+
+ $rows[-1] .= $self->cell($cell,
+ $colspan > 1 && qq{ colspan="$colspan"},
+ );
+
+ $colspan = 1;
+ }
+
+ return sprintf qq{<table class="glyphs dilabel">\n%s</table>\n},
+ join '', map {"<tr>$_\n"} @rows;
+}
+
+1;
+