1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Formatter do
6 alias Pleroma.{Emoji, HTML, User}
7 alias Pleroma.Web.MediaProxy
9 @tag_regex ~r/((?<=[^&])|\A)(\#)(\w+)/u
10 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
12 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
13 @mentions_regex ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]*@?[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/u
15 def parse_tags(text, data \\ %{}) do
16 Regex.scan(@tag_regex, text)
17 |> Enum.map(fn ["#" <> tag = full_tag | _] -> {full_tag, String.downcase(tag)} end)
19 if data["sensitive"] in [true, "True", "true", "1"],
20 do: [{"#nsfw", "nsfw"}] ++ map,
25 @doc "Parses mentions text and returns list {nickname, user}."
26 @spec parse_mentions(binary()) :: list({binary(), User.t()})
27 def parse_mentions(text) do
28 Regex.scan(@mentions_regex, text)
31 |> Enum.map(fn nickname ->
32 with nickname <- String.trim_leading(nickname, "@"),
33 do: {"@" <> nickname, User.get_cached_by_nickname(nickname)}
35 |> Enum.filter(fn {_match, user} -> user end)
39 emojify(text, Emoji.get_all())
42 def emojify(text, nil), do: text
44 def emojify(text, emoji, strip \\ false) do
45 Enum.reduce(emoji, text, fn {emoji, file}, text ->
46 emoji = HTML.strip_tags(emoji)
47 file = HTML.strip_tags(file)
53 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
65 emojify(text, Emoji.get_all(), true)
68 def demojify(text, nil), do: text
70 def get_emoji(text) when is_binary(text) do
71 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
74 def get_emoji(_), do: []
76 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
78 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
79 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
81 # TODO: make it use something other than @link_regex
82 def html_escape(text, "text/html") do
83 HTML.filter_tags(text)
86 def html_escape(text, "text/plain") do
87 Regex.split(@link_regex, text, include_captures: true)
88 |> Enum.map_every(2, fn chunk ->
89 {:safe, part} = Phoenix.HTML.html_escape(chunk)
96 Escapes a special characters in mention names.
98 @spec mentions_escape(String.t(), list({String.t(), any()})) :: String.t()
99 def mentions_escape(text, mentions) do
101 |> Enum.reduce(text, fn {name, _}, acc ->
102 escape_name = String.replace(name, @markdown_characters_regex, "\\\\\\1")
103 String.replace(acc, name, escape_name)
107 @doc "changes scheme:... urls to html links"
108 def add_links({subs, text}) do
111 |> String.split([" ", "\t", "<br>"])
112 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
113 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
114 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
115 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
119 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
123 Enum.map(links, fn {uuid, url} ->
124 {uuid, "<a href=\"#{url}\">#{url}</a>"}
130 @doc "Adds the links to mentioned users"
131 def add_user_links({subs, text}, mentions, options \\ []) do
134 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
135 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
139 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
140 String.replace(text, match, uuid)
145 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
147 if is_binary(info.source_data["url"]) do
148 info.source_data["url"]
154 if options[:format] == :full do
155 User.full_nickname(match)
157 User.local_nickname(match)
161 "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>" <>
162 "@<span>#{nickname}</span></a></span>"}
168 @doc "Adds the hashtag links"
169 def add_hashtag_links({subs, text}, tags) do
172 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
173 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
177 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
178 String.replace(text, ~r/((?<=[^&])|(\A))#{match}/, uuid)
183 Enum.map(tags, fn {tag_text, tag, uuid} ->
185 "<a class='hashtag' data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
195 def finalize({subs, text}) do
196 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
197 String.replace(result_text, uuid, replacement)
201 def truncate(text, max_length \\ 200, omission \\ "...") do
202 # Remove trailing whitespace
203 text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
205 if String.length(text) < max_length do
208 length_with_omission = max_length - String.length(omission)
209 String.slice(text, 0, length_with_omission) <> omission