mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-08 20:46:16 +08:00
Move from Helpers to Utils and add doctests
This commit is contained in:
parent
420284006a
commit
372f086044
4 changed files with 30 additions and 22 deletions
|
@ -369,6 +369,33 @@ defmodule Livebook.Utils do
|
||||||
|> Enum.join("\n")
|
|> Enum.join("\n")
|
||||||
end
|
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 """
|
@doc """
|
||||||
Formats the given number of bytes into a human-friendly text.
|
Formats the given number of bytes into a human-friendly text.
|
||||||
|
|
||||||
|
|
|
@ -84,7 +84,8 @@ if Mix.target() == :app do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp import_livebook(url) do
|
defp import_livebook(url) do
|
||||||
LivebookWeb.Helpers.notebook_import_url(url)
|
url
|
||||||
|
|> Livebook.Utils.notebook_import_url()
|
||||||
|> Livebook.Utils.browser_open()
|
|> Livebook.Utils.browser_open()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,7 @@ defmodule LivebookCLI.Server do
|
||||||
|
|
||||||
defp open_from_options(base_url, _opts, [url]) do
|
defp open_from_options(base_url, _opts, [url]) do
|
||||||
base_url
|
base_url
|
||||||
|> LivebookWeb.Helpers.notebook_import_url(url)
|
|> Livebook.Utils.notebook_import_url(url)
|
||||||
|> Livebook.Utils.browser_open()
|
|> Livebook.Utils.browser_open()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -376,26 +376,6 @@ defmodule LivebookWeb.Helpers do
|
||||||
def file_system_label(%FileSystem.Local{}), do: "Local disk"
|
def file_system_label(%FileSystem.Local{}), do: "Local disk"
|
||||||
def file_system_label(%FileSystem.S3{} = fs), do: fs.bucket_url
|
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 """
|
@doc """
|
||||||
Returns the text in singular or plural depending on the quantity
|
Returns the text in singular or plural depending on the quantity
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue