1 package Shiar_Sheet::FormatChar;
7 use PLP::Functions 'EscapeHTML';
11 our $diinfo = do 'digraphs.inc.pl';
12 our %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
13 sort { length $a <=> length $b } keys %$diinfo;
17 bless { digraph => 1, unicode => 0 }, $class;
21 my ($self, $codepoint) = @_;
22 if (defined (my $mnem = $di{$codepoint})) {
23 return ($diinfo->{$mnem}, length $mnem == 2 ? $mnem : undef);
26 if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
27 return [$codepoint, @$fullinfo{qw/name category script string/}];
33 my ($self, $char) = @_;
34 my ($info, $mnem) = $self->glyph_info(ord $char);
35 my ($codepoint, $name, $prop, $script, $string) = @$info;
37 my $cell = EscapeHTML($string || $char);
38 my $title = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
39 my @class = ('X', grep {$_} $prop, $script);
41 $cell = "<span>$cell</span>" if $prop and $prop eq 'Zs';
42 $cell = ' ' if $cell eq '';
44 return ($cell, EscapeHTML($title), join(' ', @class), $mnem);
50 return $self->glyph_html(@_) if length $_[0] <= 1;
52 my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
54 EscapeHTML($_[0]), # cell
55 join(' | ', map { $_->[1] } @chars), # title
57 join(' ', grep { defined } map { $_->[3] } @chars), # digraph
62 my ($self, $char) = @_;
63 return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
67 my ($self, $input, $html) = @_;
68 my (@class, $title, $cell, $mnem);
73 elsif ($input eq '=') {
74 push @class, 'u-invalid';
80 if ($input =~ s/^-//) {
81 push @class, 'ex'; # discouraged
84 ($cell, $title, my $class, $mnem) = $self->glyphs_html($input);
86 if ($self->{digraph}) {
88 push @class, $class =~ /\bXz\b/ ? ('l2', 'u-prop') # unofficial
89 : ('l3', 'u-di'); # standard digraph
93 my $codepoint = ord(substr $input, 0, 1);
94 if ($codepoint <= 0xFF) {
95 push @class, 'l3', 'u-lat1'; # latin1
97 elsif ($codepoint <= 0xD7FF) {
98 push @class, 'l2', 'u-bmp'; # bmp
102 if ($input =~ /[ -~]/) {
103 push @class, 'l4', 'u-ascii'; # ascii
106 push @class, 'l1'; # basic unicode
110 return sprintf('<td%s%s%s>%s%s',
111 defined $title ? qq{ title="$title"} : '',
112 @class ? sprintf(' class="%s"', join ' ', @class) : '',
114 $cell eq '' ? (' ', '') : ($cell,
115 $self->{digraph} && defined $mnem && length $mnem
116 ? sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem))
117 : $self->{unicode} + $cell =~ /^[^a-zA-Z]$/ > 0
118 ? sprintf(' <small class="%s">%04X</small>', 'value', ord $cell)
125 my ($self, $digraphs) = @_;
130 while ($digraphs->[0] !~ /^\./) {
131 my $cell = shift @$digraphs or last;
132 push @colheads, sprintf(
134 $cell =~ s/^-// ? 'td' : 'th',
135 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
136 $cell eq '_' ? ' ' : $cell
139 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
142 for my $cell (@$digraphs) {
143 if ($cell =~ s/^\.//) {
144 # dot indicates start of a new row
146 if ($cell =~ s/^>//) {
147 # header cell text follows
148 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
149 $rows[-1] .= '<th>'.($cell || ' ');
153 elsif ($cell eq '>') {
154 # merge this cell to the next column
159 $rows[-1] .= $self->cell($cell,
160 $colspan > 1 && qq{ colspan="$colspan"},
166 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
167 $self->{digraph} || $self->{unicode} >= 0 ? ' dilabel' : '',
168 join '', map {"$_\n"} @rows;
174 printf '<div class="section"><h2>%s</h2>'."\n\n", shift;
175 while (ref $_[0] and $_ = shift) {
176 print $self->table($_);