From: Jonathan McDowell Date: Wed, 14 Aug 2019 19:05:51 +0000 (+0100) Subject: Collect Unicode expansions together X-Git-Url: http://the.earth.li/gitweb/?a=commitdiff_plain;ds=sidebyside;h=refs%2Fheads%2Fmain;hp=a3186b959d8a7bb78456d8a58159b348a501951f;p=unicode-expand.git Collect Unicode expansions together When multiple Unicode characters exist together expand them all at the end rather than breaking up what might potentially be "Unicode art". Fixes: #1 --- diff --git a/unicode-expand.pl b/unicode-expand.pl index d81948c..a46f6b5 100644 --- a/unicode-expand.pl +++ b/unicode-expand.pl @@ -55,19 +55,28 @@ sub expand_topic { } sub expand_char { - my ($char) = @_; + my ($string) = @_; - my $name = charnames::viacode(ord $1); - $name = sprintf("{%X}", ord $1) unless defined($name); + my $expansion = ""; - return $name; + for my $c (split //,$string) { + my $name = charnames::viacode(ord $c); + $name = sprintf("{%X}", ord $c) unless defined($name); + if (length($expansion) == 0) { + $expansion .= $name; + } else { + $expansion .= "; " . $name; + } + } + + return $expansion; } sub expand { my ($server, $target, $data) = @_; $data = decode_utf8($data); - $data =~ s{([^\p{Letter}\p{Punctuation}\p{Control}\p{Space}\p{Sc}[:ascii:]])}{ + $data =~ s{([^\p{Letter}\p{Punctuation}\p{Control}\p{Space}\p{Sc}[:ascii:]]+)}{ "${1} [".expand_char($1)."]" }ge;