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 if ($input =~ /\p{age=unassigned}/) {
86 # check include for assignments after unicode 6.0 (perl v5.14)
87 state $agemap = do 'unicode-age.inc.pl';
88 my $version = $agemap->{ord $input};
89 push @class, $version ? 'l2' : 'l1';
91 elsif ($input =~ /^\p{in=1.1}*$/) {
92 push @class, 'l5'; # first release 1993
94 elsif ($input =~ /^\p{in=3.0}*$/) {
95 push @class, 'l4'; # 20th century
97 elsif ($input =~ /^\p{in=4.1}*$/) {
98 push @class, 'l4'; # over 10 years ago
100 elsif ($input =~ /^\p{in=6.0}*$/) {
101 push @class, 'l3'; # before 2012
104 push @class, 'l2'; # more recent
109 if ($self->{style} eq 'di') {
110 if ($class =~ /\bu-di\b/) {
111 push @class, ('l3', 'u-di'); # standard digraph
113 elsif ($class =~ /\bu-prop\b/) {
114 push @class, ('l2', 'u-prop'); # unofficial
117 elsif ($self->{style} eq 'html') {
118 if (defined $entity) {
119 push @class, ('l3', 'u-html');
123 my $codepoint = ord(substr $input, 0, 1);
124 if ($codepoint <= 0xFF) {
125 push @class, 'l3', 'u-lat1'; # latin1
127 elsif ($codepoint <= 0xD7FF) {
128 push @class, 'l2', 'u-bmp'; # bmp
132 if ($input =~ /[ -~]/) {
133 push @class, 'l4', 'u-ascii'; # ascii
136 push @class, 'l1'; # basic unicode
142 for (@{ $self->{anno} }) {
144 if (defined $entity) {
145 $entity = "&$entity;" if /^&/;
146 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
150 elsif ($_ eq 'xml') {
151 $anno = sprintf(' <small class="digraph">%s</small>',
152 sprintf '#%d', ord($cell)
156 elsif ($_ eq '&xml') {
157 $anno = sprintf(' <small class="digraph">%s</small>',
158 sprintf '&#%d;', ord($cell)
163 if (defined $mnem and length $mnem) {
164 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
169 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
170 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
177 return sprintf('<td%s%s%s>%s%s',
178 defined $title ? qq{ title="$title"} : '',
179 @class ? sprintf(' class="%s"', join ' ', @class) : '',
181 $cell eq '' ? ' ' : $cell,
187 my ($self, $digraphs) = @_;
192 while ($digraphs->[0] !~ /^\./) {
193 my $cell = shift @$digraphs or last;
194 push @colheads, sprintf(
196 $cell =~ s/^-// ? 'td' : 'th',
197 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
198 $cell eq '_' ? ' ' : $cell
201 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
204 for my $cell (@$digraphs) {
205 if ($cell =~ s/^\.//) {
206 # dot indicates start of a new row
208 if ($cell =~ s/^>//) {
209 # header cell text follows
210 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
211 my $class = $cell =~ s/^-// && ' class="ex"';
212 $rows[-1] .= "<th$class>".($cell || ' ');
216 elsif ($cell eq '>') {
217 # merge this cell to the next column
222 $rows[-1] .= $self->cell($cell,
223 $colspan > 1 && qq{ colspan="$colspan"},
229 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
230 @{ $self->{anno} } ? ' dilabel' : '',
231 join '', map {"$_\n"} @rows;
237 print '<div class="section">';
238 printf '<h2>%s</h2>', shift unless ref $_[0];
240 while (ref $_[0] and $_ = shift) {
241 print $self->table($_);