From b30df6cb37b69166fe2af9edd913626fc9eb3515 Mon Sep 17 00:00:00 2001 From: "Stephen M. Pallen" Date: Thu, 6 Apr 2017 19:59:31 -0400 Subject: [PATCH] fix double dot issue --- lib/auto_linker/parser.ex | 19 +++++++++++++++---- mix.exs | 2 +- test/parser_test.exs | 3 ++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 7cef83f..88ecd7a 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -16,6 +16,11 @@ defmodule AutoLinker.Parser do "Check out google.com" """ + @match_dots ~r/\.\.+/ + + @match_url ~r{^[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} + @match_scheme ~r{^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} + def parse(text, opts \\ %{}) def parse(text, list) when is_list(list), do: parse(text, Enum.into(list, %{})) @@ -79,12 +84,18 @@ defmodule AutoLinker.Parser do @doc false def is_url?(buffer, true) do - re = ~r{^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - Regex.match? re, buffer + if Regex.match? @match_dots, buffer do + false + else + Regex.match? @match_scheme, buffer + end end def is_url?(buffer, _) do - re = ~r{^[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - Regex.match? re, buffer + if Regex.match? @match_dots, buffer do + false + else + Regex.match? @match_url, buffer + end end @doc false diff --git a/mix.exs b/mix.exs index 3a3bbeb..5954486 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule AutoLinker.Mixfile do use Mix.Project - @version "0.1.0" + @version "0.1.1" def project do [ diff --git a/test/parser_test.exs b/test/parser_test.exs index 3553ddb..5c43a10 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -86,7 +86,8 @@ defmodule AutoLinker.ParserTest do def invalid_non_scheme_urls, do: [ "invalid.com/perl.cgi?key= | web-site.com/cgi-bin/perl.cgi?key1=value1&key2", - "invalid." + "invalid.", + "hi..there" ] end