1 package Shiar_Sheet::FormatChar;
9 use PLP::Functions 'EscapeHTML';
11 our $VERSION = '1.09';
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));
64 sub glyph_level_univer {
65 my ($self, $input) = @_;
66 if ($input =~ /\p{age=unassigned}/) {
67 # check include for assignments after unicode 6.0 (perl v5.14)
68 state $agemap = do 'data/unicode-age.inc.pl';
69 my $version = $agemap->{ord $input};
70 return $version ? 'l2' : 'l1';
72 elsif ($input =~ /^\p{in=1.1}*$/) {
73 return 'l5'; # first release 1993
75 elsif ($input =~ /^\p{in=3.0}*$/) {
76 return 'l4'; # 20th century
78 elsif ($input =~ /^\p{in=4.1}*$/) {
79 return 'l4'; # over 10 years ago
81 elsif ($input =~ /^\p{in=6.0}*$/) {
82 return 'l3'; # before 2012
85 return 'l2'; # more recent
90 my ($self, $input, $html) = @_;
91 my (@class, $title, $cell, $mnem, $entity);
96 elsif ($input eq '=') {
97 push @class, 'u-invalid';
103 if ($input =~ s/^-//) {
104 push @class, 'ex'; # discouraged
107 $input =~ s/^\\//; # escaped char
108 ($cell, $title, my $class, $mnem, $entity) = $self->glyphs_html($input);
110 if ($self->{style} eq 'univer') {
111 push @class, $self->glyph_level_univer($input);
115 my $codepoint = ord $input;
116 if ($self->{style} eq 'di') {
117 if ($mnem and $mnem =~ /…/) {
118 # incomplete representation, usually partial
120 elsif ($class =~ /\bu-di\b/) {
121 push @class, ('l4', 'u-di'); # standard digraph
123 elsif ($class =~ /\bu-prop\b/) {
124 push @class, ('l3', 'u-prop'); # unofficial
127 elsif ($self->{style} eq 'html') {
128 if (defined $entity) {
129 push @class, ($codepoint <= 0xFF ? 'l4' : 'l3', 'u-html');
133 if ($codepoint <= 0xFF) {
134 push @class, 'l4', 'u-lat1'; # latin1
136 elsif ($codepoint <= 0xD7FF) {
137 push @class, 'l3', 'u-bmp'; # bmp
141 if ($input =~ /[ -~]/) {
142 push @class, 'l5', 'u-ascii'; # ascii
144 elsif ($input =~ /^\p{in=6.0}+$/ and $input !~ /\p{Co}/) {
145 push @class, 'l2'; # in unicode 6.0
148 push @class, 'l1'; # any unicode
154 for (@{ $self->{anno} }) {
156 if (defined $entity) {
157 $entity = "&$entity;" if /^&/;
158 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
162 elsif ($_ eq 'xml') {
163 $anno = sprintf(' <small class="digraph">%s</small>',
164 sprintf '#%d', ord($cell)
168 elsif ($_ eq '&xml') {
169 $anno = sprintf(' <small class="digraph">%s</small>',
170 sprintf '&#%d;', ord($cell)
175 if (defined $mnem and length $mnem) {
176 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
181 if ($_ eq 'hex' or $input =~ /^[^a-zA-Z]$/) {
182 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $input);
189 return sprintf('<%s>%s%s',
191 defined $title ? qq{title="$title"} : (),
192 @class ? sprintf('class="%s"', join ' ', @class) : (),
195 $cell eq '' ? ' ' : $cell,
201 my ($self, $cells) = @_;
205 for my $cell (@{$cells}) {
206 if ($cell =~ s/^\.//) {
207 # dot indicates start of a new row
209 if ($cell =~ s/^>//) {
210 # header cell text follows
211 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
212 my $class = $cell =~ s/^-// && ' class="ex"';
213 $html[-1] .= "<th$class>".($cell || ' ');
217 elsif ($cell eq '>') {
218 # merge this cell to the next column
222 elsif ($cell eq '>-') {
226 elsif ($cell =~ m/^</) {
227 $html[-1] .= '<td>'.$cell;
231 $html[-1] .= $self->cell($cell,
232 $colspan > 1 && qq{colspan="$colspan"},
243 my $class = 'glyphs';
244 $class .= ' dilabel' if @{ $self->{anno} };
245 return sprintf '<table class="%s">', $class;
249 my ($self, $digraphs) = @_;
254 while ($digraphs->[0] !~ /^\./) {
255 my $cell = shift @$digraphs or last;
257 push @colheads, '<tr>';
260 push @colheads, join('',
262 $cell =~ s/^-// ? 'td' : 'th',
263 $cell =~ s/:(.*)// && qq{ title="$1"},
264 $cell =~ s/^(>+)// && ' colspan='.(length($1) + 1),
266 $cell eq '_' ? ' ' : $cell
269 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
270 push @rows, $self->row($digraphs);
272 return join '', map {"$_\n"} $self->tabletag, @rows, '</table>';
278 print '<div class="section">';
279 printf '<h2>%s</h2>', shift unless ref $_[0];
281 while (ref $_[0] and $_ = shift) {
282 print $self->table($_);
290 my @classes = $self->{style} eq 'univer' ? (
291 [l5 => 'unicode 1.1'],
292 [l4 => '20th century'],
293 [l3 => 'in 6.0 (2010)'],
294 [l2 => 'recent assignments'],
299 [l4 => $self->{style} eq 'di' ? 'digraph' : 'latin1'],
300 [l3 => $self->{style} eq 'di' ? 'proposed' : 'HTML4'],
301 [l2 => 'unicode ≤6.0'],
302 [l1 => 'other unicode'],
303 [ex => 'discouraged'],
307 '<div class="legend"><table class="glyphs"><tr>',
308 (map { sprintf '<td class="X %s">%s', @{$_} } @classes),