akkoma/lib/pleroma/formatter.ex

26 lines
863 B
Elixir
Raw Normal View History

2017-05-17 16:00:09 +00:00
defmodule Pleroma.Formatter do
alias Pleroma.User
2017-05-17 16:00:09 +00:00
2017-06-18 11:29:30 +00:00
@link_regex ~r/https?:\/\/[\w\.\/?=\-#%&]+[\w]/
2017-05-17 16:00:09 +00: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, String.downcase(tag)} end)
2017-05-17 16:00:09 +00:00
end
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 16:00:09 +00:00
end