Merge remote-tracking branch 'upstream/develop' into admin-create-users
[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
7 alias Pleroma.HTML
8 alias Pleroma.User
9 alias Pleroma.Web.MediaProxy
10
11 @safe_mention_regex ~r/^(\s*(?<mentions>(@.+?\s+){1,})+)(?<rest>.*)/s
12 @link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
13 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
14
15 @auto_linker_config hashtag: true,
16 hashtag_handler: &Pleroma.Formatter.hashtag_handler/4,
17 mention: true,
18 mention_handler: &Pleroma.Formatter.mention_handler/4
19
20 def escape_mention_handler("@" <> nickname = mention, buffer, _, _) do
21 case User.get_cached_by_nickname(nickname) do
22 %User{} ->
23 # escape markdown characters with `\\`
24 # (we don't want something like @user__name to be parsed by markdown)
25 String.replace(mention, @markdown_characters_regex, "\\\\\\1")
26
27 _ ->
28 buffer
29 end
30 end
31
32 def mention_handler("@" <> nickname, buffer, opts, acc) do
33 case User.get_cached_by_nickname(nickname) do
34 %User{id: id} = user ->
35 ap_id = get_ap_id(user)
36 nickname_text = get_nickname_text(nickname, opts)
37
38 link =
39 "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>@<span>#{
40 nickname_text
41 }</span></a></span>"
42
43 {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
44
45 _ ->
46 {buffer, acc}
47 end
48 end
49
50 def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
51 tag = String.downcase(tag)
52 url = "#{Pleroma.Web.base_url()}/tag/#{tag}"
53 link = "<a class='hashtag' data-tag='#{tag}' href='#{url}' rel='tag'>#{tag_text}</a>"
54
55 {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
56 end
57
58 @doc """
59 Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
60
61 If the 'safe_mention' option is given, only consecutive mentions at the start the post are actually mentioned.
62 """
63 @spec linkify(String.t(), keyword()) ::
64 {String.t(), [{String.t(), User.t()}], [{String.t(), String.t()}]}
65 def linkify(text, options \\ []) do
66 options = options ++ @auto_linker_config
67
68 if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
69 %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
70 acc = %{mentions: MapSet.new(), tags: MapSet.new()}
71
72 {text_mentions, %{mentions: mentions}} = AutoLinker.link_map(mentions, acc, options)
73 {text_rest, %{tags: tags}} = AutoLinker.link_map(rest, acc, options)
74
75 {text_mentions <> text_rest, MapSet.to_list(mentions), MapSet.to_list(tags)}
76 else
77 acc = %{mentions: MapSet.new(), tags: MapSet.new()}
78 {text, %{mentions: mentions, tags: tags}} = AutoLinker.link_map(text, acc, options)
79
80 {text, MapSet.to_list(mentions), MapSet.to_list(tags)}
81 end
82 end
83
84 @doc """
85 Escapes a special characters in mention names.
86 """
87 def mentions_escape(text, options \\ []) do
88 options =
89 Keyword.merge(options,
90 mention: true,
91 url: false,
92 mention_handler: &Pleroma.Formatter.escape_mention_handler/4
93 )
94
95 if options[:safe_mention] && Regex.named_captures(@safe_mention_regex, text) do
96 %{"mentions" => mentions, "rest" => rest} = Regex.named_captures(@safe_mention_regex, text)
97 AutoLinker.link(mentions, options) <> AutoLinker.link(rest, options)
98 else
99 AutoLinker.link(text, options)
100 end
101 end
102
103 def emojify(text) do
104 emojify(text, Emoji.get_all())
105 end
106
107 def emojify(text, nil), do: text
108
109 def emojify(text, emoji, strip \\ false) do
110 Enum.reduce(emoji, text, fn emoji_data, text ->
111 emoji = HTML.strip_tags(elem(emoji_data, 0))
112 file = HTML.strip_tags(elem(emoji_data, 1))
113
114 html =
115 if not strip do
116 "<img class='emoji' alt='#{emoji}' title='#{emoji}' src='#{MediaProxy.url(file)}' />"
117 else
118 ""
119 end
120
121 String.replace(text, ":#{emoji}:", html) |> HTML.filter_tags()
122 end)
123 end
124
125 def demojify(text) do
126 emojify(text, Emoji.get_all(), true)
127 end
128
129 def demojify(text, nil), do: text
130
131 @doc "Outputs a list of the emoji-shortcodes in a text"
132 def get_emoji(text) when is_binary(text) do
133 Enum.filter(Emoji.get_all(), fn {emoji, _, _} -> String.contains?(text, ":#{emoji}:") end)
134 end
135
136 def get_emoji(_), do: []
137
138 @doc "Outputs a list of the emoji-Maps in a text"
139 def get_emoji_map(text) when is_binary(text) do
140 get_emoji(text)
141 |> Enum.reduce(%{}, fn {name, file, _group}, acc ->
142 Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url()}#{file}")
143 end)
144 end
145
146 def get_emoji_map(_), do: []
147
148 def html_escape({text, mentions, hashtags}, type) do
149 {html_escape(text, type), mentions, hashtags}
150 end
151
152 def html_escape(text, "text/html") do
153 HTML.filter_tags(text)
154 end
155
156 def html_escape(text, "text/plain") do
157 Regex.split(@link_regex, text, include_captures: true)
158 |> Enum.map_every(2, fn chunk ->
159 {:safe, part} = Phoenix.HTML.html_escape(chunk)
160 part
161 end)
162 |> Enum.join("")
163 end
164
165 def truncate(text, max_length \\ 200, omission \\ "...") do
166 # Remove trailing whitespace
167 text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
168
169 if String.length(text) < max_length do
170 text
171 else
172 length_with_omission = max_length - String.length(omission)
173 String.slice(text, 0, length_with_omission) <> omission
174 end
175 end
176
177 defp get_ap_id(%User{info: %{source_data: %{"url" => url}}}) when is_binary(url), do: url
178 defp get_ap_id(%User{ap_id: ap_id}), do: ap_id
179
180 defp get_nickname_text(nickname, %{mentions_format: :full}), do: User.full_nickname(nickname)
181 defp get_nickname_text(nickname, _), do: User.local_nickname(nickname)
182 end