Merge develop to bump elixir version in the CI so I don't get failing formatting
[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 @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
12 @link_regex ~r{((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+}ui
13 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
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 mention_handler("@" <> nickname, buffer, opts, acc) do
21 case User.get_cached_by_nickname(nickname) do
22 %User{id: id} = user ->
23 ap_id = get_ap_id(user)
24 nickname_text = get_nickname_text(nickname, opts) |> maybe_escape(opts)
25
26 link =
27 "<span class='h-card'><a data-user='#{id}' class='u-url mention' href='#{ap_id}'>@<span>#{
28 nickname_text
29 }</span></a></span>"
30
31 {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}}
32
33 _ ->
34 {buffer, acc}
35 end
36 end
37
38 def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
39 tag = String.downcase(tag)
40 url = "#{Pleroma.Web.base_url()}/tag/#{tag}"
41 link = "<a class='hashtag' data-tag='#{tag}' href='#{url}' rel='tag'>#{tag_text}</a>"
42
43 {link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}
44 end
45
46 @doc """
47 Parses a text and replace plain text links with HTML. Returns a tuple with a result text, mentions, and hashtags.
48 """
49 @spec linkify(String.t(), keyword()) ::
50 {String.t(), [{String.t(), User.t()}], [{String.t(), String.t()}]}
51 def linkify(text, options \\ []) do
52 options = options ++ @auto_linker_config
53 acc = %{mentions: MapSet.new(), tags: MapSet.new()}
54 {text, %{mentions: mentions, tags: tags}} = AutoLinker.link_map(text, acc, options)
55
56 {text, MapSet.to_list(mentions), MapSet.to_list(tags)}
57 end
58
59 def emojify(text) do
60 emojify(text, Emoji.get_all())
61 end
62
63 def emojify(text, nil), do: text
64
65 def emojify(text, emoji, strip \\ false) do
66 Enum.reduce(emoji, text, fn {emoji, file}, text ->
67 emoji = HTML.strip_tags(emoji)
68 file = HTML.strip_tags(file)
69
70 html =
71 if not strip do
72 "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
73 MediaProxy.url(file)
74 }' />"
75 else
76 ""
77 end
78
79 String.replace(text, ":#{emoji}:", html) |> HTML.filter_tags()
80 end)
81 end
82
83 def demojify(text) do
84 emojify(text, Emoji.get_all(), true)
85 end
86
87 def demojify(text, nil), do: text
88
89 def get_emoji(text) when is_binary(text) do
90 Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
91 end
92
93 def get_emoji(_), do: []
94
95 def html_escape({text, mentions, hashtags}, type) do
96 {html_escape(text, type), mentions, hashtags}
97 end
98
99 def html_escape(text, "text/html") do
100 HTML.filter_tags(text)
101 end
102
103 def html_escape(text, "text/plain") do
104 Regex.split(@link_regex, text, include_captures: true)
105 |> Enum.map_every(2, fn chunk ->
106 {:safe, part} = Phoenix.HTML.html_escape(chunk)
107 part
108 end)
109 |> Enum.join("")
110 end
111
112 def truncate(text, max_length \\ 200, omission \\ "...") do
113 # Remove trailing whitespace
114 text = Regex.replace(~r/([^ \t\r\n])([ \t]+$)/u, text, "\\g{1}")
115
116 if String.length(text) < max_length do
117 text
118 else
119 length_with_omission = max_length - String.length(omission)
120 String.slice(text, 0, length_with_omission) <> omission
121 end
122 end
123
124 defp get_ap_id(%User{info: %{source_data: %{"url" => url}}}) when is_binary(url), do: url
125 defp get_ap_id(%User{ap_id: ap_id}), do: ap_id
126
127 defp get_nickname_text(nickname, %{mentions_format: :full}), do: User.full_nickname(nickname)
128 defp get_nickname_text(nickname, _), do: User.local_nickname(nickname)
129
130 defp maybe_escape(str, %{mentions_escape: true}) do
131 String.replace(str, @markdown_characters_regex, "\\\\\\1")
132 end
133
134 defp maybe_escape(str, _), do: str
135 end