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*/)
119 @emoji @finmoji_with_filenames ++ @emoji_from_file
121 def emojify(text, emoji \\ @emoji)
122 def emojify(text, nil), do: text
124 def emojify(text, emoji) do
125 Enum.reduce(emoji, text, fn {emoji, file}, text ->
126 emoji = HtmlSanitizeEx.strip_tags(emoji)
127 file = HtmlSanitizeEx.strip_tags(file)
132 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
139 def get_emoji(text) do
140 Enum.filter(@emoji, fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
143 def get_custom_emoji() do
147 @link_regex ~r/https?:\/\/[\w\.\/?=\-#\+%&@~'\(\):]+[\w\/]/u
149 def html_escape(text) do
150 Regex.split(@link_regex, text, include_captures: true)
151 |> Enum.map_every(2, fn chunk ->
152 {:safe, part} = Phoenix.HTML.html_escape(chunk)
158 @doc "changes http:... links to html links"
159 def add_links({subs, text}) do
161 Regex.scan(@link_regex, text)
162 |> Enum.map(fn [url] -> {Ecto.UUID.generate(), url} end)
163 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
167 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
171 Enum.map(links, fn {uuid, url} ->
172 {:safe, link} = Phoenix.HTML.Link.link(url, to: url)
176 |> IO.iodata_to_binary()
184 @doc "Adds the links to mentioned users"
185 def add_user_links({subs, text}, mentions) do
188 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
189 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
193 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
194 String.replace(text, match, uuid)
199 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
200 ap_id = info["source_data"]["url"] || ap_id
202 short_match = String.split(match, "@") |> tl() |> hd()
205 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
211 @doc "Adds the hashtag links"
212 def add_hashtag_links({subs, text}, tags) do
215 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
216 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
220 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
221 String.replace(text, match, uuid)
226 Enum.map(tags, fn {_, tag, uuid} ->
227 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>##{tag}</a>"
234 def finalize({subs, text}) do
235 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
236 String.replace(result_text, uuid, replacement)