|
| 1 | +open Mdx.Util.Result.Infix |
| 2 | +open Cmdliner |
| 3 | + |
| 4 | +let raw t = |
| 5 | + Notebook_t. |
| 6 | + { |
| 7 | + cell_type = `Raw; |
| 8 | + metadata = { collapsed = None; scrolled = None }; |
| 9 | + source = String.concat "\n" t; |
| 10 | + outputs = None; |
| 11 | + execution_count = None; |
| 12 | + } |
| 13 | + |
| 14 | +let txt source = |
| 15 | + Notebook_t. |
| 16 | + { |
| 17 | + cell_type = `Markdown; |
| 18 | + metadata = { collapsed = None; scrolled = None }; |
| 19 | + source; |
| 20 | + outputs = None; |
| 21 | + execution_count = None; |
| 22 | + } |
| 23 | + |
| 24 | +let execution_count = ref 1 |
| 25 | + |
| 26 | +let ocaml contents = |
| 27 | + let cell = |
| 28 | + Notebook_t. |
| 29 | + { |
| 30 | + cell_type = `Code; |
| 31 | + metadata = { collapsed = None; scrolled = None }; |
| 32 | + source = String.concat "\n" contents; |
| 33 | + outputs = Some []; |
| 34 | + execution_count = Some !execution_count; |
| 35 | + } |
| 36 | + in |
| 37 | + incr execution_count; |
| 38 | + cell |
| 39 | + |
| 40 | +let toplevel x = |
| 41 | + let cell = |
| 42 | + Notebook_t. |
| 43 | + { |
| 44 | + cell_type = `Code; |
| 45 | + metadata = { collapsed = None; scrolled = None }; |
| 46 | + source = String.concat "\n" x.Mdx.Toplevel.command; |
| 47 | + outputs = Some []; |
| 48 | + execution_count = Some !execution_count; |
| 49 | + } |
| 50 | + in |
| 51 | + incr execution_count; |
| 52 | + cell |
| 53 | + |
| 54 | +let metadata = |
| 55 | + Notebook_t. |
| 56 | + { |
| 57 | + kernelspec = |
| 58 | + { |
| 59 | + display_name = "OCaml 4.07.1"; |
| 60 | + language = "OCaml"; |
| 61 | + name = "ocaml-jupyter"; |
| 62 | + }; |
| 63 | + language_info = |
| 64 | + { |
| 65 | + name = "OCaml"; |
| 66 | + version = "4.07.1"; |
| 67 | + codemirror_mode = Some "text/x-ocaml"; |
| 68 | + file_extension = ".ml"; |
| 69 | + mimetype = "text/x-ocaml"; |
| 70 | + nbconverter_exporter = None; |
| 71 | + pygments_lexer = "OCaml"; |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | +let rec collapse_text = function |
| 76 | + | Mdx.Text x :: Mdx.Text y :: xs -> |
| 77 | + collapse_text (Mdx.Text (x ^ "\n" ^ y) :: xs) |
| 78 | + | (Mdx.Section _ as s) :: Mdx.Text y :: xs -> |
| 79 | + let s = Mdx.to_string [ s ] in |
| 80 | + collapse_text (Mdx.Text (s ^ "\n" ^ y) :: xs) |
| 81 | + | (Mdx.Section _ as s) :: xs -> |
| 82 | + let s = Mdx.to_string [ s ] in |
| 83 | + collapse_text (Mdx.Text s :: xs) |
| 84 | + | x :: ys -> x :: collapse_text ys |
| 85 | + | [] -> [] |
| 86 | + |
| 87 | +let run _setup (`Syntax syntax) (`File file) = |
| 88 | + Mdx.run_to_stdout ?syntax file ~f:(fun _file_contents items -> |
| 89 | + let syntax = |
| 90 | + match syntax with |
| 91 | + | Some s -> s |
| 92 | + | None -> ( |
| 93 | + match Mdx.Syntax.infer ~file with |
| 94 | + | Some s -> s |
| 95 | + | None -> failwith "Couldn't get syntax") |
| 96 | + in |
| 97 | + let cells = |
| 98 | + List.fold_left |
| 99 | + (fun acc -> function |
| 100 | + | Mdx.Text "" -> acc |
| 101 | + | Mdx.Text x -> txt x :: acc |
| 102 | + | Mdx.Block { value = OCaml { env = User_defined _; _ }; _ } |
| 103 | + | Mdx.Block { value = Toplevel { env = User_defined _; _ }; _ } -> |
| 104 | + failwith |
| 105 | + "internal error, cannot handle user defined environments" |
| 106 | + | Mdx.Block { value = OCaml _; contents; _ } -> |
| 107 | + ocaml contents :: acc |
| 108 | + | Mdx.Block { value = Toplevel _; contents; loc; _ } -> |
| 109 | + let blocks = Mdx.Toplevel.of_lines ~syntax ~loc contents in |
| 110 | + let newcells = List.rev_map toplevel blocks in |
| 111 | + newcells @ acc |
| 112 | + | Mdx.Block { value = Raw _; contents; _ } -> raw contents :: acc |
| 113 | + | x -> |
| 114 | + failwith |
| 115 | + (Printf.sprintf "internal error, cannot handle: %s" |
| 116 | + (Mdx.to_string [ x ]))) |
| 117 | + [] (collapse_text items) |
| 118 | + |> List.rev |
| 119 | + in |
| 120 | + Notebook_j.string_of_notebook |
| 121 | + Notebook_t.{ metadata; nbformat = 4; nbformat_minor = 2; cells }) |
| 122 | + >>! fun () -> 0 |
| 123 | + |
| 124 | +let cmd : int Term.t * Term.info = |
| 125 | + let doc = "Convert an mdx file to a jupyter notebook." in |
| 126 | + (Term.(pure run $ Cli.setup $ Cli.syntax $ Cli.file), Term.info "jupyter" ~doc) |
0 commit comments