diff --git a/lib/auto_linker.ex b/lib/auto_linker.ex index 8bc43be..222cd79 100644 --- a/lib/auto_linker.ex +++ b/lib/auto_linker.ex @@ -3,24 +3,24 @@ defmodule AutoLinker do Create url links from text containing urls. Turns an input string like `"Check out google.com"` into - `Check out "google.com"` + `Check out "google.com"` ## Examples iex> AutoLinker.link("google.com") - "google.com" + ~s(google.com) iex> AutoLinker.link("google.com", new_window: false, rel: false) - "google.com" + ~s(google.com) iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false) - "google.com" + ~s(google.com) iex> AutoLinker.link("[Google](http://google.com)", markdown: true, new_window: false, rel: false, class: false) - "Google" + ~s(Google) iex> AutoLinker.link("[Google Search](http://google.com)", markdown: true) - "Google Search" + ~s(Google Search) """ import AutoLinker.Parser diff --git a/lib/auto_linker/builder.ex b/lib/auto_linker/builder.ex index 22d1758..185357f 100644 --- a/lib/auto_linker/builder.ex +++ b/lib/auto_linker/builder.ex @@ -53,7 +53,7 @@ defmodule AutoLinker.Builder do defp format_attrs(attrs) do attrs - |> Enum.map(fn {key, value} -> ~s(#{key}='#{value}') end) + |> Enum.map(fn {key, value} -> ~s(#{key}="#{value}") end) |> Enum.join(" ") end @@ -162,12 +162,12 @@ defmodule AutoLinker.Builder do def format_email(attrs, email, _opts) do attrs = format_attrs(attrs) - "#{email}" + ~s(#{email}) end def format_extra(attrs, uri, _opts) do - attrs = format_attrs(attrs) - "#{uri}" + attrs = format_attributes(attrs) + ~s(#{uri}) end defp format_attributes(attrs) do diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index be32b59..b00fc67 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -13,7 +13,7 @@ defmodule AutoLinker.Parser do ## Examples iex> AutoLinker.Parser.parse("Check out google.com") - "Check out google.com" + ~s{Check out google.com} iex> AutoLinker.Parser.parse("call me at x9999", phone: true) ~s{call me at x9999} diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index 968c847..c906206 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -9,12 +9,12 @@ defmodule AutoLinkerTest do test "default link" do assert AutoLinker.link("google.com") == - "google.com" + "google.com" end test "markdown" do assert AutoLinker.link("[google.com](http://google.com)", markdown: true) == - "google.com" + "google.com" end test "does on link existing links" do @@ -25,7 +25,7 @@ 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" + " ab" end test "all kinds of links" do @@ -33,7 +33,7 @@ defmodule AutoLinkerTest do "hello @user google.com https://ddg.com 888 888-8888 #tag user@email.com [google.com](http://google.com) irc:///mIRC" expected = - "hello @user google.com ddg.com 888 888-8888 #tag user@email.com google.com irc:///mIRC" + "hello @user google.com ddg.com 888 888-8888 #tag user@email.com google.com irc:///mIRC" assert AutoLinker.link(text, phone: true, @@ -72,7 +72,7 @@ defmodule AutoLinkerTest do ) assert result_text == - "hello @user @valid_user and @invalid_user" + "hello @user, @valid_user and @invalid_user" assert mentions |> MapSet.to_list() |> Enum.map(&elem(&1, 1)) == valid_users end @@ -96,7 +96,7 @@ defmodule AutoLinkerTest do ) assert result_text == - "#hello #world" + "#hello #world" assert MapSet.to_list(tags) == ["hello", "world"] end @@ -105,9 +105,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 AutoLinker.link("hello @user and @anotherUser.", mention: true, mention_prefix: "https://example.com/user/" ) == expected @@ -117,7 +117,7 @@ defmodule AutoLinkerTest do text = "hey @user@example.com" expected = - "hey @user@example.com" + "hey @user@example.com" assert AutoLinker.link(text, mention: true, @@ -141,9 +141,9 @@ defmodule AutoLinkerTest do describe "hashtag links" do test "hashtag" do expected = - "one #two three #four" + " one #2two three #four." - assert AutoLinker.link("one #two three #four", + assert AutoLinker.link(" one #2two three #four.", hashtag: true, hashtag_prefix: "https://example.com/tag/" ) == expected @@ -153,7 +153,7 @@ defmodule AutoLinkerTest do text = "google.com#test #test google.com/#test #tag" expected = - "google.com#test #test google.com/#test #tag" + "google.com#test #test google.com/#test #tag" assert AutoLinker.link(text, scheme: true, @@ -169,7 +169,7 @@ defmodule AutoLinkerTest do text = "#漢字 #は #тест #ทดสอบ" expected = - "#漢字 #は #тест #ทดสอบ" + "#漢字 #は #тест #ทดสอบ" assert AutoLinker.link(text, scheme: true, @@ -187,7 +187,7 @@ 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, scheme: true) == expected @@ -200,21 +200,21 @@ defmodule AutoLinkerTest do text = "https://example.com/@user" expected = - "example.com/@user" + "example.com/@user" assert AutoLinker.link(text, scheme: true) == expected text = "https://example.com:4000/@user" expected = - "example.com:4000/@user" + "example.com:4000/@user" assert AutoLinker.link(text, scheme: true) == expected text = "https://example.com:4000/@user" expected = - "example.com:4000/@user" + "example.com:4000/@user" assert AutoLinker.link(text, scheme: true) == expected @@ -225,28 +225,28 @@ defmodule AutoLinkerTest do text = "http://www.cs.vu.nl/~ast/intel/" expected = - "cs.vu.nl/~ast/intel/" + "cs.vu.nl/~ast/intel/" assert AutoLinker.link(text, scheme: true) == 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, scheme: true) == 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, scheme: true) == 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, scheme: true) == expected end @@ -255,13 +255,16 @@ defmodule AutoLinkerTest do describe "non http links" 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) == expected end test "email" do text = "user@example.com" - expected = "user@example.com" + expected = "user@example.com" assert AutoLinker.link(text, email: true) == expected end @@ -270,7 +273,7 @@ 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) == expected end @@ -280,7 +283,7 @@ 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) == expected end @@ -291,7 +294,7 @@ defmodule AutoLinkerTest do text = "https://google.com" expected = - "google.com" + "google.com" assert AutoLinker.link(text, scheme: true) == expected end @@ -305,7 +308,7 @@ defmodule AutoLinkerTest do 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, scheme: true) == expected end @@ -318,7 +321,7 @@ defmodule AutoLinkerTest do 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, scheme: false) == expected end @@ -327,14 +330,14 @@ defmodule AutoLinkerTest 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, scheme: true) == 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, scheme: true) == expected end diff --git a/test/parser_test.exs b/test/parser_test.exs index f15c600..998b8f4 100644 --- a/test/parser_test.exs +++ b/test/parser_test.exs @@ -62,7 +62,10 @@ defmodule AutoLinker.ParserTest do test "links url inside html" do text = "Check out
google.com
" - expected = "Check out
google.com
" + + expected = + "Check out
google.com
" + assert parse(text, class: false, rel: false, new_window: false) == expected end