1 package Shiar_Sheet::FormatChar;
8 use PLP::Functions 'EscapeHTML';
10 our $VERSION = '1.06';
12 our $uc = do 'unicode-char.inc.pl';
16 bless { anno => ['di', 0], style => 'di' }, $class;
20 my ($self, $codepoint) = @_;
21 return $uc->{chr $codepoint} || eval {
23 if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
24 return [@$fullinfo{qw/category name - string/}];
30 my ($self, $char) = @_;
31 my $codepoint = ord $char;
32 my $info = $self->glyph_info($codepoint);
33 my ($class, $name, $mnem, $html, $string) = @$info;
35 my $cell = EscapeHTML($string || $char);
36 my $title = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
38 $cell = "<span>$cell</span>" if $class =~ /\bZs\b/;
39 $cell = ' ' if $cell eq '';
41 return ($cell, EscapeHTML($title), "X $class", $mnem, $html);
47 return $self->glyph_html(@_) if length $_[0] <= 1;
49 my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
51 EscapeHTML($_[0]), # cell
52 join(' | ', map { $_->[1] } @chars), # title
54 join(' ', grep { defined } map { $_->[3] } @chars), # digraph
59 my ($self, $char) = @_;
60 return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
64 my ($self, $input, $html) = @_;
65 my (@class, $title, $cell, $mnem, $entity);
70 elsif ($input eq '=') {
71 push @class, 'u-invalid';
77 if ($input =~ s/^-//) {
78 push @class, 'ex'; # discouraged
81 $input =~ s/^\\//; # escaped char
82 ($cell, $title, my $class, $mnem, $entity) = $self->glyphs_html($input);
84 if ($self->{style} eq 'univer') {
85 state $agemap = do 'unicode-age.inc.pl' or die $!;
86 my $version = $agemap->{ord $input};
88 push @class, 'l1'; # no known unicode assignment
90 elsif ($version < 20) {
91 push @class, 'l5'; # first release 1993
93 elsif ($version < 31) {
94 push @class, 'l4'; # 20th century
96 elsif ($version < 50) {
97 push @class, 'l4'; # over 10 years ago
99 elsif ($version < 61) {
100 push @class, 'l3'; # before 2012
103 push @class, 'l2'; # more recent
108 if ($self->{style} eq 'di') {
109 if ($class =~ /\bu-di\b/) {
110 push @class, ('l3', 'u-di'); # standard digraph
112 elsif ($class =~ /\bu-prop\b/) {
113 push @class, ('l2', 'u-prop'); # unofficial
116 elsif ($self->{style} eq 'html') {
117 if (defined $entity) {
118 push @class, ('l3', 'u-html');
122 my $codepoint = ord(substr $input, 0, 1);
123 if ($codepoint <= 0xFF) {
124 push @class, 'l3', 'u-lat1'; # latin1
126 elsif ($codepoint <= 0xD7FF) {
127 push @class, 'l2', 'u-bmp'; # bmp
131 if ($input =~ /[ -~]/) {
132 push @class, 'l4', 'u-ascii'; # ascii
135 push @class, 'l1'; # basic unicode
141 for (@{ $self->{anno} }) {
143 if (defined $entity) {
144 $entity = "&$entity;" if /^&/;
145 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
149 elsif ($_ eq 'xml') {
150 $anno = sprintf(' <small class="digraph">%s</small>',
151 sprintf '#%d', ord($cell)
155 elsif ($_ eq '&xml') {
156 $anno = sprintf(' <small class="digraph">%s</small>',
157 sprintf '&#%d;', ord($cell)
162 if (defined $mnem and length $mnem) {
163 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
168 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
169 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
176 return sprintf('<td%s%s%s>%s%s',
177 defined $title ? qq{ title="$title"} : '',
178 @class ? sprintf(' class="%s"', join ' ', @class) : '',
180 $cell eq '' ? ' ' : $cell,
186 my ($self, $digraphs) = @_;
191 while ($digraphs->[0] !~ /^\./) {
192 my $cell = shift @$digraphs or last;
193 push @colheads, sprintf(
195 $cell =~ s/^-// ? 'td' : 'th',
196 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
197 $cell eq '_' ? ' ' : $cell
200 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
203 for my $cell (@$digraphs) {
204 if ($cell =~ s/^\.//) {
205 # dot indicates start of a new row
207 if ($cell =~ s/^>//) {
208 # header cell text follows
209 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
210 my $class = $cell =~ s/^-// && ' class="ex"';
211 $rows[-1] .= "<th$class>".($cell || ' ');
215 elsif ($cell eq '>') {
216 # merge this cell to the next column
221 $rows[-1] .= $self->cell($cell,
222 $colspan > 1 && qq{ colspan="$colspan"},
228 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
229 @{ $self->{anno} } ? ' dilabel' : '',
230 join '', map {"$_\n"} @rows;
236 print '<div class="section">';
237 printf '<h2>%s</h2>', shift unless ref $_[0];
239 while (ref $_[0] and $_ = shift) {
240 print $self->table($_);