2017-05-17 10:00:09 -06:00
|
|
|
defmodule Pleroma.Formatter do
|
2017-06-14 05:58:56 -06:00
|
|
|
alias Pleroma.User
|
2017-05-17 10:00:09 -06:00
|
|
|
|
2017-06-18 05:29:30 -06:00
|
|
|
@link_regex ~r/https?:\/\/[\w\.\/?=\-#%&]+[\w]/
|
2017-05-17 10:00:09 -06:00
|
|
|
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
|
2017-06-14 05:58:56 -06:00
|
|
|
|
|
|
|
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
|
2017-05-17 10:00:09 -06:00
|
|
|
end
|