5 no if $] >= 5.018, warnings => 'experimental::smartmatch';
7 use open OUT => ':utf8', ':std';
10 our $VERSION = '1.02';
13 # prepare presentational string for some control(lish) entries
14 "\xAD" => {string => '-'},
15 "\x{200E}" => {string => '→'},
16 "\x{200F}" => {string => '←'},
17 "\x{200B}" => {string => '␣'}, # nbsp: ~ in TeX
18 "\x{200C}" => {string => '|'}, # ISO-9995-7-081 lookalike (alt: ∣ ⊺ ⟙)
19 "\x{200D}" => {string => '⁀'}, # join (alt: ∤ |ͯ ⨝)
21 $info{chr $_} //= {} for 32 .. 126;
24 my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
25 for (values %$tables) {
28 length $_ == 1 or next; # ignore meta values
35 } or warn "Failed reading unicode tables: $@";
38 require HTML::Entities;
40 HTML::Entities->import('%char2entity');
41 while (my ($char, $entity) = each %char2entity) {
42 $entity =~ /[a-zA-Z]/ or next; # only actual aliases
43 $info{$char}->{html} = substr($entity, 1, -1);
46 } or warn "Failed importing html entities: $@";
49 'data/digraphs-rfc.inc.pl' => 'u-di',
50 'data/digraphs-shiar.inc.pl' => 'u-prop',
51 'data/digraphs-vim.inc.pl' => 'u-vim',
53 for (sort keys %diinc) {
55 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
56 for my $mnem (sort keys %{$di}) {
57 my $cp = $di->{$mnem};
58 length $mnem == 2 or next; # limit to digraphs
59 my $class = $diinc{$_};
60 $info{$cp}->{di} //= $mnem;
61 $info{$cp}->{class}->{$class}++;
66 # read introducing unicode versions for known characters
67 my $agemap = do 'unicode-age.inc.pl' or die $@ || $!;
68 for my $chr (keys %info) {
69 my $version = $agemap->{ord $chr} or next;
70 $info{$chr}->{class}->{'u-v'.$version}++
73 } or warn "Failed including unicode version data $@";
75 for my $chr (keys %info) {
77 # attempt to get unicode character information
80 Unicode::UCD::charinfo($cp)
81 || { block => '?', category => 'Xn', name => '', script => '' }
84 $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
86 # ignore vim flag in addition to rfc support, replace otherwise
87 $info->{class}->{'u-di'} or $info->{class}->{'u-prop'}++
88 if delete $info->{class}->{'u-vim'};
90 # categorise by unicode types and writing script
91 $info->{class}->{$_}++ for $info->{category};
92 $info->{class}->{$_}++ for $info->{script} || ();
94 # add custom categories for certain blocks
95 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
96 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
99 if ($info->{string}) {
100 # keep predefined presentational string
102 elsif ($info->{combining}) {
103 # overlay combining accents
104 $info->{string} = chr(9676) . $chr;
106 elsif (($cp & ~0b1001_1111) == 0 or $cp == 127) {
107 # control characters (first 32 chars from 0 and 128)
108 # rename to something more descriptive
109 $info->{name} = $info->{unicode10}
110 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
111 : sprintf('<control U+%04X>', $cp); # at least identify by value
112 # show descriptive symbols instead of control chars themselves
113 $info->{string} = $cp < 32 ? chr($cp + 0x2400) :
114 $cp == 127 ? chr(0x2421) :
122 # output perl code of hash
123 say "# automatically generated by $0";
126 for my $cp (sort keys %info) {
127 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
128 # convert info hashes into arrays of strings to output in display order
129 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
130 # strip off trailing missing values (especially string may be unknown)
131 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
132 # final line (assume safe within single quotes)
133 say sprintf '"\x{%X}" => [%s],',
134 ord $cp, join(',', map { escapeq($_) } @$row);
140 return 'undef' if not defined;
149 mkcharinfo - Gather Unicode character details in Perl array
153 mkcharinfo > unicode-char.inc.pl
155 Test by printing the description of U+0041 (latin A):
157 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
161 Mischa POSLAWSKY <perl@shiar.org>
165 Licensed under the GNU Affero General Public License version 3.