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);
83 my $codepoint = ord(substr $input, 0, 1);
85 if ($self->{style} eq 'univer') {
86 if ($input =~ /\p{age=unassigned}/) {
87 # check include for assignments after unicode 6.0 (perl v5.14)
88 state $agemap = do 'unicode-age.inc.pl';
89 my $version = $agemap->{$codepoint};
90 push @class, $version ? 'l2' : 'l1';
92 elsif ($input =~ /^\p{in=1.1}*$/) {
93 push @class, 'l5'; # first release 1993
95 elsif ($input =~ /^\p{in=3.0}*$/) {
96 push @class, 'l4'; # 20th century
98 elsif ($input =~ /^\p{in=4.1}*$/) {
99 push @class, 'l4'; # over 10 years ago
101 elsif ($input =~ /^\p{in=6.0}*$/) {
102 push @class, 'l3'; # before 2012
105 push @class, 'l2'; # more recent
110 if ($self->{style} eq 'di') {
111 if ($class =~ /\bu-di\b/) {
112 push @class, ('l4', 'u-di'); # standard digraph
114 elsif ($class =~ /\bu-prop\b/) {
115 push @class, ('l3', 'u-prop'); # unofficial
118 elsif ($self->{style} eq 'html') {
119 if (defined $entity) {
120 push @class, ($codepoint <= 0xFF ? 'l4' : 'l3', 'u-html');
124 if ($codepoint <= 0xFF) {
125 push @class, 'l4', 'u-lat1'; # latin1
127 elsif ($codepoint <= 0xD7FF) {
128 push @class, 'l3', 'u-bmp'; # bmp
132 if ($input =~ /[ -~]/) {
133 push @class, 'l5', 'u-ascii'; # ascii
135 elsif ($input =~ /^\p{in=6.0}+$/) {
136 push @class, 'l2'; # in unicode 6.0
139 push @class, 'l1'; # any unicode
145 for (@{ $self->{anno} }) {
147 if (defined $entity) {
148 $entity = "&$entity;" if /^&/;
149 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($entity));
153 elsif ($_ eq 'xml') {
154 $anno = sprintf(' <small class="digraph">%s</small>',
155 sprintf '#%d', ord($cell)
159 elsif ($_ eq '&xml') {
160 $anno = sprintf(' <small class="digraph">%s</small>',
161 sprintf '&#%d;', ord($cell)
166 if (defined $mnem and length $mnem) {
167 $anno = sprintf(' <small class="digraph">%s</small>', EscapeHTML($mnem));
172 if ($_ eq 'hex' or $cell =~ /^[^a-zA-Z]$/) {
173 $anno = sprintf(' <small class="%s">%04X</small>', 'value', ord $cell);
180 return sprintf('<td%s%s%s>%s%s',
181 defined $title ? qq{ title="$title"} : '',
182 @class ? sprintf(' class="%s"', join ' ', @class) : '',
184 $cell eq '' ? ' ' : $cell,
190 my ($self, $digraphs) = @_;
195 while ($digraphs->[0] !~ /^\./) {
196 my $cell = shift @$digraphs or last;
197 push @colheads, sprintf(
199 $cell =~ s/^-// ? 'td' : 'th',
200 $cell =~ s/:(.*)// ? qq{ title="$1"} : '',
201 $cell eq '_' ? ' ' : $cell
204 push @rows, sprintf '<thead><tr>%s<tbody>', join '', @colheads if @colheads;
207 for my $cell (@$digraphs) {
208 if ($cell =~ s/^\.//) {
209 # dot indicates start of a new row
211 if ($cell =~ s/^>//) {
212 # header cell text follows
213 $cell =~ s/_/ /g; # underscores may be used instead of whitespace (for qw//ability)
214 my $class = $cell =~ s/^-// && ' class="ex"';
215 $rows[-1] .= "<th$class>".($cell || ' ');
219 elsif ($cell eq '>') {
220 # merge this cell to the next column
225 $rows[-1] .= $self->cell($cell,
226 $colspan > 1 && qq{ colspan="$colspan"},
232 return sprintf qq{<table class="glyphs%s">\n%s</table>\n},
233 @{ $self->{anno} } ? ' dilabel' : '',
234 join '', map {"$_\n"} @rows;
240 print '<div class="section">';
241 printf '<h2>%s</h2>', shift unless ref $_[0];
243 while (ref $_[0] and $_ = shift) {
244 print $self->table($_);