Browse Source

youtubegit add .!

master
Gustavo Adolfo Mesa Roldán 4 years ago
parent
commit
badd31d664
2 changed files with 14 additions and 17 deletions
  1. +3
    -3
      lib/linkify/builder.ex
  2. +11
    -14
      lib/linkify/parser.ex

+ 3
- 3
lib/linkify/builder.ex View File

@ -123,14 +123,14 @@ defmodule Linkify.Builder do
|> format_extra(uri, opts)
end
def create_youtube(text, opts) do
def create_youtube(text, _opts) do
id = get_video_id(text)
"<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/#{id}\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>"
end
Enum.each(["http", "https"], fn protocol ->
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
def get_video_id(unquote(protocol <> prefix) <> id), do: id
end)
end)


+ 11
- 14
lib/linkify/parser.ex View File

@ -7,7 +7,7 @@ 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_youtube ~r{(http(?:s?):\/\/)?(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?}u
@match_url ~r{^(?:\W*)?(?<url>(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u
@ -56,7 +56,7 @@ defmodule Linkify.Parser do
~s{Check out <a href="http://google.com">google.com</a>}
"""
@types [:url, :email, :hashtag, :mention, :extra, :youtube]
@types [:url, :email, :hashtag, :mention, :extra]
def parse(input, opts \\ %{})
def parse(input, opts) when is_binary(input), do: {input, %{}} |> parse(opts) |> elem(0)
@ -68,7 +68,7 @@ defmodule Linkify.Parser do
Enum.reduce(opts, input, fn
{type, true}, input when type in @types ->
do_parse(input, opts, {"", "", :parsing}, type)
a, input ->
_, input ->
input
end)
end
@ -172,19 +172,16 @@ defmodule Linkify.Parser do
defp do_parse({<<ch::8>> <> text, user_acc}, opts, {buffer, acc, state}, type),
do: do_parse({text, user_acc}, opts, {buffer <> <<ch::8>>, acc, state}, type)
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)
if url?(str, opts) do
case @match_url |> Regex.run(str, capture: [:url]) |> hd() do
^buffer -> link_url(buffer, opts)
url -> String.replace(buffer, url, link_url(url, opts))
end
else
buffer
cond do
youtube?(str, opts) == true -> link_youtube(buffer, opts)
url?(str, opts) == true ->
case @match_url |> Regex.run(str, capture: [:url]) |> hd() do
^buffer -> link_url(buffer, opts)
url -> String.replace(buffer, url, link_url(url, opts))
end
true -> buffer
end
end


Loading…
Cancel
Save