Move from Helpers to Utils and add doctests

This commit is contained in:
José Valim 2022-01-29 10:39:26 +01:00
parent 420284006a
commit 372f086044
4 changed files with 30 additions and 22 deletions

View file

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

View file

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

View file

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

View file

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