1 package Shiar_Sheet::FormatChar;
9 use PLP::Functions 'EscapeHTML';
11 our $VERSION = '1.08';
13 our $uc = do 'data/unicode-char.inc.pl';
17 bless { anno => ['di', 0], style => 'di' }, $class;
21 my ($self, $codepoint) = @_;
22 return $uc->{chr $codepoint} || eval {
24 if (my $fullinfo = Unicode::UCD::charinfo($codepoint)) {
25 return [@$fullinfo{qw/category name - string/}];
31 my ($self, $char) = @_;
32 my $codepoint = ord $char;
33 my $info = $self->glyph_info($codepoint);
34 my ($class, $name, $mnem, $entity, $string) = @$info;
36 my $cell = EscapeHTML($string || $char);
37 my $title = sprintf 'U+%04X%s', $codepoint, !!$name && " ($name)";
39 $cell = "<span>$cell</span>" if $class and $class =~ /\bZs\b/;
40 $cell = ' ' if $cell eq '';
42 return ($cell, EscapeHTML($title), !!$class && "X $class", $mnem, $entity);
48 return $self->glyph_html(@_) if length $_[0] <= 1;
50 my @chars = map { [ $self->glyph_html($_) ] } split //, $_[0];
52 EscapeHTML($_[0]), # cell
53 join(' | ', map { $_->[1] } @chars), # title
55 join(' ', map { $_->[3] // '…' } @chars), # digraph
60 my ($self, $char) = @_;
61 return sprintf('<td class="%3$s" title="%2$s">%s', $self->glyph_html($char));
65 my ($self, $input, $html) = @_;
66 my (@class, $title, $cell, $mnem, $entity);
71 elsif ($input eq '=') {
72 push @class, 'u-invalid';
78 if ($input =~ s/^-//) {
79 push @class, 'ex'; # discouraged
82 $input =~ s/^\\//; # escaped char
83 ($cell, $title, my $class, $mnem, $entity) = $self->glyphs_html($input);
84 my $codepoint = ord $input;
86 if ($self->{style} eq 'univer') {
87 if ($input =~ /\p{age=unassigned}/) {
88 # check include for assignments after unicode 6.0 (perl v5.14)
89 state $agemap = do 'data/unicode-age.inc.pl';
90 my $version = $agemap->{$codepoint};
91 push @class, $version ? 'l2' : 'l1';
93 elsif ($input =~ /^\p{in=1.1}*$/) {
94 push @class, 'l5'; # first release 1993
96 elsif ($input =~ /^\p{in=3.0}*$/) {
97 push @class, 'l4'; # 20th century
99 elsif ($input =~ /^\p{in=4.1}*$/) {
100 push @class, 'l4'; # over 10 years ago
102 elsif ($input =~ /^\p{in=6.0}*$/) {
103 push @class, 'l3'; # before 2012
106 push @class, 'l2'; # more recent
111 if ($self->{style} eq 'di') {
112 if ($mnem and $mnem =~ /…/) {
113 # incomplete representation, usually partial
115 elsif ($class =~ /\bu-di\b/) {
116 push @class, ('l4', 'u-di'); # standard digraph
118 elsif ($class =~ /\bu-prop\b/) {
119 push @class, ('l3', 'u-prop'); # unofficial
122 elsif ($self->{style} eq 'html') {
123 if (defined $entity) {
124 push @class, ($codepoint <= 0xFF ? 'l4' : 'l3', 'u-html');
128 if ($codepoint <= 0xFF) {
129 push @class, 'l4', 'u-lat1'; # latin1
131 elsif ($codepoint <= 0xD7FF) {
132 push @class, 'l3', 'u-bmp'; # bmp
136 if ($input =~ /[ -~]/) {
137 push @class, 'l5', 'u-ascii'; # ascii
139 elsif ($input =~ /^\p{in=6.0}+$/ and $input !~ /\p{Co}/) {
140 push @class, 'l2'; # in unicode 6.0
143 push @class, 'l1'; # any unicode
149 for (@{ $self->{anno} }) {
151 if (defined $entity) {
152 $entity = "&$entity;" if /^&/;
153 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
157 elsif ($_ eq 'xml') {
158 $anno = sprintf(' <small class="digraph">%s</small>',
159 sprintf '#%d', ord($cell)
163 elsif ($_ eq '&xml') {
164 $anno = sprintf(' <small class="digraph">%s</small>',
165 sprintf '&#%d;', ord($cell)
170 if (defined $mnem and length $mnem) {
171 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
176 if ($_ eq 'hex' or $input =~ /^[^a-zA-Z]$/) {
177 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $input);
184 return sprintf('<%s>%s%s',
186 defined $title ? qq{title="$title"} : (),
187 @class ? sprintf('class="%s"', join ' ', @class) : (),
190 $cell eq '' ? ' ' : $cell,
196 my ($self, $cells) = @_;
200 for my $cell (@{$cells}) {
201 if ($cell =~ s/^\.//) {
202 # dot indicates start of a new row
204 if ($cell =~ s/^>//) {
205 # header cell text follows
206 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
207 my $class = $cell =~ s/^-// && ' class="ex"';
208 $html[-1] .= "<th$class>".($cell || ' ');
212 elsif ($cell eq '>') {
213 # merge this cell to the next column
217 elsif ($cell eq '>-') {
221 elsif ($cell =~ m/^</) {
222 $html[-1] .= '<td>'.$cell;
226 $html[-1] .= $self->cell($cell,
227 $colspan > 1 && qq{colspan="$colspan"},
238 my $class = 'glyphs';
239 $class .= ' dilabel' if @{ $self->{anno} };
240 return sprintf '<table class="%s">', $class;
244 my ($self, $digraphs) = @_;
249 while ($digraphs->[0] !~ /^\./) {
250 my $cell = shift @$digraphs or last;
252 push @colheads, '<tr>';
255 push @colheads, join('',
257 $cell =~ s/^-// ? 'td' : 'th',
258 $cell =~ s/:(.*)// && qq{ title="$1"},
259 $cell =~ s/^(>+)// && ' colspan='.(length($1) + 1),
261 $cell eq '_' ? ' ' : $cell
264 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
265 push @rows, $self->row($digraphs);
267 return join '', map {"$_\n"} $self->tabletag, @rows, '</table>';
273 print '<div class="section">';
274 printf '<h2>%s</h2>', shift unless ref $_[0];
276 while (ref $_[0] and $_ = shift) {
277 print $self->table($_);
285 my @classes = $self->{style} eq 'univer' ? (
286 [l5 => 'unicode 1.1'],
287 [l4 => '20th century'],
288 [l3 => 'in 6.0 (2010)'],
289 [l2 => 'recent assignments'],
294 [l4 => $self->{style} eq 'di' ? 'digraph' : 'latin1'],
295 [l3 => $self->{style} eq 'di' ? 'proposed' : 'HTML4'],
296 [l2 => 'unicode ≤6.0'],
297 [l1 => 'other unicode'],
298 [ex => 'discouraged'],
302 '<div class="legend"><table class="glyphs"><tr>',
303 (map { sprintf '<td class="X %s">%s', @{$_} } @classes),