formatter.ex 846 Bytes
Newer Older
lain's avatar
lain committed
1
defmodule Pleroma.Formatter do
2
  alias Pleroma.User
lain's avatar
lain committed
3

lain's avatar
lain committed
4
  @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&]+[\w]/
lain's avatar
lain committed
5
6
7
8
9
10
11
12
13
  def linkify(text) do
    Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>")
  end

  @tag_regex ~r/\#\w+/u
  def parse_tags(text) do
    Regex.scan(@tag_regex, text)
    |> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, tag} end)
  end
14
15
16
17
18
19
20
21
22
23
24

  def parse_mentions(text) do
    # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
    regex = ~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])?)*/

    Regex.scan(regex, text)
    |> 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)
  end
lain's avatar
lain committed
25
end