1 defmodule Pleroma.Formatter do
3 alias Pleroma.Web.MediaProxy
5 @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u
7 Regex.replace(@link_regex, text, "<a href='\\0'>\\0</a>")
11 def parse_tags(text, data \\ %{}) do
12 Regex.scan(@tag_regex, text)
13 |> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, String.downcase(tag)} end)
14 |> (fn map -> if data["sensitive"] in [true, "True", "true", "1"], do: [{"#nsfw", "nsfw"}] ++ map, else: map end).()
17 def parse_mentions(text) do
18 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
19 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])?)*/u
21 Regex.scan(regex, text)
24 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
25 |> Enum.filter(fn ({_match, user}) -> user end)
28 def html_escape(text) do
29 Regex.split(@link_regex, text, include_captures: true)
30 |> Enum.map_every(2, fn chunk ->
31 {:safe, part} = Phoenix.HTML.html_escape(chunk)
53 "four_seasons_of_bbq",
79 "pusa_hispida_saimensis",
103 @finmoji_with_filenames Enum.map(@finmoji, fn (finmoji) ->
104 {finmoji, "/finmoji/128px/#{finmoji}-128.png"}
107 @emoji_from_file (with {:ok, default} <- File.read("config/emoji.txt") do
109 with {:ok, custom} <- File.read("config/custom_emoji.txt") do
114 (default <> "\n" <> custom)
116 |> String.split(~r/\n+/)
117 |> Enum.map(fn(line) ->
118 [name, file] = String.split(line, ~r/,\s*/)
125 @emoji @finmoji_with_filenames ++ @emoji_from_file
127 def emojify(text, additional \\ nil) do
128 all_emoji = if additional do
129 Map.to_list(additional) ++ @emoji
134 Enum.reduce(all_emoji, text, fn ({emoji, file}, text) ->
135 emoji = HtmlSanitizeEx.strip_tags(emoji)
136 file = HtmlSanitizeEx.strip_tags(file)
137 String.replace(text, ":#{emoji}:", "<img height='32px' width='32px' alt='#{emoji}' title='#{emoji}' src='#{MediaProxy.url(file)}' />")
141 def get_emoji(text) do
142 Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end)
145 def get_custom_emoji() do