From 372f086044808f11f1d1a7a7989fb41db07ee5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 29 Jan 2022 10:39:26 +0100 Subject: [PATCH] Move from Helpers to Utils and add doctests --- lib/livebook/utils.ex | 27 +++++++++++++++++++++++++++ lib/livebook_app.ex | 3 ++- lib/livebook_cli/server.ex | 2 +- lib/livebook_web/helpers.ex | 20 -------------------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/livebook/utils.ex b/lib/livebook/utils.ex index c3f9f3922..1f3470df1 100644 --- a/lib/livebook/utils.ex +++ b/lib/livebook/utils.ex @@ -369,6 +369,33 @@ defmodule Livebook.Utils do |> Enum.join("\n") end + @doc """ + Returns a URL (including localhost) to import the given `url` as a notebook. + + iex> Livebook.Utils.notebook_import_url("https://example.com/foo.livemd") + "http://localhost:4002/import?url=https%3A%2F%2Fexample.com%2Ffoo.livemd" + + iex> Livebook.Utils.notebook_import_url("https://my_host", "https://example.com/foo.livemd") + "https://my_host/import?url=https%3A%2F%2Fexample.com%2Ffoo.livemd" + + """ + def notebook_import_url(base_url \\ LivebookWeb.Endpoint.access_struct_url(), url) do + base_url + |> URI.parse() + |> Map.replace!(:path, "/import") + |> append_query("url=#{URI.encode_www_form(url)}") + |> URI.to_string() + end + + # TODO: On Elixir v1.14, use URI.append_query/2 + defp append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do + %{uri | query: query_to_add} + end + + defp append_query(%URI{} = uri, query) do + %{uri | query: uri.query <> "&" <> query} + end + @doc """ Formats the given number of bytes into a human-friendly text. diff --git a/lib/livebook_app.ex b/lib/livebook_app.ex index e47a597ef..984ff3ec7 100644 --- a/lib/livebook_app.ex +++ b/lib/livebook_app.ex @@ -84,7 +84,8 @@ if Mix.target() == :app do end defp import_livebook(url) do - LivebookWeb.Helpers.notebook_import_url(url) + url + |> Livebook.Utils.notebook_import_url() |> Livebook.Utils.browser_open() end diff --git a/lib/livebook_cli/server.ex b/lib/livebook_cli/server.ex index 84711d632..56da6a2c9 100644 --- a/lib/livebook_cli/server.ex +++ b/lib/livebook_cli/server.ex @@ -135,7 +135,7 @@ defmodule LivebookCLI.Server do defp open_from_options(base_url, _opts, [url]) do base_url - |> LivebookWeb.Helpers.notebook_import_url(url) + |> Livebook.Utils.notebook_import_url(url) |> Livebook.Utils.browser_open() end diff --git a/lib/livebook_web/helpers.ex b/lib/livebook_web/helpers.ex index ea6c8770f..f6ada6776 100644 --- a/lib/livebook_web/helpers.ex +++ b/lib/livebook_web/helpers.ex @@ -376,26 +376,6 @@ defmodule LivebookWeb.Helpers do def file_system_label(%FileSystem.Local{}), do: "Local disk" def file_system_label(%FileSystem.S3{} = fs), do: fs.bucket_url - @doc """ - Returns a URL (including localhost) to import the given `url` as a notebook. - """ - def notebook_import_url(base_url \\ LivebookWeb.Endpoint.access_struct_url(), url) do - base_url - |> URI.parse() - |> Map.replace!(:path, "/import") - |> append_query("url=#{URI.encode_www_form(url)}") - |> URI.to_string() - end - - # TODO: On Elixir v1.14, use URI.append_query/2 - defp append_query(%URI{query: query} = uri, query_to_add) when query in [nil, ""] do - %{uri | query: query_to_add} - end - - defp append_query(%URI{} = uri, query) do - %{uri | query: uri.query <> "&" <> query} - end - @doc """ Returns the text in singular or plural depending on the quantity