formatter.ex 4.56 KB
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Formatter do
2
  alias Pleroma.User
href's avatar
href committed
3
  alias Pleroma.Web.MediaProxy
4
  alias Pleroma.HTML
href's avatar
href committed
5
  alias Pleroma.Emoji
lain's avatar
lain committed
6 7

  @tag_regex ~r/\#\w+/u
8
  def parse_tags(text, data \\ %{}) do
lain's avatar
lain committed
9
    Regex.scan(@tag_regex, text)
lain's avatar
lain committed
10 11 12 13 14 15
    |> Enum.map(fn ["#" <> tag = full_tag] -> {full_tag, String.downcase(tag)} end)
    |> (fn map ->
          if data["sensitive"] in [true, "True", "true", "1"],
            do: [{"#nsfw", "nsfw"}] ++ map,
            else: map
        end).()
lain's avatar
lain committed
16
  end
17 18 19

  def parse_mentions(text) do
    # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
lain's avatar
lain committed
20
    regex =
href's avatar
href committed
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)
lain's avatar
lain committed
24 25 26 27 28 29
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.map(fn "@" <> match = full_match ->
      {full_match, User.get_cached_by_nickname(match)}
    end)
    |> Enum.filter(fn {_match, user} -> user end)
30
  end
lain's avatar
lain committed
31

href's avatar
href committed
32 33 34
  def emojify(text) do
    emojify(text, Emoji.get_all())
  end
lain's avatar
lain committed
35

lain's avatar
lain committed
36
  def emojify(text, nil), do: text
lain's avatar
lain committed
37

lain's avatar
lain committed
38
  def emojify(text, emoji) do
lain's avatar
lain committed
39
    Enum.reduce(emoji, text, fn {emoji, file}, text ->
40 41
      emoji = HTML.strip_tags(emoji)
      file = HTML.strip_tags(file)
lain's avatar
lain committed
42 43 44 45 46 47 48 49

      String.replace(
        text,
        ":#{emoji}:",
        "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{
          MediaProxy.url(file)
        }' />"
      )
50
      |> HTML.filter_tags()
lain's avatar
lain committed
51 52
    end)
  end
lain's avatar
lain committed
53

54
  def get_emoji(text) when is_binary(text) do
href's avatar
href committed
55
    Enum.filter(Emoji.get_all(), fn {emoji, _} -> String.contains?(text, ":#{emoji}:") end)
lain's avatar
lain committed
56
  end
eal's avatar
eal committed
57

58 59
  def get_emoji(_), do: []

60 61
  @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui

62 63
  @uri_schemes Application.get_env(:pleroma, :uri_schemes, [])
  @valid_schemes Keyword.get(@uri_schemes, :valid_schemes, [])
lain's avatar
lain committed
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 69 70
  end

  def html_escape(text, "text/plain") do
lain's avatar
lain committed
71 72 73 74 75 76 77 78
    Regex.split(@link_regex, text, include_captures: true)
    |> Enum.map_every(2, fn chunk ->
      {:safe, part} = Phoenix.HTML.html_escape(chunk)
      part
    end)
    |> Enum.join("")
  end

79
  @doc "changes scheme:... urls to html links"
lain's avatar
lain committed
80
  def add_links({subs, text}) do
lain's avatar
lain committed
81
    links =
82 83
      text
      |> String.split([" ", "\t", "<br>"])
84
      |> Enum.filter(fn word -> String.starts_with?(word, @valid_schemes) end)
85 86
      |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
      |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
Thog's avatar
Thog committed
87
      |> Enum.sort_by(fn {_, url} -> -String.length(url) end)
lain's avatar
lain committed
88

lain's avatar
lain committed
89 90 91
    uuid_text =
      links
      |> Enum.reduce(text, fn {uuid, url}, acc -> String.replace(acc, url, uuid) end)
lain's avatar
lain committed
92

lain's avatar
lain committed
93 94 95
    subs =
      subs ++
        Enum.map(links, fn {uuid, url} ->
96
          {uuid, "<a href=\"#{url}\">#{url}</a>"}
lain's avatar
lain committed
97
        end)
lain's avatar
lain committed
98 99 100 101 102 103

    {subs, uuid_text}
  end

  @doc "Adds the links to mentioned users"
  def add_user_links({subs, text}, mentions) do
lain's avatar
lain committed
104 105 106 107 108 109 110 111 112 113 114 115 116
    mentions =
      mentions
      |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
      |> Enum.map(fn {name, user} -> {name, user, Ecto.UUID.generate()} end)

    uuid_text =
      mentions
      |> Enum.reduce(text, fn {match, _user, uuid}, text ->
        String.replace(text, match, uuid)
      end)

    subs =
      subs ++
ValD's avatar
ValD committed
117
        Enum.map(mentions, fn {match, %User{id: id, ap_id: ap_id, info: info}, uuid} ->
kaniini's avatar
kaniini committed
118
          ap_id =
lain's avatar
lain committed
119 120
            if is_binary(info.source_data["url"]) do
              info.source_data["url"]
kaniini's avatar
kaniini committed
121 122 123
            else
              ap_id
            end
lain's avatar
lain committed
124

lain's avatar
lain committed
125
          short_match = String.split(match, "@") |> tl() |> hd()
kaniini's avatar
kaniini committed
126 127

          {uuid,
ValD's avatar
ValD committed
128
           "<span><a data-user='#{id}' class='mention' href='#{ap_id}'>@<span>#{short_match}</span></a></span>"}
lain's avatar
lain committed
129
        end)
lain's avatar
lain committed
130 131 132 133 134 135

    {subs, uuid_text}
  end

  @doc "Adds the hashtag links"
  def add_hashtag_links({subs, text}, tags) do
lain's avatar
lain committed
136 137 138 139 140 141 142 143 144 145 146 147 148
    tags =
      tags
      |> Enum.sort_by(fn {name, _} -> -String.length(name) end)
      |> Enum.map(fn {name, short} -> {name, short, Ecto.UUID.generate()} end)

    uuid_text =
      tags
      |> Enum.reduce(text, fn {match, _short, uuid}, text ->
        String.replace(text, match, uuid)
      end)

    subs =
      subs ++
kaniini's avatar
kaniini committed
149
        Enum.map(tags, fn {tag_text, tag, uuid} ->
ValD's avatar
ValD committed
150 151 152 153 154
          url =
            "<a data-tag='#{tag}' href='#{Pleroma.Web.base_url()}/tag/#{tag}' rel='tag'>#{
              tag_text
            }</a>"

lain's avatar
lain committed
155 156
          {uuid, url}
        end)
lain's avatar
lain committed
157 158 159 160 161

    {subs, uuid_text}
  end

  def finalize({subs, text}) do
lain's avatar
lain committed
162
    Enum.reduce(subs, text, fn {uuid, replacement}, result_text ->
lain's avatar
lain committed
163 164 165
      String.replace(result_text, uuid, replacement)
    end)
  end
lain's avatar
lain committed
166
end