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 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
175 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
177 # TODO: make it use something other than @link_regex
178 def html_escape(text, "text/html") do
179 HTML.filter_tags(text)
182 def html_escape(text, "text/plain") do
183 Regex.split(@link_regex, text, include_captures: true)
184 |> Enum.map_every(2, fn chunk ->
185 {:safe, part} = Phoenix.HTML.html_escape(chunk)
191 @doc "changes scheme:... urls to html links"
192 def add_links({subs, text}) do
195 |> String.split([" ", "\t", "<br>"])
196 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
197 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
198 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
199 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
203 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
207 Enum.map(links, fn {uuid, url} ->
208 {uuid, "<a href=\"#{url}\">#{url}</a>"}
214 @doc "Adds the links to mentioned users"
215 def add_user_links({subs, text}, mentions) do
218 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
219 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
223 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
224 String.replace(text, match, uuid)
229 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
231 if is_binary(info["source_data"]["url"]) do
232 info["source_data"]["url"]
237 short_match = String.split(match, "@") |> tl() |> hd()
240 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
246 @doc "Adds the hashtag links"
247 def add_hashtag_links({subs, text}, tags) do
250 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
251 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
255 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
256 String.replace(text, match, uuid)
261 Enum.map(tags, fn {tag_text, tag, uuid} ->
262 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
269 def finalize({subs, text}) do
270 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
271 String.replace(result_text, uuid, replacement)