mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-11-12 23:23:52 +08:00
Add public function for converting .livemd to .exs (#573)
This commit is contained in:
parent
a57927ec2a
commit
25d90eabf1
2 changed files with 54 additions and 0 deletions
27
lib/livebook.ex
Normal file
27
lib/livebook.ex
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
defmodule Livebook do
|
||||||
|
@moduledoc """
|
||||||
|
Livebook is an interactive notebook system for Elixir.
|
||||||
|
|
||||||
|
This module includes the public API.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@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
|
||||||
|
{notebook, _messages} = Livebook.LiveMarkdown.Import.notebook_from_markdown(markdown)
|
||||||
|
Livebook.Notebook.Export.Elixir.notebook_to_elixir(notebook)
|
||||||
|
end
|
||||||
|
end
|
||||||
27
test/livebook_test.exs
Normal file
27
test/livebook_test.exs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
defmodule LivebookTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
test "live_markdown_to_elixir/1" do
|
||||||
|
markdown = """
|
||||||
|
# Lists
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Let's generate a list of numbers:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
Enum.to_list(1..10)
|
||||||
|
```
|
||||||
|
"""
|
||||||
|
|
||||||
|
assert Livebook.live_markdown_to_elixir(markdown) == """
|
||||||
|
# Title: Lists
|
||||||
|
|
||||||
|
# ── Introduction ──
|
||||||
|
|
||||||
|
# Let's generate a list of numbers:
|
||||||
|
|
||||||
|
Enum.to_list(1..10)
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Reference in a new issue