From 32a6e41cd06bb5d9cc3f0bc962a3b70e88420696 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 9 Apr 2019 15:00:39 +0700 Subject: [PATCH] cleanup --- lib/auto_linker/builder.ex | 2 +- lib/auto_linker/parser.ex | 47 +++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex index c2d3799..888fd82 100644 --- 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 index 89c1c62..f0dbe42 100644 --- 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 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} - """ - @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ @match_url ~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - @match_scheme ~r{^(?:\W*)?(?(?:\W*https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u + @match_scheme ~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}))" @@ -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 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 \\ %{}) 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({" text, user_acc}, opts, {buffer, acc, :parsing}, handler), do: do_parse({text, user_acc}, opts, {"", acc <> buffer <> "