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)
32 @instance Application.get_env(:pleroma, :instance)
35 emojify(text, Emoji.get_all())
38 def emojify(text, nil), do: text
40 def emojify(text, emoji) do
41 Enum.reduce(emoji, text, fn {emoji, file}, text ->
42 emoji = HTML.strip_tags(emoji)
43 file = HTML.strip_tags(file)
48 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
56 def get_emoji(text) when is_binary(text) do
57 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
60 def get_emoji(_), do: []
62 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
64 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
65 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
67 # TODO: make it use something other than @link_regex
68 def html_escape(text, "text/html") do
69 HTML.filter_tags(text)
72 def html_escape(text, "text/plain") do
73 Regex.split(@link_regex, text, include_captures: true)
74 |> Enum.map_every(2, fn chunk ->
75 {:safe, part} = Phoenix.HTML.html_escape(chunk)
81 @doc "changes scheme:... urls to html links"
82 def add_links({subs, text}) do
85 |> String.split([" ", "\t", "<br>"])
86 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
87 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
88 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
89 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
93 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
97 Enum.map(links, fn {uuid, url} ->
98 {uuid, "<a href=\"#{url}\">#{url}</a>"}
104 @doc "Adds the links to mentioned users"
105 def add_user_links({subs, text}, mentions) do
108 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
109 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
113 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
114 String.replace(text, match, uuid)
119 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
121 if is_binary(info["source_data"]["url"]) do
122 info["source_data"]["url"]
127 short_match = String.split(match, "@") |> tl() |> hd()
130 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
136 @doc "Adds the hashtag links"
137 def add_hashtag_links({subs, text}, tags) do
140 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
141 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
145 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
146 String.replace(text, match, uuid)
151 Enum.map(tags, fn {tag_text, tag, uuid} ->
152 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
159 def finalize({subs, text}) do
160 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
161 String.replace(result_text, uuid, replacement)