misc/bin/little

57 lines
1.6 KiB
Raku
Executable file

#!/usr/bin/env raku
#| print arguments or stdin as superscript
unit sub MAIN(*@args);
# char ranges {{{
my %uc = (
A => '', B => '', D => '', E => '', G => '', H => '', I => '',
J => '', K => '', L => '', M => '', N => '', O => '', P => '',
R => 'ᴿ', T => '', U => '', W => ''
);
my %lc = (
a => '', b => '', c => '', d => '', e => '', f => '',
g => '', h => 'ʰ', i => '', j => 'ʲ', k => '', l => 'ˡ',
m => '', n => '', o => '', p => '', r => 'ʳ', s => 'ˢ',
t => '', u => '', v => '', w => 'ʷ', x => 'ˣ', y => 'ʸ',
z => ''
);
my %num = (
0 => '', 1 => '¹', 2 => '²', 3 => '³',
([4..9] Z [''..'']).Map
);
my %other = ('!' => '', '.' => '·', '(' => '', ')' => '');
# }}}
my %caseless = %lc, %num, %other;
my %cased = %uc, %caseless;
sub rejects(@chars, %map) { @chars.grep(* %map).unique.join(', ') }
sub non-ws($str) { $str.comb.grep(* !~~ /\s/) }
sub shrink(Str $str) {
my $str-lc = lc $str;
my @chars = non-ws $str;
my @lc-chars = non-ws $str-lc;
my $cased-rejects = rejects @chars, %cased;
my $caseless-rejects = rejects @lc-chars, %caseless;
if !$cased-rejects {
$str.trans: %cased
} elsif !$caseless-rejects {
note "using lowercase because no superscripts for $cased-rejects";
$str-lc.trans: %caseless;
} else {
note "no superscripts for $caseless-rejects";
""
}
}
if @args > 0 {
say shrink @args.join: ' '
} else {
say shrink $_ for $*IN.lines
}
# vim: set ft=little fdm=marker :