diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..68ec917 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '2' + +services: + auto_linker: + image: elixir + restart: always + hostname: auto_linker + container_name: auto_linker + entrypoint: + - /bin/sleep + - infinity + volumes: + - ./:/auto_linker + networks: + mynet: + ipv4_address: 172.144.0.101 + +networks: + mynet: + driver: bridge + ipam: + config: + - subnet: 172.144.0.0/24 diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex index b86e2c4..4de8fb6 100644 --- a/lib/linkify/parser.ex +++ b/lib/linkify/parser.ex @@ -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*)?(?(?:https?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:\/?#[\]@!\$&'\(\)\*\+,;=.]+$)}u @match_hostname ~r{^\W*(?https?:\/\/)?(?:[^@\n]+\\w@)?(?[^:#~\/\n?]+)}u @@ -54,7 +57,7 @@ defmodule Linkify.Parser do ~s{Check out google.com} """ - @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({<> <> 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 + end + def check_and_link(:url, buffer, opts, _user_acc) do str = strip_parens(buffer)