tools: prefix all generated includes by automation comment
[sheet.git] / tools / mkcharinfo
index 577ffb17b65cfaaa30640df2754d5bb5796c2475..23154959e50b654ff454ce50d8e6da1b5e842523 100755 (executable)
@@ -3,6 +3,7 @@ use 5.010;
 use strict;
 use warnings;
 use utf8;
+no if $] >= 5.018, warnings => 'experimental::smartmatch';
 
 use open OUT => ':utf8', ':std';
 use Data::Dump 'pp';
@@ -10,9 +11,13 @@ use Data::Dump 'pp';
 our $VERSION = '1.00';
 
 my %info = (
+       # prepare presentational string for some control(lish) entries
        "\xAD"     => {string => '-'},
        "\x{200E}" => {string => '→'},
        "\x{200F}" => {string => '←'},
+       "\x{200B}" => {string => '␣'},
+       "\x{200C}" => {string => '|'}, # ISO-9995-7-081 lookalike (alt: ∣ ⊺ ⟙)
+       "\x{200D}" => {string => '⁀'}, # join (alt: ∤ |ͯ ⨝)
 );
 $info{chr $_} //= {} for 32 .. 126;
 
@@ -32,7 +37,9 @@ eval {
 
 eval {
        require HTML::Entities;
-       while (my ($char, $entity) = each %HTML::Entities::char2entity) {
+       our %char2entity;
+       HTML::Entities->import('%char2entity');
+       while (my ($char, $entity) = each %char2entity) {
                $entity =~ /[a-zA-Z]/ or next;  # only actual aliases
                $info{$char}->{html} = substr($entity, 1, -1);
        }
@@ -58,6 +65,16 @@ for (keys %diinc) {
        }
 }
 
+eval {
+       # read introducing unicode versions for known characters
+       my $agemap = do 'unicode-age.inc.pl' or die $@ || $!;
+       for my $chr (keys %info) {
+               my $version = $agemap->{ord $chr} or next;
+               $info{$chr}->{class}->{'u-v'.$version}++
+       }
+       1;
+} or warn "Failed including unicode version data $@";
+
 for my $chr (keys %info) {
        my $cp = ord $chr;
        # attempt to get unicode character information
@@ -67,7 +84,7 @@ for my $chr (keys %info) {
                        || { block => '?', category => 'Xn', name => '', script => '' }
        } or next;
 
-       $info->{$_} = $info{$chr}->{$_} for qw(di html class string);
+       $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
 
        # categorise by unicode types and writing script
        $info->{class}->{$_}++ for $info->{category};
@@ -92,7 +109,9 @@ for my $chr (keys %info) {
                                ? '<'.$info->{unicode10}.'>'  # the old name was much more useful
                                : sprintf('<control U+%04X>', $cp);  # at least identify by value
                        # show descriptive symbols instead of control chars themselves
-                       $info->{string} = $cp < 32 ? chr($cp + 0x2400) : chr(0xFFFD);
+                       $info->{string} = $cp < 32   ? chr($cp + 0x2400) :
+                                         $cp == 127 ? chr(0x2421) :
+                                                      chr(0xFFFD);
                }
        }
 
@@ -100,6 +119,7 @@ for my $chr (keys %info) {
 }
 
 # output perl code of hash
+say "# automatically generated by $0";
 say 'use utf8;';
 say '+{';
 for my $cp (sort keys %info) {
@@ -115,7 +135,7 @@ for my $cp (sort keys %info) {
 say '}';
 
 sub escapeq {
-       my $_ = shift;
+       local $_ = shift;
        return 'undef' if not defined;
        s/(['\\])/\\$1/g;
        return "'$_'";