|
|
@ -7,6 +7,9 @@ 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*)?(?<url>(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u |
|
|
|
|
|
|
|
@match_hostname ~r{^\W*(?<scheme>https?:\/\/)?(?:[^@\n]+\\w@)?(?<host>[^:#~\/\n?]+)}u |
|
|
@ -54,7 +57,7 @@ defmodule Linkify.Parser do |
|
|
|
~s{Check out <a href="http://google.com">google.com</a>} |
|
|
|
""" |
|
|
|
|
|
|
|
@types [:url, :email, :hashtag, :mention, :extra] |
|
|
|
@types [:youtube, :url, :email, :hashtag, :mention, :extra] |
|
|
|
|
|
|
|
def parse(input, opts \\ %{}) |
|
|
|
def parse(input, opts) when is_binary(input), do: {input, %{}} |> parse(opts) |> elem(0) |
|
|
@ -65,9 +68,11 @@ defmodule Linkify.Parser do |
|
|
|
|
|
|
|
Enum.reduce(opts, input, fn |
|
|
|
{type, true}, input when type in @types -> |
|
|
|
IO.puts inspect type |
|
|
|
do_parse(input, opts, {"", "", :parsing}, type) |
|
|
|
|
|
|
|
_, input -> |
|
|
|
IO.puts inspect input |
|
|
|
input |
|
|
|
end) |
|
|
|
end |
|
|
@ -171,6 +176,19 @@ 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 |
|
|
|
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 |
|
|
|
end |
|
|
|
|
|
|
|
def check_and_link(:url, buffer, opts, _user_acc) do |
|
|
|
str = strip_parens(buffer) |
|
|
|
|
|
|
|