2018-12-24 13:34:45 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
2019-07-26 11:30:28 +02:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
|
2018-12-24 13:34:45 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2018-12-27 11:24:04 +01:00
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/formatter.ex
|
2018-12-24 13:34:45 +01:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
defmodule Mobilizon.Service.Formatter do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Formats input text to structured data, extracts mentions and hashtags.
|
2019-01-03 14:59:59 +01:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2022-04-20 09:01:27 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Service.Formatter.HTML
|
2020-11-30 16:14:13 +01:00
|
|
|
alias Phoenix.HTML.Tag
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2021-11-26 14:30:46 +01:00
|
|
|
# https://github.com/rrrene/credo/issues/912
|
|
|
|
# credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
|
2019-07-26 11:30:28 +02:00
|
|
|
@link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
|
|
|
|
@markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec escape_mention_handler(String.t(), String.t(), any(), any()) :: String.t()
|
|
|
|
defp escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
|
2022-04-20 09:01:27 +02:00
|
|
|
case ActivityPubActor.find_or_make_actor_from_nickname(nickname) do
|
|
|
|
{:ok, %Actor{}} ->
|
2019-07-26 11:30:28 +02:00
|
|
|
# escape markdown characters with `\\`
|
|
|
|
# (we don't want something like @user__name to be parsed by markdown)
|
|
|
|
String.replace(mention, @markdown_characters_regex, "\\\\\\1")
|
|
|
|
|
2022-04-20 09:01:27 +02:00
|
|
|
{:error, _err} ->
|
2019-07-26 11:30:28 +02:00
|
|
|
buffer
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec mention_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
|
2019-07-26 11:30:28 +02:00
|
|
|
def mention_handler("@" <> nickname, buffer, _opts, acc) do
|
2022-04-20 09:01:27 +02:00
|
|
|
case ActivityPubActor.find_or_make_actor_from_nickname(nickname) do
|
2019-12-03 11:29:51 +01:00
|
|
|
# %Actor{preferred_username: preferred_username} = actor ->
|
|
|
|
# link = "<span class='h-card mention'>@<span>#{preferred_username}</span></span>"
|
|
|
|
#
|
|
|
|
# {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, actor})}}
|
2019-11-15 18:36:47 +01:00
|
|
|
|
2022-04-20 09:01:27 +02:00
|
|
|
{:ok, %Actor{type: :Person, id: id, preferred_username: preferred_username} = actor} ->
|
2020-11-30 16:14:13 +01:00
|
|
|
# link =
|
|
|
|
# "<span class='h-card mention' data-user='#{id}'>@<span>#{preferred_username}</span></span>"
|
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
link =
|
2020-11-30 16:14:13 +01:00
|
|
|
Tag.content_tag(
|
|
|
|
:span,
|
|
|
|
[
|
|
|
|
"@",
|
|
|
|
Tag.content_tag(
|
|
|
|
:span,
|
|
|
|
preferred_username
|
|
|
|
)
|
|
|
|
],
|
|
|
|
"data-user": id,
|
|
|
|
class: "h-card mention"
|
|
|
|
)
|
|
|
|
|> Phoenix.HTML.safe_to_string()
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
{link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, actor})}}
|
|
|
|
|
2022-04-20 09:01:27 +02:00
|
|
|
{:error, _} ->
|
2019-07-26 11:30:28 +02:00
|
|
|
{buffer, acc}
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec hashtag_handler(String.t(), String.t(), any(), map()) :: {String.t(), map()}
|
2019-07-26 11:30:28 +02:00
|
|
|
def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
|
|
|
|
tag = String.downcase(tag)
|
2020-01-28 19:18:33 +01:00
|
|
|
url = "#{Endpoint.url()}/tag/#{tag}"
|
2021-03-25 17:55:21 +01:00
|
|
|
|
|
|
|
link =
|
|
|
|
Tag.content_tag(:a, tag_text,
|
|
|
|
class: "hashtag",
|
|
|
|
"data-tag": tag,
|
|
|
|
href: url,
|
|
|
|
rel: "tag ugc"
|
|
|
|
)
|
|
|
|
|> Phoenix.HTML.safe_to_string()
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
{link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
@doc """
|
|
|
|
Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
"""
|
|
|
|
@spec linkify(String.t(), keyword()) ::
|
|
|
|
{String.t(), [{String.t(), Actor.t()}], [{String.t(), String.t()}]}
|
|
|
|
def linkify(text, options \\ []) do
|
2020-11-30 12:22:05 +01:00
|
|
|
options = linkify_opts() ++ options
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
acc = %{mentions: MapSet.new(), tags: MapSet.new()}
|
2022-01-18 12:46:33 +01:00
|
|
|
{text, %{mentions: mentions, tags: tags}} = Linkify.link_map(text, acc, options)
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
{text, MapSet.to_list(mentions), MapSet.to_list(tags)}
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
@doc """
|
|
|
|
Escapes a special characters in mention names.
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec mentions_escape(String.t(), Keyword.t()) :: String.t()
|
2019-07-26 11:30:28 +02:00
|
|
|
def mentions_escape(text, options \\ []) do
|
|
|
|
options =
|
|
|
|
Keyword.merge(options,
|
|
|
|
mention: true,
|
|
|
|
url: false,
|
|
|
|
mention_handler: &escape_mention_handler/4
|
|
|
|
)
|
|
|
|
|
2020-11-30 12:22:05 +01:00
|
|
|
Linkify.link(text, options)
|
2019-07-26 11:30:28 +02:00
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec html_escape(
|
|
|
|
{text :: String.t(), mentions :: list(), hashtags :: list()},
|
|
|
|
type :: String.t()
|
|
|
|
) :: {String.t(), list(), list()}
|
|
|
|
@spec html_escape(text :: String.t(), type :: String.t()) :: String.t()
|
2019-07-26 11:30:28 +02:00
|
|
|
def html_escape({text, mentions, hashtags}, type) do
|
|
|
|
{html_escape(text, type), mentions, hashtags}
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-30 16:40:59 +02:00
|
|
|
def html_escape(text, "text/html") do
|
2020-02-18 08:57:00 +01:00
|
|
|
with {:ok, content} <- HTML.filter_tags(text) do
|
|
|
|
content
|
|
|
|
end
|
2019-07-26 11:30:28 +02:00
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
|
|
|
|
def html_escape(text, "text/plain") do
|
2019-09-22 13:41:24 +02:00
|
|
|
@link_regex
|
|
|
|
|> Regex.split(text, include_captures: true)
|
2018-12-14 17:41:55 +01:00
|
|
|
|> Enum.map_every(2, fn chunk ->
|
|
|
|
{:safe, part} = Phoenix.HTML.html_escape(chunk)
|
|
|
|
part
|
|
|
|
end)
|
|
|
|
|> Enum.join("")
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec truncate(String.t(), non_neg_integer(), String.t()) :: String.t()
|
2019-07-26 11:30:28 +02:00
|
|
|
def truncate(text, max_length \\ 200, omission \\ "...") do
|
|
|
|
# Remove trailing whitespace
|
|
|
|
text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-07-26 11:30:28 +02:00
|
|
|
if String.length(text) < max_length do
|
|
|
|
text
|
|
|
|
else
|
|
|
|
length_with_omission = max_length - String.length(omission)
|
|
|
|
String.slice(text, 0, length_with_omission) <> omission
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
2020-11-30 12:22:05 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec linkify_opts :: Keyword.t()
|
2020-11-30 12:22:05 +01:00
|
|
|
defp linkify_opts do
|
|
|
|
Mobilizon.Config.get(__MODULE__) ++
|
|
|
|
[
|
2022-01-18 12:46:33 +01:00
|
|
|
hashtag: true,
|
|
|
|
hashtag_handler: &__MODULE__.hashtag_handler/4,
|
2020-11-30 12:22:05 +01:00
|
|
|
mention: true,
|
2020-11-30 16:14:13 +01:00
|
|
|
mention_handler: &__MODULE__.mention_handler/4
|
2020-11-30 12:22:05 +01:00
|
|
|
]
|
|
|
|
end
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|