Drop phone links support
This commit is contained in:
parent
b044a63910
commit
529014c8f4
5 changed files with 4 additions and 161 deletions
|
@ -75,34 +75,6 @@ defmodule AutoLinker.Builder do
|
|||
|
||||
defp strip_prefix(url, _), do: url
|
||||
|
||||
def create_phone_link([], buffer, _), do: buffer
|
||||
|
||||
def create_phone_link([h | t], buffer, opts) do
|
||||
create_phone_link(t, format_phone_link(h, buffer, opts), opts)
|
||||
end
|
||||
|
||||
def format_phone_link([h | _], buffer, opts) do
|
||||
val =
|
||||
h
|
||||
|> String.replace(~r/[\.\+\- x\(\)]+/, "")
|
||||
|> format_phone_link(h, opts)
|
||||
|
||||
# val = ~s'<a href="#" class="phone-number" data-phone="#{number}">#{h}</a>'
|
||||
String.replace(buffer, h, val)
|
||||
end
|
||||
|
||||
def format_phone_link(number, original, opts) do
|
||||
tag = opts[:tag] || "a"
|
||||
class = opts[:class] || "phone-number"
|
||||
data_phone = opts[:data_phone] || "data-phone"
|
||||
attrs = format_attributes(opts[:attributes] || [])
|
||||
href = opts[:href] || "#"
|
||||
|
||||
~s'<#{tag} href="#{href}" class="#{class}" #{data_phone}="#{number}"#{attrs}>#{original}</#{
|
||||
tag
|
||||
}>'
|
||||
end
|
||||
|
||||
def create_mention_link("@" <> name, _buffer, opts) do
|
||||
mention_prefix = opts[:mention_prefix]
|
||||
|
||||
|
@ -164,10 +136,4 @@ defmodule AutoLinker.Builder do
|
|||
attrs = format_attrs(attrs)
|
||||
~s(<a #{attrs}>#{uri}</a>)
|
||||
end
|
||||
|
||||
defp format_attributes(attrs) do
|
||||
Enum.reduce(attrs, "", fn {name, value}, acc ->
|
||||
acc <> ~s' #{name}="#{value}"'
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,8 +9,6 @@ defmodule AutoLinker.Parser do
|
|||
|
||||
@match_url ~r{^(?:\W*)?(?<url>(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u
|
||||
|
||||
@match_phone ~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))"
|
||||
|
||||
@match_hostname ~r{^\W*(?<scheme>https?:\/\/)?(?:[^@\n]+\\w@)?(?<host>[^:#~\/\n?]+)}u
|
||||
|
||||
@match_ip ~r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
|
||||
|
@ -51,15 +49,6 @@ defmodule AutoLinker.Parser do
|
|||
|
||||
iex> AutoLinker.Parser.parse("Check out google.com")
|
||||
~s{Check out <a href="http://google.com" class="auto-linker" target="_blank" rel="noopener noreferrer">google.com</a>}
|
||||
|
||||
iex> AutoLinker.Parser.parse("call me at x9999", phone: true)
|
||||
~s{call me at <a href="#" class="phone-number" data-phone="9999">x9999</a>}
|
||||
|
||||
iex> AutoLinker.Parser.parse("or at home on 555.555.5555", phone: true)
|
||||
~s{or at home on <a href="#" class="phone-number" data-phone="5555555555">555.555.5555</a>}
|
||||
|
||||
iex> AutoLinker.Parser.parse(", work (555) 555-5555", phone: true)
|
||||
~s{, work <a href="#" class="phone-number" data-phone="5555555555">(555) 555-5555</a>}
|
||||
"""
|
||||
|
||||
def parse(input, opts \\ %{})
|
||||
|
@ -88,15 +77,8 @@ defmodule AutoLinker.Parser do
|
|||
do_parse(input, Map.merge(config, opts))
|
||||
end
|
||||
|
||||
defp do_parse(input, %{phone: false} = opts), do: do_parse(input, Map.delete(opts, :phone))
|
||||
defp do_parse(input, %{url: false} = opts), do: do_parse(input, Map.delete(opts, :url))
|
||||
|
||||
defp do_parse(input, %{phone: _} = opts) do
|
||||
input
|
||||
|> do_parse(opts, {"", "", :parsing}, &check_and_link_phone/3)
|
||||
|> do_parse(Map.delete(opts, :phone))
|
||||
end
|
||||
|
||||
defp do_parse(input, %{hashtag: true} = opts) do
|
||||
input
|
||||
|> do_parse(opts, {"", "", :parsing}, &check_and_link_hashtag/3)
|
||||
|
@ -210,15 +192,6 @@ defmodule AutoLinker.Parser do
|
|||
do_parse({text, user_acc}, opts, {"", acc <> buffer, {:attrs, level}}, handler)
|
||||
end
|
||||
|
||||
# default cases where state is not important
|
||||
defp do_parse(
|
||||
{" " <> text, user_acc},
|
||||
%{phone: _} = opts,
|
||||
{buffer, acc, state},
|
||||
handler
|
||||
),
|
||||
do: do_parse({text, user_acc}, opts, {buffer <> " ", acc, state}, handler)
|
||||
|
||||
defp do_parse(
|
||||
{<<char::bytes-size(1), text::binary>>, user_acc},
|
||||
opts,
|
||||
|
@ -273,12 +246,6 @@ defmodule AutoLinker.Parser do
|
|||
if email?(buffer, opts), do: link_email(buffer, opts), else: buffer
|
||||
end
|
||||
|
||||
def check_and_link_phone(buffer, opts, _user_acc) do
|
||||
buffer
|
||||
|> match_phone
|
||||
|> link_phone(buffer, opts)
|
||||
end
|
||||
|
||||
def check_and_link_mention(buffer, opts, user_acc) do
|
||||
buffer
|
||||
|> match_mention
|
||||
|
@ -340,14 +307,6 @@ defmodule AutoLinker.Parser do
|
|||
|
||||
def ip?(buffer), do: Regex.match?(@match_ip, buffer)
|
||||
|
||||
@doc false
|
||||
def match_phone(buffer) do
|
||||
case Regex.scan(@match_phone, buffer) do
|
||||
[] -> nil
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
def match_mention(buffer) do
|
||||
case Regex.run(@match_mention, buffer) do
|
||||
[mention] -> mention
|
||||
|
@ -402,12 +361,6 @@ defmodule AutoLinker.Parser do
|
|||
|
||||
defp maybe_update_buffer(out, _match, _buffer), do: out
|
||||
|
||||
def link_phone(nil, buffer, _), do: buffer
|
||||
|
||||
def link_phone(list, buffer, opts) do
|
||||
Builder.create_phone_link(list, buffer, opts)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def link_url(buffer, opts) do
|
||||
Builder.create_link(buffer, opts)
|
||||
|
|
|
@ -2,11 +2,6 @@ defmodule AutoLinkerTest do
|
|||
use ExUnit.Case, async: true
|
||||
doctest AutoLinker
|
||||
|
||||
test "phone number" do
|
||||
assert AutoLinker.link(", work (555) 555-5555", phone: true) ==
|
||||
~s{, work <a href="#" class="phone-number" data-phone="5555555555">(555) 555-5555</a>}
|
||||
end
|
||||
|
||||
test "default link" do
|
||||
assert AutoLinker.link("google.com") ==
|
||||
"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
|
||||
|
@ -18,13 +13,12 @@ defmodule AutoLinkerTest do
|
|||
end
|
||||
|
||||
test "all kinds of links" do
|
||||
text = "hello google.com https://ddg.com 888 888-8888 user@email.com irc:///mIRC"
|
||||
text = "hello google.com https://ddg.com user@email.com irc:///mIRC"
|
||||
|
||||
expected =
|
||||
"hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"#\" class=\"phone-number\" data-phone=\"8888888888\">888 888-8888</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
|
||||
"hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
|
||||
|
||||
assert AutoLinker.link(text,
|
||||
phone: true,
|
||||
email: true,
|
||||
extra: true,
|
||||
class: false,
|
||||
|
|
|
@ -46,31 +46,6 @@ defmodule AutoLinker.BuilderTest do
|
|||
assert format_mention(%{href: "url"}, "user@host", nil) == expected
|
||||
end
|
||||
|
||||
describe "create_phone_link" do
|
||||
test "finishes" do
|
||||
assert create_phone_link([], "", []) == ""
|
||||
end
|
||||
|
||||
test "handles one link" do
|
||||
phrase = "my exten is x888. Call me."
|
||||
|
||||
expected =
|
||||
~s'my exten is <a href="#" class="phone-number" data-phone="888" test=\"test\">x888</a>. Call me.'
|
||||
|
||||
assert create_phone_link([["x888", ""]], phrase, attributes: [test: "test"]) == expected
|
||||
end
|
||||
|
||||
test "handles multiple links" do
|
||||
phrase = "555.555.5555 or (555) 888-8888"
|
||||
|
||||
expected =
|
||||
~s'<a href="#" class="phone-number" data-phone="5555555555">555.555.5555</a> or ' <>
|
||||
~s'<a href="#" class="phone-number" data-phone="5558888888">(555) 888-8888</a>'
|
||||
|
||||
assert create_phone_link([["555.555.5555", ""], ["(555) 888-8888"]], phrase, []) == expected
|
||||
end
|
||||
end
|
||||
|
||||
test "create_mention_link/3" do
|
||||
expected =
|
||||
"<a href=\"/u/navi\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">@navi</a>"
|
||||
|
|
|
@ -106,22 +106,6 @@ defmodule AutoLinker.ParserTest do
|
|||
end
|
||||
end
|
||||
|
||||
describe "match_phone" do
|
||||
test "valid" do
|
||||
valid_phone_nunbers()
|
||||
|> Enum.each(fn number ->
|
||||
assert number |> match_phone() |> valid_number?(number)
|
||||
end)
|
||||
end
|
||||
|
||||
test "invalid" do
|
||||
invalid_phone_numbers()
|
||||
|> Enum.each(fn number ->
|
||||
assert number |> match_phone() |> is_nil
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
describe "parse" do
|
||||
test "handle line breakes" do
|
||||
text = "google.com\r\nssss"
|
||||
|
@ -157,7 +141,7 @@ defmodule AutoLinker.ParserTest do
|
|||
|
||||
expected = "<div><a href=\"http://google.com\">google.com</a></div>"
|
||||
|
||||
assert parse(text, class: false, rel: false, new_window: false, phone: false) == expected
|
||||
assert parse(text, class: false, rel: false, new_window: false) == expected
|
||||
|
||||
text = "Check out <div class='section'>google.com</div>"
|
||||
|
||||
|
@ -196,7 +180,7 @@ defmodule AutoLinker.ParserTest do
|
|||
|
||||
test "do not link urls" do
|
||||
text = "google.com"
|
||||
assert parse(text, url: false, phone: true) == text
|
||||
assert parse(text, url: false) == text
|
||||
end
|
||||
|
||||
test "do not link `:test.test`" do
|
||||
|
@ -260,35 +244,6 @@ defmodule AutoLinker.ParserTest do
|
|||
"555.555.5555"
|
||||
]
|
||||
|
||||
def valid_phone_nunbers,
|
||||
do: [
|
||||
"x55",
|
||||
"x555",
|
||||
"x5555",
|
||||
"x12345",
|
||||
"+1 555 555-5555",
|
||||
"555 555-5555",
|
||||
"555.555.5555",
|
||||
"613-555-5555",
|
||||
"1 (555) 555-5555",
|
||||
"(555) 555-5555",
|
||||
"1.555.555.5555",
|
||||
"800 555-5555",
|
||||
"1.800.555.5555",
|
||||
"1 (800) 555-5555",
|
||||
"888 555-5555",
|
||||
"887 555-5555",
|
||||
"1-877-555-5555",
|
||||
"1 800 710-5515"
|
||||
]
|
||||
|
||||
def invalid_phone_numbers,
|
||||
do: [
|
||||
"5555",
|
||||
"x5",
|
||||
"(555) 555-55"
|
||||
]
|
||||
|
||||
def custom_tld_scheme_urls,
|
||||
do: [
|
||||
"http://whatever.null/",
|
||||
|
|
Loading…
Reference in a new issue