Skip to content

Instantly share code, notes, and snippets.

@capezotte
Created May 12, 2021 21:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capezotte/a9cdf25133fbabfe7760253e5278585c to your computer and use it in GitHub Desktop.
Save capezotte/a9cdf25133fbabfe7760253e5278585c to your computer and use it in GitHub Desktop.
Text-to-Emoji gawk script
#!/usr/bin/gawk -f
# GAWK because unicode
# Keycap boilerplate
function keycap(k) { return sprintf("%c%c%c",k "",65039,8419); }
BEGIN {
# Regex of included characters
unmatch="[^-[:blank]"
# A-Z (regional indicators are also a continuous range)
for (i=65;i<=90;i++) { regional[sprintf("%c",i)] = sprintf("%c",i+127397); }
# Keycaps
for (i=1;i<=9;i++) { regional[i] = keycap(i) }
regional["#"] = keycap("#"); regional["*"] = keycap("*")
# Punctuation marks
regional["?"] = "❔"; regional["!"] = "❕"; regional["."] = "▪️"; regional[","] = "🌙"
regional["+"] = "➕" # Minus can't be here because regex
# Quotes
regional["\""] = "⏸️"; regional["'"] = "🚦"
# Misc
regional["$"] = "💲"; regional["÷"] = "➗"; regional["~"] = "〰️"; regional["|"] = "🚦"
# Add all regional characters so far
for (char in regional) {
unmatch=unmatch char
}
# Dash - must be last because regex
regional["-"] = "➖"
# Anti-diacritic command
iconv="iconv -f utf8 -t ascii//TRANSLIT"
# Close regex
unmatch = "]"
}
{
# Remove diacritrics
print $0 |& iconv
}
END {
# Iconv won't let us get the data until we close the pipe
close(iconv, "to")
# Get the data, now!
while (iconv |& getline) {
# Only uppercase
$0 = toupper($0)
# Remove
gsub(unmatch,"")
out=""
char_quant = split($0,chars,"")
for (i=1;i<=char_quant;i++) {
chr = chars[i]
if (regional[chr]) {
# Letter and digit
out = out " " regional[chr]
} else {
# Blank
out = out " "
}
}
print out | "xclip"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment