1 defmodule Pleroma.Formatter do
3 alias Pleroma.Web.MediaProxy
7 def parse_tags(text, data \\ %{}) do
8 Regex.scan(@tag_regex, text)
9 |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
11 if data["sensitive"] in [true, "True", "true", "1"],
12 do: [{"#nsfw", "nsfw"}] ++ map,
17 def parse_mentions(text) do
18 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
20 ~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
22 Regex.scan(regex, text)
25 |> Enum.map(fn "@" <> match = full_match ->
26 {full_match, User.get_cached_by_nickname(match)}
28 |> Enum.filter(fn {_match, user} -> user end)
47 "four_seasons_of_bbq",
73 "pusa_hispida_saimensis",
97 @finmoji_with_filenames Enum.map(@finmoji, fn finmoji ->
98 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
101 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
103 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
109 (default <> "\n" <> custom)
111 |> String.split(~r/\n+/)
112 |> Enum.map(fn line ->
113 [name, file] = String.split(line, ~r/,\s*/)
121 static_path = Path.join(:code.priv_dir(:pleroma), "static")
124 Application.get_env(:pleroma, :emoji, [])
125 |> Keyword.get(:shortcode_globs, [])
128 Enum.map(globs, fn glob ->
129 Path.join(static_path, glob)
134 Enum.map(paths, fn path ->
135 shortcode = Path.basename(path, Path.extname(path))
136 external_path = Path.join("/", Path.relative_to(path, static_path))
137 {shortcode, external_path}
141 @emoji @finmoji_with_filenames ++ @emoji_from_globs ++ @emoji_from_file
143 def emojify(text, emoji \\ @emoji)
144 def emojify(text, nil), do: text
146 def emojify(text, emoji) do
147 Enum.reduce(emoji, text, fn {emoji, file}, text ->
148 emoji = HTML.strip_tags(emoji)
149 file = HTML.strip_tags(file)
154 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
158 |> HTML.filter_tags()
162 def get_emoji(text) when is_binary(text) do
163 Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
166 def get_emoji(_), do: []
168 def get_custom_emoji() do
172 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
174 # IANA got a list https://www.iana.org/assignments/uri-schemes/ but
175 # Stuff like ipfs isn’t in it
176 # There is very niche stuff
194 # TODO: make it use something other than @link_regex
195 def html_escape(text, "text/html") do
196 HTML.filter_tags(text)
199 def html_escape(text, "text/plain") do
200 Regex.split(@link_regex, text, include_captures: true)
201 |> Enum.map_every(2, fn chunk ->
202 {:safe, part} = Phoenix.HTML.html_escape(chunk)
208 @doc "changes scheme:... urls to html links"
209 def add_links({subs, text}) do
210 additionnal_schemes =
211 Application.get_env(:pleroma, :uri_schemes, [])
212 |> Keyword.get(:additionnal_schemes, [])
216 |> String.split([" ", "\t", "<br>"])
217 |> Enum.filter(fn word -> String.starts_with?(word, @uri_schemes ++ additionnal_schemes) end)
218 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
219 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
220 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
224 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
228 Enum.map(links, fn {uuid, url} ->
229 {uuid, "<a href=\"#{url}\">#{url}</a>"}
235 @doc "Adds the links to mentioned users"
236 def add_user_links({subs, text}, mentions) do
239 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
240 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
244 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
245 String.replace(text, match, uuid)
250 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
251 ap_id = info["source_data"]["url"] || ap_id
253 short_match = String.split(match, "@") |> tl() |> hd()
256 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
262 @doc "Adds the hashtag links"
263 def add_hashtag_links({subs, text}, tags) do
266 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
267 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
271 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
272 String.replace(text, match, uuid)
277 Enum.map(tags, fn {tag_text, tag, uuid} ->
278 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
285 def finalize({subs, text}) do
286 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
287 String.replace(result_text, uuid, replacement)