Merge branch 'runtime-router' 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 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)
11 |> (fn map ->
12 if data["sensitive"] in [true, "True", "true", "1"],
13 do: [{"#nsfw", "nsfw"}] ++ map,
14 else: map
15 end).()
16 end
17
18 def parse_mentions(text) do
19 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
20 regex =
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
22
23 Regex.scan(regex, text)
24 |> List.flatten()
25 |> Enum.uniq()
26 |> Enum.map(fn "@" <> match = full_match ->
27 {full_match, User.get_cached_by_nickname(match)}
28 end)
29 |> Enum.filter(fn {_match, user} -> user end)
30 end
31
32 @instance Application.get_env(:pleroma, :instance)
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 "changes scheme:... urls to html links"
82 def add_links({subs, text}) do
83 links =
84 text
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)
90
91 uuid_text =
92 links
93 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
94
95 subs =
96 subs ++
97 Enum.map(links, fn {uuid, url} ->
98 {uuid, "<a href=\"#{url}\">#{url}</a>"}
99 end)
100
101 {subs, uuid_text}
102 end
103
104 @doc "Adds the links to mentioned users"
105 def add_user_links({subs, text}, mentions) do
106 mentions =
107 mentions
108 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
109 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
110
111 uuid_text =
112 mentions
113 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
114 String.replace(text, match, uuid)
115 end)
116
117 subs =
118 subs ++
119 Enum.map(mentions, fn {match, %User{ap_id: ap_id, info: info}, uuid} ->
120 ap_id =
121 if is_binary(info["source_data"]["url"]) do
122 info["source_data"]["url"]
123 else
124 ap_id
125 end
126
127 short_match = String.split(match, "@") |> tl() |> hd()
128
129 {uuid,
130 "<span><a class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
131 end)
132
133 {subs, uuid_text}
134 end
135
136 @doc "Adds the hashtag links"
137 def add_hashtag_links({subs, text}, tags) do
138 tags =
139 tags
140 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
141 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
142
143 uuid_text =
144 tags
145 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
146 String.replace(text, match, uuid)
147 end)
148
149 subs =
150 subs ++
151 Enum.map(tags, fn {tag_text, tag, uuid} ->
152 url = "<a href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{tag_text}</a>"
153 {uuid, url}
154 end)
155
156 {subs, uuid_text}
157 end
158
159 def finalize({subs, text}) do
160 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
161 String.replace(result_text, uuid, replacement)
162 end)
163 end
164 end