1 defmodule Pleroma.Formatter do
3 alias Pleroma.Web.MediaProxy
6 def parse_tags(text, data \\ %{}) do
7 Regex.scan(@tag_regex, text)
8 |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
10 if data["sensitive"] in [true, "True", "true", "1"],
11 do: [{"#nsfw", "nsfw"}] ++ map,
16 def parse_mentions(text) do
17 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
19 ~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
21 Regex.scan(regex, text)
24 |> Enum.map(fn "@" <> match = full_match ->
25 {full_match, User.get_cached_by_nickname(match)}
27 |> Enum.filter(fn {_match, user} -> user end)
46 "four_seasons_of_bbq",
72 "pusa_hispida_saimensis",
96 @finmoji_with_filenames Enum.map(@finmoji, fn finmoji ->
97 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
100 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
102 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
108 (default <> "\n" <> custom)
110 |> String.split(~r/\n+/)
111 |> Enum.map(fn line ->
112 [name, file] = String.split(line, ~r/,\s*/)
120 static_path = Path.join(:code.priv_dir(:pleroma), "static")
123 Application.get_env(:pleroma, :emoji, [])
124 |> Keyword.get(:shortcode_globs, [])
127 Enum.map(globs, fn glob ->
128 Path.join(static_path, glob)
133 Enum.map(paths, fn path ->
134 shortcode = Path.basename(path, Path.extname(path))
135 external_path = Path.join("/", Path.relative_to(path, static_path))
136 {shortcode, external_path}
140 @emoji @finmoji_with_filenames ++ @emoji_from_globs ++ @emoji_from_file
142 def emojify(text, emoji \\ @emoji)
143 def emojify(text, nil), do: text
145 def emojify(text, emoji) do
146 Enum.reduce(emoji, text, fn {emoji, file}, text ->
147 emoji = HtmlSanitizeEx.strip_tags(emoji)
148 file = HtmlSanitizeEx.strip_tags(file)
153 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
157 |> HtmlSanitizeEx.basic_html()
161 def get_emoji(text) when is_binary(text) do
162 Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
165 def get_emoji(_), do: []
167 def get_custom_emoji() do
171 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
173 # IANA got a list https://www.iana.org/assignments/uri-schemes/ but
174 # Stuff like ipfs isn’t in it
175 # There is very niche stuff
193 # TODO: make it use something other than @link_regex
194 def html_escape(text) do
195 Regex.split(@link_regex, text, include_captures: true)
196 |> Enum.map_every(2, fn chunk ->
197 {:safe, part} = Phoenix.HTML.html_escape(chunk)
203 @doc "changes scheme:... urls to html links"
204 def add_links({subs, text}) do
205 additionnal_schemes =
206 Application.get_env(:pleroma, :uri_schemes, [])
207 |> Keyword.get(:additionnal_schemes, [])
211 |> String.split([" ", "\t", "<br>"])
212 |> Enum.filter(fn word -> String.starts_with?(word, @uri_schemes ++ additionnal_schemes) end)
213 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
214 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
215 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
219 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
223 Enum.map(links, fn {uuid, url} ->
224 {:safe, link} = Phoenix.HTML.Link.link(url, to: url)
228 |> IO.iodata_to_binary()
236 @doc "Adds the links to mentioned users"
237 def add_user_links({subs, text}, mentions) do
240 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
241 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
245 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
246 String.replace(text, match, uuid)
251 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
252 ap_id = info["source_data"]["url"] || ap_id
254 short_match = String.split(match, "@") |> tl() |> hd()
257 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
263 @doc "Adds the hashtag links"
264 def add_hashtag_links({subs, text}, tags) do
267 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
268 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
272 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
273 String.replace(text, match, uuid)
278 Enum.map(tags, fn {tag_text, tag, uuid} ->
279 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
286 def finalize({subs, text}) do
287 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
288 String.replace(result_text, uuid, replacement)