Skip to content

Commit

Permalink
handle sections and raw blocks as well for jupyter
Browse files Browse the repository at this point in the history
fail on unknown markdown, to make debugging easier
  • Loading branch information
avsm committed Sep 13, 2019
1 parent 1cd5dd7 commit 3155e5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
23 changes: 22 additions & 1 deletion bin/jupyter.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
open Cmdliner

let raw t =
let cell = Notebook_t.{
cell_type = `Raw;
metadata = {
collapsed = None;
scrolled = None;
};
source = String.concat "\n" t;
outputs = None;
execution_count = None;
} in
cell

let txt t =
let cell = Notebook_t.{
cell_type = `Markdown;
Expand Down Expand Up @@ -52,6 +65,12 @@ let run _setup syntax file =
let rec collapse_text = function
| Mdx.Text x :: Mdx.Text y :: xs ->
collapse_text (Mdx.Text (x ^ "\n" ^ y) :: xs)
| Mdx.Section _ as s :: Mdx.Text y :: xs ->
let s = Mdx.to_string [s] in
collapse_text (Mdx.Text (s ^ "\n" ^ y) :: xs)
| Mdx.Section _ as s :: xs ->
let s = Mdx.to_string [s] in
collapse_text (Mdx.Text s :: xs)
| x::ys -> x :: collapse_text ys
| [] -> []
in
Expand All @@ -64,7 +83,9 @@ let run _setup syntax file =
| Mdx.Block {value=Toplevel xs; _} ->
let newcells = List.rev_map toplevel xs in
cells := newcells @ !cells
| _ -> ()
| Mdx.Block {value=Raw; contents; _} ->
cells := (raw contents) :: !cells
| x -> failwith (Printf.sprintf "internal error, cannot handle: %s" (Mdx.to_string [x]))
) (collapse_text items);
"OK"
);
Expand Down
3 changes: 2 additions & 1 deletion bin/notebook.atd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type cell_metadata = {
type cell_type = [
| Code <json name="code">
| Markdown <json name="markdown">
| Raw <json name="raw">
]

type output_type = [
Expand All @@ -53,4 +54,4 @@ type notebook = {
nbformat: int;
nbformat_minor: int;
cells: cell list
}
}

0 comments on commit 3155e5c

Please sign in to comment.