diff --git a/README.md b/README.md index 142e9d8..6d1def4 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,6 @@ -# AutoLinker +# Linkify -[![Build Status](https://travis-ci.org/smpallen99/auto_linker.png?branch=master)](https://travis-ci.org/smpallen99/auto_linker) [![Hex Version][hex-img]][hex] [![License][license-img]][license] - -[hex-img]: https://img.shields.io/hexpm/v/auto_linker.svg -[hex]: https://hex.pm/packages/auto_linker -[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg -[license]: http://opensource.org/licenses/MIT - -AutoLinker is a basic package for turning website names, and phone numbers into links. +Linkify is a basic package for turning website names, and phone numbers into links. Use this package in your web view to convert web references into click-able links. @@ -15,11 +8,11 @@ This is a very early version. Some of the described options are not yet function ## Installation -The package can be installed by adding `auto_linker` to your list of dependencies in `mix.exs`: +The package can be installed by adding `linkify` to your list of dependencies in `mix.exs`: ```elixir def deps do - [{:auto_linker, "~> 0.2"}] + [{:linkify, "~> 0.1"}] end ``` @@ -28,26 +21,26 @@ end The following examples illustrate some examples on how to use the auto linker. ```iex -iex> AutoLinker.link("google.com") -"google.com" +iex> Linkify.link("google.com") +"google.com" -iex> AutoLinker.link("google.com", new_window: false, rel: false) -"google.com" +iex> Linkify.link("google.com", new_window: false, rel: false) +"google.com" -iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false) +iex> Linkify.link("google.com", new_window: false, rel: false, class: false) "google.com" -iex> AutoLinker.link("call me at x9999", phone: true) +iex> Linkify.link("call me at x9999", phone: true) "call me at x9999" -iex> AutoLinker.link("or at home on 555.555.5555", phone: true) +iex> Linkify.link("or at home on 555.555.5555", phone: true) "or at home on 555.555.5555" -iex> AutoLinker.link(", work (555) 555-5555", phone: true) +iex> Linkify.link(", work (555) 555-5555", phone: true) ", work (555) 555-5555" ``` -See the [Docs](https://hexdocs.pm/auto_linker/) for more examples +See the [Docs](https://hexdocs.pm/linkify/) for more examples ## Configuration @@ -55,7 +48,7 @@ By default, link parsing is enabled and phone parsing is disabled. ```elixir # enable phone parsing, and disable link parsing -config :auto_linker, opts: [phone: true, url: false] +config :linkify, opts: [phone: true, url: false] ``` diff --git a/lib/auto_linker.ex b/lib/linkify.ex similarity index 77% rename from lib/auto_linker.ex rename to lib/linkify.ex index 0568068..66b8b67 100644 --- a/lib/auto_linker.ex +++ b/lib/linkify.ex @@ -1,4 +1,4 @@ -defmodule AutoLinker do +defmodule Linkify do @moduledoc """ Create url links from text containing urls. @@ -7,24 +7,24 @@ defmodule AutoLinker do ## Examples - iex> AutoLinker.link("google.com") - ~s(google.com) + iex> Linkify.link("google.com") + ~s(google.com) - iex> AutoLinker.link("google.com", new_window: false, rel: false) - ~s(google.com) + iex> Linkify.link("google.com", new_window: false, rel: false) + ~s(google.com) - iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false) + iex> Linkify.link("google.com", new_window: false, rel: false, class: false) ~s(google.com) """ - import AutoLinker.Parser + import Linkify.Parser @doc """ Auto link a string. Options: - * `class: "auto-linker"` - specify the class to be added to the generated link. false to clear + * `class: "linkified"` - specify the class to be added to the generated link. false to clear * `rel: "noopener noreferrer"` - override the rel attribute. false to clear * `new_window: true` - set to false to remove `target='_blank'` attribute * `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..` @@ -42,9 +42,9 @@ defmodule AutoLinker do * `validate_tld: true` - Set to false to disable TLD validation for urls/emails, also can be set to :no_scheme to validate TLDs only for urls without a scheme (e.g `example.com` will be validated, but `http://example.loki` won't) Each of the above options can be specified when calling `link(text, opts)` - or can be set in the `:auto_linker`'s configuration. For example: + or can be set in the `:linkify`'s configuration. For example: - config :auto_linker, + config :linkify, class: false, new_window: false diff --git a/lib/auto_linker/builder.ex b/lib/linkify/builder.ex similarity index 97% rename from lib/auto_linker/builder.ex rename to lib/linkify/builder.ex index 560a3b2..bab5a25 100644 --- a/lib/auto_linker/builder.ex +++ b/lib/linkify/builder.ex @@ -1,4 +1,4 @@ -defmodule AutoLinker.Builder do +defmodule Linkify.Builder do @moduledoc """ Module for building the auto generated link. """ @@ -36,7 +36,7 @@ defmodule AutoLinker.Builder do end defp build_attrs(attrs, _, opts, :class) do - case Map.get(opts, :class, "auto-linker") do + case Map.get(opts, :class, "linkified") do cls when is_binary(cls) -> [{:class, cls} | attrs] _ -> attrs end diff --git a/lib/auto_linker/parser.ex b/lib/linkify/parser.ex similarity index 97% rename from lib/auto_linker/parser.ex rename to lib/linkify/parser.ex index 68dfa36..4af1f26 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/linkify/parser.ex @@ -1,9 +1,9 @@ -defmodule AutoLinker.Parser do +defmodule Linkify.Parser do @moduledoc """ Module to handle parsing the the input string. """ - alias AutoLinker.Builder + alias Linkify.Builder @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ @@ -50,8 +50,8 @@ defmodule AutoLinker.Parser do ## Examples - iex> AutoLinker.Parser.parse("Check out google.com") - ~s{Check out google.com} + iex> Linkify.Parser.parse("Check out google.com") + ~s{Check out google.com} """ @types [:url, :email, :hashtag, :mention, :extra] @@ -214,8 +214,6 @@ defmodule AutoLinker.Parser do defp strip_parens(buffer), do: buffer - # @doc false - def url?(buffer, opts) do valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts) end diff --git a/mix.exs b/mix.exs index d73175c..a815188 100644 --- a/mix.exs +++ b/mix.exs @@ -1,21 +1,21 @@ -defmodule AutoLinker.Mixfile do +defmodule Linkify.Mixfile do use Mix.Project - @version "0.2.2" + @version "0.1.0" def project do [ - app: :auto_linker, + app: :linkify, version: @version, - elixir: "~> 1.4", + elixir: "~> 1.7", build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, deps: deps(), docs: [extras: ["README.md"]], package: package(), - name: "AutoLinker", + name: "Linkify", description: """ - AutoLinker is a basic package for turning website names into links. + Linkify is a basic package for turning website names into links. """ ] end @@ -37,10 +37,9 @@ defmodule AutoLinker.Mixfile do defp package do [ - maintainers: ["Stephen Pallen"], licenses: ["MIT"], - links: %{"Github" => "https://github.com/smpallen99/auto_linker"}, - files: ~w(lib README.md mix.exs LICENSE) + links: %{"GitLab" => "https://git.pleroma.social/pleroma/linkify"}, + files: ~w(lib priv README.md mix.exs LICENSE) ] end end diff --git a/test/builder_test.exs b/test/builder_test.exs index 0b30065..ac3f0b1 100644 --- a/test/builder_test.exs +++ b/test/builder_test.exs @@ -1,31 +1,30 @@ -defmodule AutoLinker.BuilderTest do +defmodule Linkify.BuilderTest do use ExUnit.Case, async: true - doctest AutoLinker.Builder + doctest Linkify.Builder - import AutoLinker.Builder + import Linkify.Builder test "create_link/2" do expected = - "text" + "text" assert create_link("text", %{}) == expected - expected = "text" + expected = "text" assert create_link("text", %{rel: nil}) == expected - expected = - "text" + expected = "text" assert create_link("text", %{rel: "me"}) == expected - expected = "t..." + expected = "t..." assert create_link("text", %{truncate: 3, rel: false}) == expected - expected = "text" + expected = "text" assert create_link("text", %{truncate: 2, rel: false}) == expected - expected = "http://text" + expected = "http://text" assert create_link("http://text", %{rel: false, strip_prefix: false}) == expected end @@ -48,13 +47,13 @@ defmodule AutoLinker.BuilderTest do test "create_mention_link/3" do expected = - "@navi" + "@navi" assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected end test "create_email_link/3" do - expected = "user@example.org" + expected = "user@example.org" assert create_email_link("user@example.org", %{}) == expected assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected end diff --git a/test/auto_linker_test.exs b/test/linkify_test.exs similarity index 66% rename from test/auto_linker_test.exs rename to test/linkify_test.exs index 4941fe8..199a2b2 100644 --- a/test/auto_linker_test.exs +++ b/test/linkify_test.exs @@ -1,15 +1,15 @@ -defmodule AutoLinkerTest do +defmodule LinkifyTest do use ExUnit.Case, async: true - doctest AutoLinker + doctest Linkify test "default link" do - assert AutoLinker.link("google.com") == - "google.com" + assert Linkify.link("google.com") == + "google.com" end test "does on link existing links" do text = ~s(google.com) - assert AutoLinker.link(text) == text + assert Linkify.link(text) == text end test "all kinds of links" do @@ -18,7 +18,7 @@ defmodule AutoLinkerTest do expected = "hello google.com ddg.com user@email.com irc:///mIRC" - assert AutoLinker.link(text, + assert Linkify.link(text, email: true, extra: true, class: false, @@ -36,7 +36,7 @@ defmodule AutoLinkerTest do url |> String.split(".") |> List.last() end - assert AutoLinker.link(text, + assert Linkify.link(text, class: false, new_window: false, rel: custom_rel @@ -48,7 +48,7 @@ defmodule AutoLinkerTest do custom_rel = fn _ -> nil end - assert AutoLinker.link(text, + assert Linkify.link(text, class: false, new_window: false, rel: custom_rel @@ -56,8 +56,8 @@ defmodule AutoLinkerTest do end test "link_map/2" do - assert AutoLinker.link_map("google.com", []) == - {"google.com", + assert Linkify.link_map("google.com", []) == + {"google.com", []} end @@ -76,7 +76,7 @@ defmodule AutoLinkerTest do end {result_text, %{mentions: mentions}} = - AutoLinker.link_map(text, %{mentions: MapSet.new()}, + Linkify.link_map(text, %{mentions: MapSet.new()}, mention: true, mention_handler: handler ) @@ -91,12 +91,12 @@ defmodule AutoLinkerTest do text = "#hello #world" handler = fn hashtag, buffer, opts, acc -> - link = AutoLinker.Builder.create_hashtag_link(hashtag, buffer, opts) + link = Linkify.Builder.create_hashtag_link(hashtag, buffer, opts) {link, %{acc | tags: MapSet.put(acc.tags, hashtag)}} end {result_text, %{tags: tags}} = - AutoLinker.link_map(text, %{tags: MapSet.new()}, + Linkify.link_map(text, %{tags: MapSet.new()}, hashtag: true, hashtag_handler: handler, hashtag_prefix: "https://example.com/user/", @@ -120,9 +120,9 @@ defmodule AutoLinkerTest do end expected = - ~s(Hello again, @@user.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric) + ~s(Hello again, @@user.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric) - assert AutoLinker.link(text, + assert Linkify.link(text, mention: true, mention_handler: handler, hashtag: true, @@ -134,9 +134,9 @@ defmodule AutoLinkerTest do describe "mentions" do test "simple mentions" do expected = - ~s{hello @user and @anotherUser.} + ~s{hello @user and @anotherUser.} - assert AutoLinker.link("hello @user and @anotherUser.", + assert Linkify.link("hello @user and @anotherUser.", mention: true, mention_prefix: "https://example.com/user/" ) == expected @@ -149,7 +149,7 @@ defmodule AutoLinkerTest do expected = "

hello world

\n

<`em>another @user__test and @user__test google.com paragraph

\n" - assert AutoLinker.link(text, + assert Linkify.link(text, mention: true, mention_prefix: "u/", class: false, @@ -162,9 +162,9 @@ defmodule AutoLinkerTest do text = "hey @user@example.com" expected = - "hey @user@example.com" + "hey @user@example.com" - assert AutoLinker.link(text, + assert Linkify.link(text, mention: true, mention_prefix: "https://example.com/user/" ) == expected @@ -174,9 +174,9 @@ defmodule AutoLinkerTest do describe "hashtag links" do test "hashtag" do expected = - " one #2two three #four." + " one #2two three #four." - assert AutoLinker.link(" one #2two three #four.", + assert Linkify.link(" one #2two three #four.", hashtag: true, hashtag_prefix: "https://example.com/tag/" ) == expected @@ -185,7 +185,7 @@ defmodule AutoLinkerTest do test "must have non-numbers" do expected = "#1ok #42 #7" - assert AutoLinker.link("#1ok #42 #7", + assert Linkify.link("#1ok #42 #7", hashtag: true, hashtag_prefix: "/t/", class: false, @@ -200,7 +200,7 @@ defmodule AutoLinkerTest do expected = "#administrateur·rice·s #ingénieur·e·s" - assert AutoLinker.link(text, + assert Linkify.link(text, hashtag: true, hashtag_prefix: "/t/", class: false, @@ -215,7 +215,7 @@ defmodule AutoLinkerTest do expected = "#చక్రం #కకకకక్ #కకకకాక #కకకక్రకకకక" - assert AutoLinker.link(text, + assert Linkify.link(text, hashtag: true, hashtag_prefix: "/t/", class: false, @@ -230,7 +230,7 @@ defmodule AutoLinkerTest do expected = "google.com#test #test google.com/#test #tag" - assert AutoLinker.link(text, + assert Linkify.link(text, hashtag: true, class: false, new_window: false, @@ -245,7 +245,7 @@ defmodule AutoLinkerTest do expected = "#漢字 #は #тест #ทดสอบ" - assert AutoLinker.link(text, + assert Linkify.link(text, class: false, new_window: false, rel: false, @@ -260,75 +260,75 @@ defmodule AutoLinkerTest do text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." expected = - "Hey, check out youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." + "Hey, check out youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected # no scheme text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ." - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end test "turn urls with schema into urls" do text = "📌https://google.com" expected = "📌google.com" - assert AutoLinker.link(text, class: false, new_window: false, rel: false) == expected + assert Linkify.link(text, class: false, new_window: false, rel: false) == expected end test "hostname/@user" do text = "https://example.com/@user" expected = - "example.com/@user" + "example.com/@user" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "https://example.com:4000/@user" expected = - "example.com:4000/@user" + "example.com:4000/@user" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "https://example.com:4000/@user" expected = - "example.com:4000/@user" + "example.com:4000/@user" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "@username" expected = "@username" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "http://www.cs.vu.nl/~ast/intel/" expected = - "cs.vu.nl/~ast/intel/" + "cs.vu.nl/~ast/intel/" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087" expected = - "forum.zdoom.org/viewtopic.php?f=44&t=57087" + "forum.zdoom.org/viewtopic.php?f=44&t=57087" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" expected = - "en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" + "en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "https://en.wikipedia.org/wiki/Duff's_device" expected = - "en.wikipedia.org/wiki/Duff's_device" + "en.wikipedia.org/wiki/Duff's_device" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end end @@ -336,16 +336,15 @@ defmodule AutoLinkerTest do test "xmpp" do text = "xmpp:user@example.com" - expected = - "xmpp:user@example.com" + expected = "xmpp:user@example.com" - assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected + assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected end test "email" do text = "user@example.com" - expected = "user@example.com" - assert AutoLinker.link(text, email: true) == expected + expected = "user@example.com" + assert Linkify.link(text, email: true) == expected end test "magnet" do @@ -353,9 +352,9 @@ defmodule AutoLinkerTest do "magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce" expected = - "magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce" + "magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce" - assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected + assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected end test "dweb" do @@ -363,9 +362,9 @@ defmodule AutoLinkerTest do "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt" expected = - "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt" + "dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt" - assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected + assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected end end @@ -374,51 +373,51 @@ defmodule AutoLinkerTest do text = "https://google.com" expected = - "google.com" + "google.com" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end test "only existing TLDs with scheme" do text = "this url https://google.foobar.blah11blah/ has invalid TLD" expected = "this url https://google.foobar.blah11blah/ has invalid TLD" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "this url https://google.foobar.com/ has valid TLD" expected = - "this url google.foobar.com/ has valid TLD" + "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end test "only existing TLDs without scheme" do text = "this url google.foobar.blah11blah/ has invalid TLD" - assert AutoLinker.link(text) == text + assert Linkify.link(text) == text text = "this url google.foobar.com/ has valid TLD" expected = - "this url google.foobar.com/ has valid TLD" + "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end test "only existing TLDs with and without scheme" do text = "this url http://google.foobar.com/ has valid TLD" expected = - "this url google.foobar.com/ has valid TLD" + "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected text = "this url google.foobar.com/ has valid TLD" expected = - "this url google.foobar.com/ has valid TLD" + "this url google.foobar.com/ has valid TLD" - assert AutoLinker.link(text) == expected + assert Linkify.link(text) == expected end end end diff --git a/test/parser_test.exs b/test/parser_test.exs index ca0dfe9..f83531d 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -1,8 +1,8 @@ -defmodule AutoLinker.ParserTest do +defmodule Linkify.ParserTest do use ExUnit.Case, async: true - doctest AutoLinker.Parser + doctest Linkify.Parser - import AutoLinker.Parser + import Linkify.Parser describe "url?/2" do test "valid scheme true" do @@ -111,7 +111,7 @@ defmodule AutoLinker.ParserTest do text = "google.com\r\nssss" expected = - "google.com\r\nssss" + "google.com\r\nssss" assert parse(text) == expected end