1 package Shiar_Sheet::FormatChar;
9 use PLP::Functions 'EscapeHTML';
11 our $VERSION = '1.06';
13 our $uc = do '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, $html, $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 =~ /\bZs\b/;
40 $cell = ' ' if $cell eq '';
42 return ($cell, EscapeHTML($title), "X $class", $mnem, $html);
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 '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') {
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}+$/) {
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 $cell =~ /^[^a-zA-Z]$/) {
177 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
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, $digraphs) = @_;
201 while ($digraphs->[0] !~ /^\./) {
202 my $cell = shift @$digraphs or last;
203 push @colheads, sprintf(
205 $cell =~ s/^-// ? 'td' : 'th',
206 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
207 $cell eq '_' ? ' ' : $cell
210 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
213 for my $cell (@$digraphs) {
214 if ($cell =~ s/^\.//) {
215 # dot indicates start of a new row
217 if ($cell =~ s/^>//) {
218 # header cell text follows
219 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
220 my $class = $cell =~ s/^-// && ' class="ex"';
221 $rows[-1] .= "<th$class>".($cell || ' ');
225 elsif ($cell eq '>') {
226 # merge this cell to the next column
230 elsif ($cell eq '>-') {
234 elsif ($cell =~ m/^</) {
235 $rows[-1] .= '<td>'.$cell;
239 $rows[-1] .= $self->cell($cell,
240 $colspan > 1 && qq{colspan="$colspan"},
246 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
247 @{ $self->{anno} } ? ' dilabel' : '',
248 join '', map {"$_\n"} @rows;
254 print '<div class="section">';
255 printf '<h2>%s</h2>', shift unless ref $_[0];
257 while (ref $_[0] and $_ = shift) {
258 print $self->table($_);