forked from AkkomaGang/akkoma
Emoji: Generate emoji detecting functions at compile time.
Suggested by jvalim
This commit is contained in:
parent
3ff5532220
commit
6fe2f554c3
1 changed files with 26 additions and 20 deletions
|
@ -261,28 +261,34 @@ defp match_group_patterns(group_patterns, matcher) do
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, file} = File.read("lib/pleroma/emoji-data.txt")
|
@external_resource "lib/pleroma/emoji-data.txt"
|
||||||
|
|
||||||
@unicode_emoji file
|
emojis =
|
||||||
|
@external_resource
|
||||||
|
|> File.read!()
|
||||||
|> String.split("\n")
|
|> String.split("\n")
|
||||||
|> Enum.filter(fn line -> String.starts_with?(line, ["1", "2", "3", "4"]) end)
|
|> Enum.filter(fn line -> line != "" and not String.starts_with?(line, "#") end)
|
||||||
|> Enum.map(fn line -> String.split(line) |> List.first() end)
|
|
||||||
|> Enum.map(fn line ->
|
|> Enum.map(fn line ->
|
||||||
case String.split(line, "..") do
|
line
|
||||||
|
|> String.split(";", parts: 2)
|
||||||
|
|> hd()
|
||||||
|
|> String.trim()
|
||||||
|
|> String.split("..")
|
||||||
|
|> case do
|
||||||
[number] ->
|
[number] ->
|
||||||
String.to_integer(number, 16)
|
<<String.to_integer(number, 16)::utf8>>
|
||||||
|
|
||||||
[first, last] ->
|
[first, last] ->
|
||||||
Range.new(String.to_integer(first, 16), String.to_integer(last, 16))
|
String.to_integer(first, 16)..String.to_integer(last, 16)
|
||||||
|> Enum.to_list()
|
|> Enum.map(&<<&1::utf8>>)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|> List.flatten()
|
|> List.flatten()
|
||||||
|> Enum.filter(&is_integer/1)
|
|
||||||
|> Enum.uniq()
|
|> Enum.uniq()
|
||||||
|> Enum.map(fn n -> :unicode.characters_to_binary([n], :utf32) end)
|
|
||||||
|
|
||||||
def is_unicode_emoji?(str) do
|
for emoji <- emojis do
|
||||||
str in @unicode_emoji
|
def is_unicode_emoji?(unquote(emoji)), do: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_unicode_emoji?(_), do: false
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue