Merge branch 'tests-warning-as-errors' into 'develop'
[akkoma] / lib / pleroma / formatter.ex
1 defmodule Pleroma.Formatter do
2 alias Pleroma.User
3 alias Pleroma.Web.MediaProxy
4 alias Pleroma.HTML
5 alias Pleroma.Emoji
6
7 @tag_regex ~r/\#\w+/u
8 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
9
10 def parse_tags(text, data \\ %{}) do
11 Regex.scan(@tag_regex, text)
12 |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
13 |> (fn map ->
14 if data["sensitive"] in [true, "True", "true", "1"],
15 do: [{"#nsfw", "nsfw"}] ++ map,
16 else: map
17 end).()
18 end
19
20 def parse_mentions(text) do
21 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
22 regex =
23 ~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
24
25 Regex.scan(regex, text)
26 |> List.flatten()
27 |> Enum.uniq()
28 |> Enum.map(fn "@" <> match = full_match ->
29 {full_match, User.get_cached_by_nickname(match)}
30 end)
31 |> Enum.filter(fn {_match, user} -> user end)
32 end
33
34 def emojify(text) do
35 emojify(text, Emoji.get_all())
36 end
37
38 def emojify(text, nil), do: text
39
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)
44
45 String.replace(
46 text,
47 ":#{emoji}:",
48 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
49 MediaProxy.url(file)
50 }' />"
51 )
52 |> HTML.filter_tags()
53 end)
54 end
55
56 def get_emoji(text) when is_binary(text) do
57 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
58 end
59
60 def get_emoji(_), do: []
61
62 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
63
64 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
65 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
66
67 # TODO: make it use something other than @link_regex
68 def html_escape(text, "text/html") do
69 HTML.filter_tags(text)
70 end
71
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)
76 part
77 end)
78 |> Enum.join("")
79 end
80
81 @doc """
82 Escapes a special characters in mention names.
83 """
84 @spec mentions_escape(String.t(), list({String.t(), any()})) :: String.t()
85 def mentions_escape(text, mentions) do
86 mentions
87 |> Enum.reduce(text, fn {name, _}, acc ->
88 escape_name = String.replace(name, @markdown_characters_regex, "\\\\\\1")
89 String.replace(acc, name, escape_name)
90 end)
91 end
92
93 @doc "changes scheme:... urls to html links"
94 def add_links({subs, text}) do
95 links =
96 text
97 |> String.split([" ", "\t", "<br>"])
98 |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
99 |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
100 |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
101 |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
102
103 uuid_text =
104 links
105 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
106
107 subs =
108 subs ++
109 Enum.map(links, fn {uuid, url} ->
110 {uuid, "<a href=\"#{url}\">#{url}</a>"}
111 end)
112
113 {subs, uuid_text}
114 end
115
116 @doc "Adds the links to mentioned users"
117 def add_user_links({subs, text}, mentions) do
118 mentions =
119 mentions
120 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
121 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
122
123 uuid_text =
124 mentions
125 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
126 String.replace(text, match, uuid)
127 end)
128
129 subs =
130 subs ++
131 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
132 ap_id =
133 if is_binary(info.source_data["url"]) do
134 info.source_data["url"]
135 else
136 ap_id
137 end
138
139 short_match = String.split(match, "@") |> tl() |> hd()
140
141 {uuid,
142 "<span><a data-user='#{id}' class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
143 end)
144
145 {subs, uuid_text}
146 end
147
148 @doc "Adds the hashtag links"
149 def add_hashtag_links({subs, text}, tags) do
150 tags =
151 tags
152 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
153 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
154
155 uuid_text =
156 tags
157 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
158 String.replace(text, match, uuid)
159 end)
160
161 subs =
162 subs ++
163 Enum.map(tags, fn {tag_text, tag, uuid} ->
164 url =
165 "<a data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
166 tag_text
167 }</a>"
168
169 {uuid, url}
170 end)
171
172 {subs, uuid_text}
173 end
174
175 def finalize({subs, text}) do
176 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
177 String.replace(result_text, uuid, replacement)
178 end)
179 end
180 end