2021-10-05 06:26:41 +08:00
|
|
|
defmodule Livebook do
|
|
|
|
@moduledoc """
|
|
|
|
Livebook is an interactive notebook system for Elixir.
|
|
|
|
|
|
|
|
This module includes the public API.
|
|
|
|
"""
|
|
|
|
|
2021-10-05 06:49:01 +08:00
|
|
|
@doc """
|
|
|
|
Executes Livebook's config/runtime.exs.
|
|
|
|
|
|
|
|
If you use Livebook as a dependency, you can add the following
|
|
|
|
to your `config/runtime.exs` to trigger Livebook's config/runtime.exs
|
|
|
|
configuration:
|
|
|
|
|
|
|
|
Livebook.config_runtime()
|
|
|
|
|
|
|
|
"""
|
|
|
|
def config_runtime do
|
|
|
|
import Config
|
|
|
|
|
|
|
|
config :livebook, LivebookWeb.Endpoint,
|
|
|
|
secret_key_base:
|
|
|
|
Livebook.Config.secret!("LIVEBOOK_SECRET_KEY_BASE") ||
|
|
|
|
Base.encode64(:crypto.strong_rand_bytes(48))
|
|
|
|
|
|
|
|
if port = Livebook.Config.port!("LIVEBOOK_PORT") do
|
|
|
|
config :livebook, LivebookWeb.Endpoint, http: [port: port]
|
|
|
|
end
|
|
|
|
|
|
|
|
if ip = Livebook.Config.ip!("LIVEBOOK_IP") do
|
|
|
|
config :livebook, LivebookWeb.Endpoint, http: [ip: ip]
|
|
|
|
end
|
|
|
|
|
2021-11-10 20:54:50 +08:00
|
|
|
cond do
|
|
|
|
password = Livebook.Config.password!("LIVEBOOK_PASSWORD") ->
|
|
|
|
config :livebook, authentication_mode: :password, password: password
|
|
|
|
|
|
|
|
Livebook.Config.token_enabled!("LIVEBOOK_TOKEN_ENABLED") ->
|
|
|
|
config :livebook, token: Livebook.Utils.random_id()
|
|
|
|
|
|
|
|
true ->
|
|
|
|
config :livebook, authentication_mode: :disabled
|
2021-10-05 06:49:01 +08:00
|
|
|
end
|
|
|
|
|
2022-02-08 21:45:58 +08:00
|
|
|
if port = Livebook.Config.port!("LIVEBOOK_IFRAME_PORT") do
|
|
|
|
config :livebook, :iframe_port, port
|
|
|
|
end
|
|
|
|
|
2021-10-05 06:49:01 +08:00
|
|
|
if runtime = Livebook.Config.default_runtime!("LIVEBOOK_DEFAULT_RUNTIME") do
|
|
|
|
config :livebook, :default_runtime, runtime
|
|
|
|
end
|
|
|
|
|
2022-01-31 18:51:57 +08:00
|
|
|
if home = Livebook.Config.writable_dir!("LIVEBOOK_HOME") do
|
|
|
|
config :livebook, :home, home
|
|
|
|
end
|
|
|
|
|
|
|
|
if data_path = Livebook.Config.writable_dir!("LIVEBOOK_DATA_PATH") do
|
|
|
|
config :livebook, :data_path, data_path
|
|
|
|
end
|
|
|
|
|
2022-03-24 01:24:58 +08:00
|
|
|
if force_ssl_host = Livebook.Config.force_ssl_host!("LIVEBOOK_FORCE_SSL_HOST") do
|
|
|
|
config :livebook, :force_ssl_host, force_ssl_host
|
|
|
|
end
|
|
|
|
|
2021-10-05 06:49:01 +08:00
|
|
|
config :livebook,
|
|
|
|
:cookie,
|
|
|
|
Livebook.Config.cookie!("LIVEBOOK_COOKIE") ||
|
|
|
|
Livebook.Config.cookie!("RELEASE_COOKIE") ||
|
|
|
|
Livebook.Utils.random_cookie()
|
|
|
|
end
|
|
|
|
|
2021-10-05 06:26:41 +08:00
|
|
|
@doc """
|
|
|
|
Parses the given Live Markdown document and converts it to Elixir
|
|
|
|
source code.
|
|
|
|
|
|
|
|
## Limitations
|
|
|
|
|
|
|
|
Note that the resulting script may not compile in some cases, for
|
|
|
|
example if you define a macro in one cell and import it in another
|
|
|
|
cell, it works fine in Livebook, because each cell is compiled
|
|
|
|
separately. However, when running the script it gets compiled as a
|
|
|
|
whole and consequently doing so doesn't work.
|
|
|
|
|
|
|
|
Additionally, branching sections are commented out.
|
|
|
|
"""
|
|
|
|
@spec live_markdown_to_elixir(String.t()) :: String.t()
|
|
|
|
def live_markdown_to_elixir(markdown) do
|
2022-02-10 19:15:41 +08:00
|
|
|
{notebook, _messages} = Livebook.LiveMarkdown.notebook_from_livemd(markdown)
|
2021-10-05 06:26:41 +08:00
|
|
|
Livebook.Notebook.Export.Elixir.notebook_to_elixir(notebook)
|
|
|
|
end
|
|
|
|
end
|