2017-05-17 09:00:09 -07:00
|
|
|
defmodule Pleroma.Formatter do
|
2017-06-14 04:58:56 -07:00
|
|
|
alias Pleroma.User
|
2017-05-17 09:00:09 -07:00
|
|
|
|
2017-06-18 04:29:30 -07:00
|
|
|
@link_regex ~r/https?:\/\/[\w\.\/?=\-#%&]+[\w]/
|
2017-05-17 09:00:09 -07: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)
|
2017-06-18 10:04:46 -07:00
|
|
|
|> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, String.downcase(tag)} end)
|
2017-05-17 09:00:09 -07:00
|
|
|
end
|
2017-06-14 04:58:56 -07: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 09:00:09 -07:00
|
|
|
end
|