diff --git a/README.md b/README.md
index ab956a8..0a940d6 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,8 @@
# Linkify
+Linkify.link("https://www.youtube.com/watch?v=YNPvBU43oYg", %{youtube: true, url: false})
+
+
Linkify is a basic package for turning website names into links.
Use this package in your web view to convert web references into click-able links.
diff --git a/lib/linkify/builder.ex b/lib/linkify/builder.ex
index 385f6b3..4776d26 100644
--- a/lib/linkify/builder.ex
+++ b/lib/linkify/builder.ex
@@ -123,6 +123,17 @@ defmodule Linkify.Builder do
|> format_extra(uri, opts)
end
+ def create_youtube(text, opts) do
+ id = get_video_id(text)
+ ""
+ end
+
+ Enum.each(["http", "https"], fn protocol ->
+ Enum.each(["youtu.be/", "www.youtube.com/watch?v=", "www.youtube.com/?v="], fn prefix ->
+ def get_video_id(unquote(protocol <> "://" <> prefix) <> id), do: id
+ end)
+ end)
+
def format_mention(attrs, name, _opts) do
attrs = format_attrs(attrs)
"@#{name}"
diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex
index 4de8fb6..9f175bf 100644
--- a/lib/linkify/parser.ex
+++ b/lib/linkify/parser.ex
@@ -7,7 +7,6 @@ defmodule Linkify.Parser do
@invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/
-
@match_youtube ~r{http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?}u
@match_url ~r{^(?:\W*)?(?(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u
@@ -57,7 +56,7 @@ defmodule Linkify.Parser do
~s{Check out google.com}
"""
- @types [:youtube, :url, :email, :hashtag, :mention, :extra]
+ @types [:url, :email, :hashtag, :mention, :extra, :youtube]
def parse(input, opts \\ %{})
def parse(input, opts) when is_binary(input), do: {input, %{}} |> parse(opts) |> elem(0)
@@ -65,16 +64,13 @@ defmodule Linkify.Parser do
def parse(input, opts) do
opts = Map.merge(@default_opts, opts)
-
+
Enum.reduce(opts, input, fn
- {type, true}, input when type in @types ->
- IO.puts inspect type
+ {type, true}, input when type in @types ->
do_parse(input, opts, {"", "", :parsing}, type)
-
- _, input ->
- IO.puts inspect input
+ a, input ->
input
- end)
+ end)
end
defp do_parse({"", user_acc}, _opts, {"", acc, _}, _handler),
@@ -176,22 +172,12 @@ defmodule Linkify.Parser do
defp do_parse({<> <> text, user_acc}, opts, {buffer, acc, state}, type),
do: do_parse({text, user_acc}, opts, {buffer <> <>, acc, state}, type)
- def check_and_link(:youtube, buffer, opts, _user_acc) do
- str = strip_parens(buffer)
-
- if url?(str, opts) do
- case @match_youtube |> Regex.run(str, capture: [:youtube]) |> hd() do
- ^buffer -> link_url(buffer, opts)
- youtube -> String.replace(buffer, youtube, link_url(youtube, opts))
- end
- else
- buffer
- end
+ def check_and_link(:youtube, buffer, opts, _user_acc) do
+ if youtube?(buffer, opts), do: link_youtube(buffer, opts), else: buffer
end
def check_and_link(:url, buffer, opts, _user_acc) do
- str = strip_parens(buffer)
-
+ str = strip_parens(buffer)
if url?(str, opts) do
case @match_url |> Regex.run(str, capture: [:url]) |> hd() do
^buffer -> link_url(buffer, opts)
@@ -232,6 +218,10 @@ defmodule Linkify.Parser do
defp strip_parens(buffer), do: buffer
+ def youtube?(buffer, opts) do
+ valid_url?(buffer) && Regex.match?(@match_youtube, buffer) && valid_tld?(buffer, opts)
+ end
+
def url?(buffer, opts) do
valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts)
end
@@ -325,6 +315,11 @@ defmodule Linkify.Parser do
defp maybe_update_buffer(out, _match, _buffer), do: out
+ @doc false
+ def link_youtube(buffer, opts) do
+ Builder.create_youtube(buffer, opts)
+ end
+
@doc false
def link_url(buffer, opts) do
Builder.create_link(buffer, opts)
diff --git a/mix.lock b/mix.lock
index 32e36c5..745c353 100644
--- a/mix.lock
+++ b/mix.lock
@@ -1,10 +1,10 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
- "credo": {:hex, :credo, "1.1.0", "e0c07b2fd7e2109495f582430a1bc96b2c71b7d94c59dfad120529f65f19872f", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
- "earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm"},
- "ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
+ "credo": {:hex, :credo, "1.1.2", "02b6422f3e659eb74b05aca3c20c1d8da0119a05ee82577a82e6c2938bf29f81", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
+ "earmark": {:hex, :earmark, "1.3.3", "5e8be428fcef362692b6dbd7dc55bdc7023da26d995cb3fb19aa4bd682bfd3f9", [:mix], [], "hexpm"},
+ "ex_doc": {:hex, :ex_doc, "0.21.1", "5ac36660846967cd869255f4426467a11672fec3d8db602c429425ce5b613b90", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
- "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
- "makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
+ "makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
+ "makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
}