package Shiar_Sheet::FormatChar;
use strict;
use warnings;
use Data::Dump 'pp';
use PLP::Functions 'EscapeHTML';
our $VERSION = '1.02';
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 { digraph => 1, unicode => 0 }, $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 = "$cell" if $prop and $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('
%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 ($self->{digraph}) {
if (defined $mnem) {
push @class, 'di-d'; # digraph
push @class, 'di-prop' if $class =~ /\bXz\b/; # unofficial
}
}
else {
my $codepoint = ord(substr $input, 0, 1);
if ($codepoint <= 0xFF) {
push @class, 'di-d'; # latin1
}
elsif ($codepoint <= 0xD7FF) {
push @class, 'di-prop'; # bmp
}
}
if ($input =~ /[ -~]/) {
push @class, 'di-a'; # ascii
}
else {
push @class, 'di-b'; # basic unicode
}
}
return sprintf(' | %s%s',
defined $title ? qq{ title="$title"} : '',
@class ? sprintf(' class="%s"', join ' ', @class) : '',
$html || '',
$cell eq '' ? (' ', '') : ($cell,
$self->{digraph} && defined $mnem && length $mnem
? sprintf(' %s', EscapeHTML($mnem))
: $self->{unicode} + $cell =~ /^[^a-zA-Z]$/ > 0
? sprintf(' %04X', '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 '%s | ', 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] .= ''.($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{\n},
$self->{digraph} || $self->{unicode} >= 0 ? ' dilabel' : '',
join '', map {" | $_\n"} @rows;
}
sub print {
my $self = shift;
while (@_) {
printf '%s
'."\n\n", shift;
while (ref $_[0] and $_ = shift) {
print $self->table($_);
}
print '';
}
}
1;