2019-08-29 13:01:37 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-08-29 13:01:37 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Emoji.Formatter do
|
|
|
|
alias Pleroma.Emoji
|
|
|
|
alias Pleroma.HTML
|
2019-02-06 10:05:34 -07:00
|
|
|
alias Pleroma.Web
|
2019-08-29 13:01:37 -06:00
|
|
|
alias Pleroma.Web.MediaProxy
|
|
|
|
|
|
|
|
def emojify(text) do
|
|
|
|
emojify(text, Emoji.get_all())
|
|
|
|
end
|
|
|
|
|
|
|
|
def emojify(text, nil), do: text
|
|
|
|
|
|
|
|
def emojify(text, emoji, strip \\ false) do
|
|
|
|
Enum.reduce(emoji, text, fn
|
2019-08-31 01:14:53 -06:00
|
|
|
{_, %Emoji{safe_code: emoji, safe_file: file}}, text ->
|
2019-08-29 13:01:37 -06:00
|
|
|
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
|
|
|
|
2019-08-31 01:14:53 -06:00
|
|
|
{unsafe_emoji, unsafe_file}, text ->
|
|
|
|
emoji = HTML.strip_tags(unsafe_emoji)
|
|
|
|
file = HTML.strip_tags(unsafe_file)
|
2019-08-29 13:01:37 -06:00
|
|
|
String.replace(text, ":#{emoji}:", prepare_emoji_html(emoji, file, strip))
|
|
|
|
end)
|
|
|
|
|> HTML.filter_tags()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp prepare_emoji_html(_emoji, _file, true), do: ""
|
|
|
|
|
|
|
|
defp prepare_emoji_html(emoji, file, _strip) do
|
|
|
|
"<img class='emoji' alt='#{emoji}' title='#{emoji}' src='#{MediaProxy.url(file)}' />"
|
|
|
|
end
|
|
|
|
|
|
|
|
def demojify(text) do
|
|
|
|
emojify(text, Emoji.get_all(), true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def demojify(text, nil), do: text
|
|
|
|
|
|
|
|
@doc "Outputs a list of the emoji-Maps in a text"
|
|
|
|
def get_emoji_map(text) when is_binary(text) do
|
2020-04-03 05:03:32 -06:00
|
|
|
Emoji.get_all()
|
|
|
|
|> Enum.filter(fn {emoji, %Emoji{}} -> String.contains?(text, ":#{emoji}:") end)
|
2019-08-31 01:14:53 -06:00
|
|
|
|> Enum.reduce(%{}, fn {name, %Emoji{file: file}}, acc ->
|
2019-02-06 10:05:34 -07:00
|
|
|
Map.put(acc, name, to_string(URI.merge(Web.base_url(), file)))
|
2019-08-29 13:01:37 -06:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-04-03 05:03:32 -06:00
|
|
|
def get_emoji_map(_), do: %{}
|
2019-08-29 13:01:37 -06:00
|
|
|
end
|