1 package Shiar_Sheet::FormatChar;
7 use PLP::Functions 'EscapeHTML';
11 our $uc = do 'unicode-char.inc.pl';
15 bless { anno => ['di', 0], style => 'di' }, $class;
19 my ($self, $codepoint) = @_;
20 return $uc->{chr $codepoint} || eval {
22 if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
23 return [@$fullinfo{qw/category name - string/}];
29 my ($self, $char) = @_;
30 my $codepoint = ord $char;
31 my $info = $self->glyph_info($codepoint);
32 my ($class, $name, $mnem, $html, $string) = @$info;
34 my $cell = EscapeHTML($string || $char);
35 my $title = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
37 $cell = "<span>$cell</span>" if $class =~ /\bZs\b/;
38 $cell = ' ' if $cell eq '';
40 return ($cell, EscapeHTML($title), "X $class", $mnem, $html);
46 return $self->glyph_html(@_) if length $_[0] <= 1;
48 my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
50 EscapeHTML($_[0]), # cell
51 join(' | ', map { $_->[1] } @chars), # title
53 join(' ', grep { defined } map { $_->[3] } @chars), # digraph
58 my ($self, $char) = @_;
59 return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
63 my ($self, $input, $html) = @_;
64 my (@class, $title, $cell, $mnem, $entity);
69 elsif ($input eq '=') {
70 push @class, 'u-invalid';
76 if ($input =~ s/^-//) {
77 push @class, 'ex'; # discouraged
80 $input =~ s/^\\//; # escaped char
81 ($cell, $title, my $class, $mnem, $entity) = $self->glyphs_html($input);
83 if ($self->{style} eq 'di') {
84 if ($class =~ /\bu-di\b/) {
85 push @class, ('l3', 'u-di'); # standard digraph
87 elsif ($class =~ /\bu-prop\b/) {
88 push @class, ('l2', 'u-prop'); # unofficial
91 elsif ($self->{style} eq 'html') {
92 if (defined $entity) {
93 push @class, ('l3', 'u-html');
97 my $codepoint = ord(substr $input, 0, 1);
98 if ($codepoint <= 0xFF) {
99 push @class, 'l3', 'u-lat1'; # latin1
101 elsif ($codepoint <= 0xD7FF) {
102 push @class, 'l2', 'u-bmp'; # bmp
106 if ($input =~ /[ -~]/) {
107 push @class, 'l4', 'u-ascii'; # ascii
110 push @class, 'l1'; # basic unicode
116 for (@{ $self->{anno} }) {
118 if (defined $entity) {
119 $entity = "&$entity;" if /^&/;
120 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
124 elsif ($_ eq 'xml') {
125 $anno = sprintf(' <small class="digraph">%s</small>',
126 sprintf '#%d', ord($cell)
130 elsif ($_ eq '&xml') {
131 $anno = sprintf(' <small class="digraph">%s</small>',
132 sprintf '&#%d;', ord($cell)
137 if (defined $mnem and length $mnem) {
138 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
143 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
144 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
151 return sprintf('<td%s%s%s>%s%s',
152 defined $title ? qq{ title="$title"} : '',
153 @class ? sprintf(' class="%s"', join ' ', @class) : '',
155 $cell eq '' ? ' ' : $cell,
161 my ($self, $digraphs) = @_;
166 while ($digraphs->[0] !~ /^\./) {
167 my $cell = shift @$digraphs or last;
168 push @colheads, sprintf(
170 $cell =~ s/^-// ? 'td' : 'th',
171 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
172 $cell eq '_' ? ' ' : $cell
175 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
178 for my $cell (@$digraphs) {
179 if ($cell =~ s/^\.//) {
180 # dot indicates start of a new row
182 if ($cell =~ s/^>//) {
183 # header cell text follows
184 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
185 $rows[-1] .= '<th>'.($cell || ' ');
189 elsif ($cell eq '>') {
190 # merge this cell to the next column
195 $rows[-1] .= $self->cell($cell,
196 $colspan > 1 && qq{ colspan="$colspan"},
202 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
203 @{ $self->{anno} } ? ' dilabel' : '',
204 join '', map {"$_\n"} @rows;
210 print '<div class="section">';
211 printf '<h2>%s</h2>', shift unless ref $_[0];
213 while (ref $_[0] and $_ = shift) {
214 print $self->table($_);