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 { anno => ['di', 0], style => 'di' }, $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->{style} = 'di') {
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
111 for (@{ $self->{anno} }) {
113 require HTML::Entities;
114 if (my $entity = $HTML::Entities::char2entity{$cell}) {
115 $entity = substr($entity, 1, -1) unless /^&/;
116 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
120 elsif ($_ eq 'xml') {
121 $anno = sprintf(' <small class="digraph">%s</small>',
122 sprintf '#%d', ord($cell)
126 elsif ($_ eq '&xml') {
127 $anno = sprintf(' <small class="digraph">%s</small>',
128 sprintf '&#%d;', ord($cell)
133 if (defined $mnem and length $mnem) {
134 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
139 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
140 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
146 return sprintf('<td%s%s%s>%s%s',
147 defined $title ? qq{ title="$title"} : '',
148 @class ? sprintf(' class="%s"', join ' ', @class) : '',
150 $cell eq '' ? ' ' : $cell,
156 my ($self, $digraphs) = @_;
161 while ($digraphs->[0] !~ /^\./) {
162 my $cell = shift @$digraphs or last;
163 push @colheads, sprintf(
165 $cell =~ s/^-// ? 'td' : 'th',
166 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
167 $cell eq '_' ? ' ' : $cell
170 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
173 for my $cell (@$digraphs) {
174 if ($cell =~ s/^\.//) {
175 # dot indicates start of a new row
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 || ' ');
184 elsif ($cell eq '>') {
185 # merge this cell to the next column
190 $rows[-1] .= $self->cell($cell,
191 $colspan > 1 && qq{ colspan="$colspan"},
197 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
198 @{ $self->{anno} } ? ' dilabel' : '',
199 join '', map {"$_\n"} @rows;
205 printf '<div class="section"><h2>%s</h2>'."\n\n", shift;
206 while (ref $_[0] and $_ = shift) {
207 print $self->table($_);