|
|
@ -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 |
|
|
|
|
|
|
|