commit: 32a6e41cd06bb5d9cc3f0bc962a3b70e88420696
parent 55636e859391bceec705b7152e25b65125d1879e
Author: Egor Kislitsyn <egor@kislitsyn.com>
Date: Tue, 9 Apr 2019 15:00:39 +0700
cleanup
Diffstat:
2 files changed, 22 insertions(+), 27 deletions(-)
diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex
@@ -82,7 +82,7 @@ defmodule AutoLinker.Builder do
defp truncate(url, len) when len < 3, do: url
defp truncate(url, len) do
- if String.length(url) > len, do: String.slice(url, 0, len - 2) <> "..", else: url
+ if String.length(url) > len, do: String.slice(url, 0, len - 2) <> "...", else: url
end
defp strip_prefix(url, true) do
diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex
@@ -5,31 +5,11 @@ defmodule AutoLinker.Parser do
alias AutoLinker.Builder
- @doc """
- Parse the given string, identifying items to link.
-
- Parses the string, replacing the matching urls and phone numbers with an html link.
-
- ## Examples
-
- 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>}
- """
-
@invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/
@match_url ~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$}
- @match_scheme ~r{^(?:\W*)?(?<url>(?:\W*https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u
+ @match_scheme ~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}))"
@@ -64,6 +44,26 @@ defmodule AutoLinker.Parser do
@default_opts ~w(url)a
+ @doc """
+ Parse the given string, identifying items to link.
+
+ Parses the string, replacing the matching urls and phone numbers with an html link.
+
+ ## Examples
+
+ 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 \\ %{})
def parse(input, opts) when is_binary(input), do: {input, nil} |> parse(opts) |> elem(0)
def parse(input, list) when is_list(list), do: parse(input, Enum.into(list, %{}))
@@ -154,11 +154,6 @@ defmodule AutoLinker.Parser do
defp do_parse({"", user_acc}, _opts, {"", acc, _}, _handler),
do: {acc, user_acc}
- defp do_parse({"", user_acc}, opts, {buffer, acc, _}, handler) do
- {buffer, user_acc} = run_handler(handler, buffer, opts, user_acc)
- {acc <> buffer, user_acc}
- end
-
defp do_parse({"<a" <> text, user_acc}, opts, {buffer, acc, :parsing}, handler),
do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "<a", :skip}, handler)