From 529014c8f4efc2f953b3e93411234f24f80838d6 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 21 Jun 2019 19:05:47 +0700 Subject: [PATCH] Drop phone links support --- lib/auto_linker/builder.ex | 34 -------------------------- lib/auto_linker/parser.ex | 47 ------------------------------------ test/auto_linker_test.exs | 10 ++------ test/builder_test.exs | 25 ------------------- test/parser_test.exs | 49 ++------------------------------------ 5 files changed, 4 insertions(+), 161 deletions(-) diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex index e430576..2cf714b 100644 --- a/lib/auto_linker/builder.ex +++ b/lib/auto_linker/builder.ex @@ -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'#{h}' - 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}' - 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(#{uri}) end - - defp format_attributes(attrs) do - Enum.reduce(attrs, "", fn {name, value}, acc -> - acc <> ~s' #{name}="#{value}"' - end) - end end diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 78a9e8c..bf02cac 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -9,8 +9,6 @@ defmodule AutoLinker.Parser do @match_url ~r{^(?:\W*)?(?(?: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*(?https?:\/\/)?(?:[^@\n]+\\w@)?(?[^:#~\/\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 google.com} - - iex> AutoLinker.Parser.parse("call me at x9999", phone: true) - ~s{call me at x9999} - - iex> AutoLinker.Parser.parse("or at home on 555.555.5555", phone: true) - ~s{or at home on 555.555.5555} - - iex> AutoLinker.Parser.parse(", work (555) 555-5555", phone: true) - ~s{, work (555) 555-5555} """ 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( {<>, 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) diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index a21f009..4941fe8 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -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 (555) 555-5555} - end - test "default link" do assert AutoLinker.link("google.com") == "google.com" @@ -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 google.com ddg.com 888 888-8888 user@email.com irc:///mIRC" + "hello google.com ddg.com user@email.com irc:///mIRC" assert AutoLinker.link(text, - phone: true, email: true, extra: true, class: false, diff --git a/test/builder_test.exs b/test/builder_test.exs index 9fd332e..0b30065 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -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 x888. 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'555.555.5555 or ' <> - ~s'(555) 888-8888' - - assert create_phone_link([["555.555.5555", ""], ["(555) 888-8888"]], phrase, []) == expected - end - end - test "create_mention_link/3" do expected = "@navi" diff --git a/test/parser_test.exs b/test/parser_test.exs index cb4f8fb..7abeb6f 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -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 = "" - 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
google.com
" @@ -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/",