diff options
Diffstat (limited to 'a8utf8')
-rwxr-xr-x | a8utf8 | 163 |
1 files changed, 163 insertions, 0 deletions
@@ -0,0 +1,163 @@ +#!/usr/bin/perl -w + +# convert A8 text to UTF-8. Control graphics characters are replaced with +# nearest Unicode equivalents (mostly from the box-drawing range, or from +# the basic-latin range with -i option). + +# Careful editing this script: you need an editor that groks UTF-8, or at +# least one that won't mangle the UTF-8 sequences embedded in the tables +# below. + +($SELF = $0) =~ s,.*/,,; + +binmode(STDOUT, ":utf8"); +binmode(STDIN, ":bytes"); + +use utf8; + +%atascii_table = ( + 0 => "♥", + 1 => "┣", + 2 => "┃", + 3 => "┛", + 4 => "┫", + 5 => "┓", + 6 => "╱", + 7 => "╲", + 8 => "◢", + 9 => "▗", + 10 => "◣", + 11 => "▝", + 12 => "▘", + 13 => "▔", + 14 => "▁", + 15 => "▖", + 16 => "♣", + 17 => "┏", + 18 => "━", + 19 => "╋", + 20 => "●", + 21 => "▄", + 22 => "▎", + 23 => "┳", + 24 => "┻", + 25 => "▌", + 26 => "┗", + 27 => "␛", + 28 => "↑", + 29 => "↓", + 30 => "←", + 31 => "→", + 96 => "◆", + 123 => "♠", + 125 => "↰", + 126 => "◀", + 127 => "▶", + 136 => "◤", + 137 => "▛", + 138 => "◥", + 139 => "▙", + 140 => "▟", + 141 => "▆", + 142 => "🮅", + 143 => "▜", + 148 => "◙", + 149 => "▀", + 150 => "🮊", + 153 => "▐", + 155 => "\n", + 156 => "⍐", + 157 => "⍗", + 158 => "⍇", + 159 => "⍈", + 160 => "█", +); + +%xl_intl_table = ( + 0 => "á", + 1 => "ù", + 2 => "Ñ", + 3 => "É", + 4 => "ç", + 5 => "ô", + 6 => "ò", + 7 => "ì", + 8 => "£", + 9 => "ï", + 10 => "ü", + 11 => "ä", + 12 => "Ö", + 13 => "ú", + 14 => "ó", + 15 => "ö", + 16 => "Ü", + 17 => "â", + 18 => "û", + 19 => "î", + 20 => "é", + 21 => "è", + 22 => "ñ", + 23 => "ê", + 24 => "ȧ", + 25 => "à", + 26 => "Ȧ", + 27 => "␛", + 28 => "↑", + 29 => "↓", + 30 => "←", + 31 => "→", + 96 => "¡", + 123 => "Ä", + 126 => "◀", + 127 => "▶", + 155 => "\n", +); + +undef $/; + +$table = \%atascii_table; +$print_table = 0; +while(@ARGV && $ARGV[0] =~ /^-/) { + for($ARGV[0]) { + /^-i$/ && do { $table = \%xl_intl_table; next; }; + /^-t$/ && do { $print_table = 1; next; }; + /^--?h/ && do { usage(0) }; + warn "$SELF: unknown option: $_\n"; + usage(1); + } + shift @ARGV; +} + +if($print_table) { + for(sort { $a <=> $b } keys %$table) { + my $chr = translate(chr $_); + $chr = '\n' if $chr eq "\n"; + printf '"%d","$%02x","%s"' . "\n", $_, $_, $chr; + } + exit 0; +} + +$_ = <>; +s/(.)/translate($1)/seg; +print; + +sub translate { + my $o = ord(shift); + my $ret; + + $ret = $table->{$o}; + return $ret if defined($ret); + + $ret = $table->{$o & 0x7f}; + return $ret if defined($ret); + + return chr($o & 0x7f); +} + +sub usage { + print <<EOF; +Usage: $SELF [--help] | [-t [-i]] | [-i] infile [infile ...] +See man page for details. +EOF + exit $_[0]; +} |