8a8af266cd1ae7ef160d8f33776b51ff650fdaab
[akkoma] / lib / pleroma / formatter.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Formatter do
6 alias Pleroma.{Emoji, HTML, User}
7 alias Pleroma.Web.MediaProxy
8
9 @tag_regex ~r/((?<=[^&])|\A)(\#)(\w+)/u
10 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
11
12 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
13 @mentions_regex ~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
14
15 def parse_tags(text, data \\ %{}) do
16 Regex.scan(@tag_regex, text)
17 |> Enum.map(fn ["#" <> tag = full_tag | _] -> {full_tag, String.downcase(tag)} end)
18 |> (fn map ->
19 if data["sensitive"] in [true, "True", "true", "1"],
20 do: [{"#nsfw", "nsfw"}] ++ map,
21 else: map
22 end).()
23 end
24
25 @doc "Parses mentions text and returns list {nickname, user}."
26 @spec parse_mentions(binary()) :: list({binary(), User.t()})
27 def parse_mentions(text) do
28 Regex.scan(@mentions_regex, text)
29 |> List.flatten()
30 |> Enum.uniq()
31 |> Enum.map(fn nickname ->
32 with nickname <- String.trim_leading(nickname, "@"),
33 do: {"@" <> nickname, User.get_cached_by_nickname(nickname)}
34 end)
35 |> Enum.filter(fn {_match, user} -> user end)
36 end
37
38 def emojify(text) do
39 emojify(text, Emoji.get_all())
40 end
41
42 def emojify(text, nil), do: text
43
44 def emojify(text, emoji, strip \\ false) do
45 Enum.reduce(emoji, text, fn {emoji, file}, text ->
46 emoji = HTML.strip_tags(emoji)
47 file = HTML.strip_tags(file)
48
49 String.replace(
50 text,
51 ":#{emoji}:",
52 if not strip do
53 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
54 MediaProxy.url(file)
55 }' />"
56 else
57 ""
58 end
59 )
60 |> HTML.filter_tags()
61 end)
62 end
63
64 def demojify(text) do
65 emojify(text, Emoji.get_all(), true)
66 end
67
68 def demojify(text, nil), do: text
69
70 def get_emoji(text) when is_binary(text) do
71 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
72 end
73
74 def get_emoji(_), do: []
75
76 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
77
78 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
79 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
80
81 # TODO: make it use something other than @link_regex
82 def html_escape(text, "text/html") do
83 HTML.filter_tags(text)
84 end
85
86 def html_escape(text, "text/plain") do
87 Regex.split(@link_regex, text, include_captures: true)
88 |> Enum.map_every(2, fn chunk ->
89 {:safe, part} = Phoenix.HTML.html_escape(chunk)
90 part
91 end)
92 |> Enum.join("")
93 end
94
95 @doc """
96 Escapes a special characters in mention names.
97 """
98 @spec mentions_escape(String.t(), list({String.t(), any()})) :: String.t()
99 def mentions_escape(text, mentions) do
100 mentions
101 |> Enum.reduce(text, fn {name, _}, acc ->
102 escape_name = String.replace(name, @markdown_characters_regex, "\\\\\\1")
103 String.replace(acc, name, escape_name)
104 end)
105 end
106
107 @doc "changes scheme:... urls to html links"
108 def add_links({subs, text}) do
109 links =
110 text
111 |> String.split([" ", "\t", "<br>"])
112 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
113 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
114 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
115 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
116
117 uuid_text =
118 links
119 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
120
121 subs =
122 subs ++
123 Enum.map(links, fn {uuid, url} ->
124 {uuid, "<a href=\"#{url}\">#{url}</a>"}
125 end)
126
127 {subs, uuid_text}
128 end
129
130 @doc "Adds the links to mentioned users"
131 def add_user_links({subs, text}, mentions, options \\ []) do
132 mentions =
133 mentions
134 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
135 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
136
137 uuid_text =
138 mentions
139 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
140 String.replace(text, match, uuid)
141 end)
142
143 subs =
144 subs ++
145 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
146 ap_id =
147 if is_binary(info.source_data["url"]) do
148 info.source_data["url"]
149 else
150 ap_id
151 end
152
153 nickname =
154 if options[:format] == :full do
155 User.full_nickname(match)
156 else
157 User.local_nickname(match)
158 end
159
160 {uuid,
161 "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>" <>
162 "@<span>#{nickname}</span></a></span>"}
163 end)
164
165 {subs, uuid_text}
166 end
167
168 @doc "Adds the hashtag links"
169 def add_hashtag_links({subs, text}, tags) do
170 tags =
171 tags
172 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
173 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
174
175 uuid_text =
176 tags
177 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
178 String.replace(text, ~r/((?<=[^&])|(\A))#{match}/, uuid)
179 end)
180
181 subs =
182 subs ++
183 Enum.map(tags, fn {tag_text, tag, uuid} ->
184 url =
185 "<a class='hashtag' data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
186 tag_text
187 }</a>"
188
189 {uuid, url}
190 end)
191
192 {subs, uuid_text}
193 end
194
195 def finalize({subs, text}) do
196 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
197 String.replace(result_text, uuid, replacement)
198 end)
199 end
200
201 def truncate(text, max_length \\ 200, omission \\ "...") do
202 # Remove trailing whitespace
203 text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
204
205 if String.length(text) < max_length do
206 text
207 else
208 length_with_omission = max_length - String.length(omission)
209 String.slice(text, 0, length_with_omission) <> omission
210 end
211 end
212 end