1 defmodule Pleroma.Formatter do
3 alias Pleroma.Web.MediaProxy
8 def parse_tags(text, data \\ %{}) do
9 Regex.scan(@tag_regex, text)
10 |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
12 if data["sensitive"] in [true, "True", "true", "1"],
13 do: [{"#nsfw", "nsfw"}] ++ map,
18 def parse_mentions(text) do
19 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
21 ~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
23 Regex.scan(regex, text)
26 |> Enum.map(fn "@" <> match = full_match ->
27 {full_match, User.get_cached_by_nickname(match)}
29 |> Enum.filter(fn {_match, user} -> user end)
33 emojify(text, Emoji.get_all())
36 def emojify(text, nil), do: text
38 def emojify(text, emoji) do
39 Enum.reduce(emoji, text, fn {emoji, file}, text ->
40 emoji = HTML.strip_tags(emoji)
41 file = HTML.strip_tags(file)
46 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
54 def get_emoji(text) when is_binary(text) do
55 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
58 def get_emoji(_), do: []
60 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
62 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
63 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
65 # TODO: make it use something other than @link_regex
66 def html_escape(text, "text/html") do
67 HTML.filter_tags(text)
70 def html_escape(text, "text/plain") do
71 Regex.split(@link_regex, text, include_captures: true)
72 |> Enum.map_every(2, fn chunk ->
73 {:safe, part} = Phoenix.HTML.html_escape(chunk)
79 @doc "changes scheme:... urls to html links"
80 def add_links({subs, text}) do
83 |> String.split([" ", "\t", "<br>"])
84 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
85 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
86 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
87 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
91 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
95 Enum.map(links, fn {uuid, url} ->
96 {uuid, "<a href=\"#{url}\">#{url}</a>"}
102 @doc "Adds the links to mentioned users"
103 def add_user_links({subs, text}, mentions) do
106 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
107 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
111 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
112 String.replace(text, match, uuid)
117 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
119 if is_binary(info.source_data["url"]) do
120 info.source_data["url"]
125 short_match = String.split(match, "@") |> tl() |> hd()
128 "<span><a data-user='#{id}' class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
134 @doc "Adds the hashtag links"
135 def add_hashtag_links({subs, text}, tags) do
138 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
139 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
143 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
144 String.replace(text, match, uuid)
149 Enum.map(tags, fn {tag_text, tag, uuid} ->
151 "<a data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
161 def finalize({subs, text}) do
162 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
163 String.replace(result_text, uuid, replacement)