added data attrs for user and tag
[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 def emojify(text) do
33 emojify(text, Emoji.get_all())
34 end
35
36 def emojify(text, nil), do: text
37
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)
42
43 String.replace(
44 text,
45 ":#{emoji}:",
46 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
47 MediaProxy.url(file)
48 }' />"
49 )
50 |> HTML.filter_tags()
51 end)
52 end
53
54 def get_emoji(text) when is_binary(text) do
55 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
56 end
57
58 def get_emoji(_), do: []
59
60 @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
61
62 @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
63 @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
64
65 # TODO: make it use something other than @link_regex
66 def html_escape(text, "text/html") do
67 HTML.filter_tags(text)
68 end
69
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)
74 part
75 end)
76 |> Enum.join("")
77 end
78
79 @doc "changes scheme:... urls to html links"
80 def add_links({subs, text}) do
81 links =
82 text
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)
88
89 uuid_text =
90 links
91 |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
92
93 subs =
94 subs ++
95 Enum.map(links, fn {uuid, url} ->
96 {uuid, "<a href=\"#{url}\">#{url}</a>"}
97 end)
98
99 {subs, uuid_text}
100 end
101
102 @doc "Adds the links to mentioned users"
103 def add_user_links({subs, text}, mentions) do
104 mentions =
105 mentions
106 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
107 |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)
108
109 uuid_text =
110 mentions
111 |> Enum.reduce(text, fn {match, _user, uuid}, text ->
112 String.replace(text, match, uuid)
113 end)
114
115 subs =
116 subs ++
117 Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
118 ap_id =
119 if is_binary(info.source_data["url"]) do
120 info.source_data["url"]
121 else
122 ap_id
123 end
124
125 short_match = String.split(match, "@") |> tl() |> hd()
126
127 {uuid,
128 "<span><a data-user='#{id}' class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
129 end)
130
131 {subs, uuid_text}
132 end
133
134 @doc "Adds the hashtag links"
135 def add_hashtag_links({subs, text}, tags) do
136 tags =
137 tags
138 |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
139 |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)
140
141 uuid_text =
142 tags
143 |> Enum.reduce(text, fn {match, _short, uuid}, text ->
144 String.replace(text, match, uuid)
145 end)
146
147 subs =
148 subs ++
149 Enum.map(tags, fn {tag_text, tag, uuid} ->
150 url =
151 "<a data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
152 tag_text
153 }</a>"
154
155 {uuid, url}
156 end)
157
158 {subs, uuid_text}
159 end
160
161 def finalize({subs, text}) do
162 Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
163 String.replace(result_text, uuid, replacement)
164 end)
165 end
166 end