7 use open OUT => ':utf8', ':std';
10 our $VERSION = '1.00';
13 "\xAD" => {string => '-'},
14 "\x{200E}" => {string => '→'},
15 "\x{200F}" => {string => '←'},
17 $info{chr $_} //= {} for 32 .. 126;
20 my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
21 for (values %$tables) {
24 length $_ == 1 or next; # ignore meta values
31 } or warn "Failed reading unicode tables: $@";
34 require HTML::Entities;
35 while (my ($char, $entity) = each %HTML::Entities::char2entity) {
36 $entity =~ /[a-zA-Z]/ or next; # only actual aliases
37 $info{$char}->{html} = substr($entity, 1, -1);
40 } or warn "Failed importing html entities: $@";
43 'digraphs.inc.pl' => 'u-di',
47 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
48 while (my ($mnem, $cp) = each %$di) {
49 length $mnem == 2 or next; # limit to digraphs
50 $cp = chr $cp->[0] if ref $cp; # old style array
51 $info{$cp}->{di} //= $mnem;
52 $info{$cp}->{class}->{$_}++ for $diinc{$_};
56 for my $chr (keys %info) {
58 # attempt to get unicode character information
61 Unicode::UCD::charinfo($cp)
62 || { block => '?', category => 'Xn', name => '', script => '' }
65 $info->{$_} = $info{$chr}->{$_} for qw(di html class string);
67 # categorise by unicode types and writing script
68 $info->{class}->{$_}++ for $info->{category};
69 $info->{class}->{"u-$_"}++ for $info->{script} || ();
71 # add custom categories for certain blocks
72 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
73 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
76 when ($info->{string}) {
77 # keep predefined presentational string
79 when ($info->{combining}) {
80 # overlay combining accents
81 $info->{string} = chr(9676) . $chr;
83 when (($cp & ~0b1001_1111) == 0 or $cp == 127) {
84 # control characters (first 32 chars from 0 and 128)
85 # rename to something more descriptive
86 $info->{name} = $info->{unicode10}
87 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
88 : sprintf('<control U+%04X>', $cp); # at least identify by value
89 # show descriptive symbols instead of control chars themselves
90 $info->{string} = $cp < 32 ? chr($cp + 0x2400) : chr(0xFFFD);
97 # output perl code of hash
99 for my $cp (sort keys %info) {
100 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
101 # convert info hashes into arrays of strings to output in display order
102 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
103 # strip off trailing missing values (especially string may be unknown)
104 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
105 # final line (assume safe within single quotes)
106 say sprintf '"\x{%X}" => [%s],',
107 ord $cp, join(',', map { escapeq($_) } @$row);
113 return 'undef' if not defined;
122 convert-unicode.pl - Gather Unicode character details in Perl array
126 convert-unicode > unicode-char.inc.pl
128 Test by printing the description of U+0041 (latin A):
130 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
134 Mischa POSLAWSKY <perl@shiar.org>
138 Licensed under the GNU Affero General Public License version 3.