logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git
commit: c19c7afa5b3ae417001cc9b090c792383bed48f8
parent c8026cd16c37079e3b2468f06f37f7dcd6aa3f95
Author: rinpatch <rinpatch@sdf.org>
Date:   Tue, 18 Jun 2019 12:56:17 +0300

Refactor url?/2 to use opts

Diffstat:

Mlib/auto_linker/parser.ex19+++++++++----------
Mtest/parser_test.exs22+++++++++++-----------
2 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex @@ -262,7 +262,7 @@ defmodule AutoLinker.Parser do def check_and_link(buffer, opts, _user_acc) do str = strip_parens(buffer) - if url?(str, opts[:scheme], opts[:validate_tld]) do + if url?(str, opts) do case parse_link(str, opts) do ^buffer -> link_url(buffer, opts) url -> String.replace(buffer, url, link_url(url, opts)) @@ -315,16 +315,15 @@ defmodule AutoLinker.Parser do end # @doc false - def url?(buffer, scheme, validate_tld \\ true) - def url?(buffer, true, validate_tld) do - valid_url?(buffer) && Regex.match?(@match_scheme, buffer) && - (!validate_tld or validate_tld == :no_scheme || valid_tld?(buffer)) - end - - def url?(buffer, _, validate_tld) do - valid_url?(buffer) && Regex.match?(@match_url, buffer) && - (validate_tld == false || valid_tld?(buffer)) + def url?(buffer, opts) do + if opts[:scheme] do + valid_url?(buffer) && Regex.match?(@match_scheme, buffer) && + (!opts[:validate_tld] or opts[:validate_tld] == :no_scheme || valid_tld?(buffer)) + else + valid_url?(buffer) && Regex.match?(@match_url, buffer) && + (opts[:validate_tld] == false || valid_tld?(buffer)) + end end def email?(buffer) do diff --git a/test/parser_test.exs b/test/parser_test.exs @@ -4,74 +4,74 @@ defmodule AutoLinker.ParserTest do import AutoLinker.Parser - describe "url?/3" do + describe "url?/2" do test "valid scheme true" do valid_scheme_urls() |> Enum.each(fn url -> - assert url?(url, true) + assert url?(url, scheme: true, validate_tld: true) end) end test "invalid scheme true" do invalid_scheme_urls() |> Enum.each(fn url -> - refute url?(url, true) + refute url?(url, scheme: true, validate_tld: true) end) end test "valid scheme false" do valid_non_scheme_urls() |> Enum.each(fn url -> - assert url?(url, false) + assert url?(url, scheme: false, validate_tld: true) end) end test "invalid scheme false" do invalid_non_scheme_urls() |> Enum.each(fn url -> - refute url?(url, false) + refute url?(url, scheme: false, validate_tld: true) end) end test "checks the tld for url with a scheme when validate_tld: true" do custom_tld_scheme_urls() |> Enum.each(fn url -> - refute url?(url, true, true) + refute url?(url, scheme: true, validate_tld: true) end) end test "does not check the tld for url with a scheme when validate_tld: false" do custom_tld_scheme_urls() |> Enum.each(fn url -> - assert url?(url, true, false) + assert url?(url, scheme: true, validate_tld: false) end) end test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do custom_tld_scheme_urls() |> Enum.each(fn url -> - assert url?(url, true, :no_scheme) + assert url?(url, scheme: true, validate_tld: :no_scheme) end) end test "checks the tld for url without a scheme when validate_tld: true" do custom_tld_non_scheme_urls() |> Enum.each(fn url -> - refute url?(url, false, true) + refute url?(url, scheme: false, validate_tld: true) end) end test "checks the tld for url without a scheme when validate_tld: :no_scheme" do custom_tld_non_scheme_urls() |> Enum.each(fn url -> - refute url?(url, false, :no_scheme) + refute url?(url, scheme: false, validate_tld: :no_scheme) end) end test "does not check the tld for url without a scheme when validate_tld: false" do custom_tld_non_scheme_urls() |> Enum.each(fn url -> - assert url?(url, false, false) + assert url?(url, scheme: false, validate_tld: false) end) end end