X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fformatter.ex;h=cf2944c38fffa668ba6526dfd415af9ea10458ef;hb=aab2bdddf4456c99d5d5042183c1225f7e771f56;hp=53e2c204f30c0e6c542b00b63485ab0555ae7fa8;hpb=1afd6d37bd2febb58ee09d273b9c7d690022d3c3;p=akkoma
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex
index 53e2c204f..cf2944c38 100644
--- a/lib/pleroma/formatter.ex
+++ b/lib/pleroma/formatter.ex
@@ -16,7 +16,7 @@ defmodule Pleroma.Formatter do
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])?)*/u
+ ~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
Regex.scan(regex, text)
|> List.flatten()
@@ -116,7 +116,28 @@ defmodule Pleroma.Formatter do
_ -> []
end)
- @emoji @finmoji_with_filenames ++ @emoji_from_file
+ @emoji_from_globs (
+ static_path = Path.join(:code.priv_dir(:pleroma), "static")
+
+ globs =
+ Application.get_env(:pleroma, :emoji, [])
+ |> Keyword.get(:shortcode_globs, [])
+
+ paths =
+ Enum.map(globs, fn glob ->
+ Path.join(static_path, glob)
+ |> Path.wildcard()
+ end)
+ |> Enum.concat()
+
+ Enum.map(paths, fn path ->
+ shortcode = Path.basename(path, Path.extname(path))
+ external_path = Path.join("/", Path.relative_to(path, static_path))
+ {shortcode, external_path}
+ end)
+ )
+
+ @emoji @finmoji_with_filenames ++ @emoji_from_globs ++ @emoji_from_file
def emojify(text, emoji \\ @emoji)
def emojify(text, nil), do: text
@@ -144,8 +165,29 @@ defmodule Pleroma.Formatter do
@emoji
end
- @link_regex ~r/https?:\/\/[\w\.\/?=\-#\+%&@~'\(\):]+[\w\/]/u
+ @link_regex ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
+ # IANA got a list https://www.iana.org/assignments/uri-schemes/ but
+ # Stuff like ipfs isnât in it
+ # There is very niche stuff
+ @uri_schemes [
+ "https://",
+ "http://",
+ "dat://",
+ "dweb://",
+ "gopher://",
+ "ipfs://",
+ "ipns://",
+ "irc:",
+ "ircs:",
+ "magnet:",
+ "mailto:",
+ "mumble:",
+ "ssb://",
+ "xmpp:"
+ ]
+
+ # TODO: make it use something other than @link_regex
def html_escape(text) do
Regex.split(@link_regex, text, include_captures: true)
|> Enum.map_every(2, fn chunk ->
@@ -155,11 +197,18 @@ defmodule Pleroma.Formatter do
|> Enum.join("")
end
- @doc "changes http:... links to html links"
+ @doc "changes scheme:... urls to html links"
def add_links({subs, text}) do
+ additionnal_schemes =
+ Application.get_env(:pleroma, :uri_schemes, [])
+ |> Keyword.get(:additionnal_schemes, [])
+
links =
- Regex.scan(@link_regex, text)
- |> Enum.map(fn [url] -> {Ecto.UUID.generate(), url} end)
+ text
+ |> String.split([" ", "\t", "
"])
+ |> Enum.filter(fn word -> String.starts_with?(word, @uri_schemes ++ additionnal_schemes) end)
+ |> Enum.filter(fn word -> Regex.match?(@link_regex, word) end)
+ |> Enum.map(fn url -> {Ecto.UUID.generate(), url} end)
|> Enum.sort_by(fn {_, url} -> -String.length(url) end)
uuid_text =
@@ -200,7 +249,9 @@ defmodule Pleroma.Formatter do
ap_id = info["source_data"]["url"] || ap_id
short_match = String.split(match, "@") |> tl() |> hd()
- {uuid, "@#{short_match}"}
+
+ {uuid,
+ "@#{short_match}"}
end)
{subs, uuid_text}
@@ -221,8 +272,8 @@ defmodule Pleroma.Formatter do
subs =
subs ++
- Enum.map(tags, fn {_, tag, uuid} ->
- url = "##{tag}"
+ Enum.map(tags, fn {tag_text, tag, uuid} ->
+ url = "#{tag_text}"
{uuid, url}
end)