From 37c5c3bdd1842308f97d21cb93785143d590dcd6 Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 3 Apr 2019 08:00:52 +0000 Subject: [PATCH 1/2] Improve CI --- .gitlab-ci.yml | 14 +++++++++++--- lib/auto_linker/parser.ex | 2 ++ test/auto_linker_test.exs | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index afce633..91c100b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,11 +3,13 @@ image: elixir:1.7.2 cache: key: ${CI_COMMIT_REF_SLUG} paths: - - deps - - _build + - deps + - _build + stages: - lint - test + - analysis before_script: - mix local.hex --force @@ -22,5 +24,11 @@ lint: unit-testing: stage: test + coverage: '/(\d+\.\d+\%) \| Total/' + script: + - mix test --trace --cover + +analysis: + stage: analysis script: - - mix test --trace + - mix credo --strict diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index c37f56c..3395b9c 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -39,9 +39,11 @@ defmodule AutoLinker.Parser do # @user # @user@example.com + # credo:disable-for-next-line @match_mention ~r/^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+/u # https://www.w3.org/TR/html5/forms.html#valid-e-mail-address + # credo:disable-for-next-line @match_email ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/u @match_hashtag ~r/^(?\#[[:word:]_]*[[:alpha:]_·][[:word:]_·]*)/u diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index 9bd0719..6fce60e 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -24,8 +24,8 @@ defmodule AutoLinkerTest do test "phone number and markdown link" do assert AutoLinker.link("888 888-8888 [ab](a.com)", phone: true, markdown: true) == - "888 888-8888" <> - " ab" + ~s(888 888-8888) <> + ~s( ab) end test "all kinds of links" do From 12f8ca53191074f2f340adcab3c3ab678265e47d Mon Sep 17 00:00:00 2001 From: Egor Date: Wed, 3 Apr 2019 12:02:31 +0000 Subject: [PATCH 2/2] Improve detection of URLs and Asian scripts --- lib/auto_linker/parser.ex | 23 ++++++++++++++++------- test/auto_linker_test.exs | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 3395b9c..149f00d 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -29,24 +29,22 @@ defmodule AutoLinker.Parser do @match_url ~r{^[\w\.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - @match_scheme ~r{^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$} + @match_scheme ~r{^(?:\W*)?(?(?:\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{^(?:https?:\/\/)?(?:[^@\n]+\\w@)?(?[^:#~\/\n?]+)} + @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])$" # @user # @user@example.com - # credo:disable-for-next-line - @match_mention ~r/^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+/u + @match_mention ~r"^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+"u # https://www.w3.org/TR/html5/forms.html#valid-e-mail-address - # credo:disable-for-next-line - @match_email ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/u + @match_email ~r"^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"u - @match_hashtag ~r/^(?\#[[:word:]_]*[[:alpha:]_·][[:word:]_·]*)/u + @match_hashtag ~r/^(?\#[[:word:]_]*[[:alpha:]_·][[:word:]_·\p{M}]*)/u @prefix_extra [ "magnet:?", @@ -261,6 +259,17 @@ defmodule AutoLinker.Parser do defp do_parse({<> <> text, user_acc}, opts, {buffer, acc, state}, handler), do: do_parse({text, user_acc}, opts, {buffer <> <>, acc, state}, handler) + def check_and_link(buffer, %{scheme: true} = opts, _user_acc) do + if is_url?(buffer, opts[:scheme]) do + case Regex.run(@match_scheme, buffer, capture: [:url]) do + [^buffer] -> link_url(true, buffer, opts) + [url] -> String.replace(buffer, url, link_url(true, url, opts)) + end + else + buffer + end + end + def check_and_link(buffer, opts, _user_acc) do buffer |> is_url?(opts[:scheme]) diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index 6fce60e..e6706fa 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -195,6 +195,21 @@ defmodule AutoLinkerTest do ) == expected end + test "support Telugu" do + text = "#చక్రం #కకకకక్ #కకకకాక #కకకక్రకకకక" + + expected = + "#చక్రం #కకకకక్ #కకకకాక #కకకక్రకకకక" + + assert AutoLinker.link(text, + hashtag: true, + hashtag_prefix: "/t/", + class: false, + rel: false, + new_window: false + ) == expected + end + test "do not turn urls with hashes into hashtags" do text = "google.com#test #test google.com/#test #tag" @@ -242,6 +257,14 @@ defmodule AutoLinkerTest do assert AutoLinker.link(text, scheme: true) == expected end + test "turn urls with schema into urls" do + text = "📌https://google.com" + expected = "📌google.com" + + assert AutoLinker.link(text, scheme: true, class: false, new_window: false, rel: false) == + expected + end + test "hostname/@user" do text = "https://example.com/@user"