Skip to content

Commit 1d95d20

Browse files
committed
jupyter: auto-format code
1 parent b7b48c4 commit 1d95d20

File tree

2 files changed

+116
-107
lines changed

2 files changed

+116
-107
lines changed

bin/dune

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
(libraries cli mdx))
1212

1313
(rule
14-
(targets notebook_j.ml
15-
notebook_j.mli)
16-
(deps notebook.atd)
17-
(action (run atdgen -j -j-std %{deps})))
14+
(targets notebook_j.ml notebook_j.mli)
15+
(deps notebook.atd)
16+
(action
17+
(run atdgen -j -j-std %{deps})))
1818

1919
(rule
20-
(targets notebook_t.ml
21-
notebook_t.mli)
22-
(deps notebook.atd)
23-
(action (run atdgen -t %{deps})))
20+
(targets notebook_t.ml notebook_t.mli)
21+
(deps notebook.atd)
22+
(action
23+
(run atdgen -t %{deps})))
2424

2525
(install
2626
(section bin)

bin/jupyter.ml

Lines changed: 108 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,127 +2,136 @@ open Mdx.Util.Result.Infix
22
open Cmdliner
33

44
let raw t =
5-
let cell = Notebook_t.{
6-
cell_type = `Raw;
7-
metadata = {
8-
collapsed = None;
9-
scrolled = None;
10-
};
11-
source = String.concat "\n" t;
12-
outputs = None;
13-
execution_count = None;
14-
} in
5+
let cell =
6+
Notebook_t.
7+
{
8+
cell_type = `Raw;
9+
metadata = { collapsed = None; scrolled = None };
10+
source = String.concat "\n" t;
11+
outputs = None;
12+
execution_count = None;
13+
}
14+
in
1515
cell
1616

1717
let txt t =
18-
let cell = Notebook_t.{
19-
cell_type = `Markdown;
20-
metadata = {
21-
collapsed = None;
22-
scrolled = None;
23-
};
24-
source = t;
25-
outputs = None;
26-
execution_count = None;
27-
} in
18+
let cell =
19+
Notebook_t.
20+
{
21+
cell_type = `Markdown;
22+
metadata = { collapsed = None; scrolled = None };
23+
source = t;
24+
outputs = None;
25+
execution_count = None;
26+
}
27+
in
2828
cell
2929

3030
let execution_count = ref 1
3131

3232
let ocaml contents =
33-
let cell = Notebook_t.{
34-
cell_type = `Code;
35-
metadata = {
36-
collapsed = None;
37-
scrolled = None;
38-
};
39-
source = String.concat "\n" contents;
40-
outputs = Some [];
41-
execution_count = (Some !execution_count);
42-
} in
33+
let cell =
34+
Notebook_t.
35+
{
36+
cell_type = `Code;
37+
metadata = { collapsed = None; scrolled = None };
38+
source = String.concat "\n" contents;
39+
outputs = Some [];
40+
execution_count = Some !execution_count;
41+
}
42+
in
4343
incr execution_count;
4444
cell
4545

46-
4746
let toplevel x =
48-
let cell = Notebook_t.{
49-
cell_type = `Code;
50-
metadata = {
51-
collapsed = None;
52-
scrolled = None;
53-
};
54-
source = String.concat "\n" x.Mdx.Toplevel.command;
55-
outputs = Some [];
56-
execution_count = (Some !execution_count);
47+
let cell =
48+
Notebook_t.
49+
{
50+
cell_type = `Code;
51+
metadata = { collapsed = None; scrolled = None };
52+
source = String.concat "\n" x.Mdx.Toplevel.command;
53+
outputs = Some [];
54+
execution_count = Some !execution_count;
5755
}
5856
in
5957
incr execution_count;
6058
cell
6159

62-
6360
let run _setup (`Syntax syntax) (`File file) =
6461
let cells = ref [] in
6562
Mdx.run ?syntax file ~f:(fun _file_contents items ->
66-
let syntax = match syntax with
63+
let syntax =
64+
match syntax with
6765
| Some s -> s
68-
| None -> (match Mdx.Syntax.infer ~file with
69-
| Some s -> s
70-
| None -> failwith "Couldn't get syntax") in
71-
let rec collapse_text = function
72-
| Mdx.Text x :: Mdx.Text y :: xs ->
73-
collapse_text (Mdx.Text (x ^ "\n" ^ y) :: xs)
74-
| Mdx.Section _ as s :: Mdx.Text y :: xs ->
75-
let s = Mdx.to_string [s] in
76-
collapse_text (Mdx.Text (s ^ "\n" ^ y) :: xs)
77-
| Mdx.Section _ as s :: xs ->
78-
let s = Mdx.to_string [s] in
79-
collapse_text (Mdx.Text s :: xs)
80-
| x::ys -> x :: collapse_text ys
81-
| [] -> []
82-
in
83-
List.iter (function
84-
| Mdx.Text "" -> ()
85-
| Mdx.Text x ->
86-
cells := (txt x) :: !cells
87-
| Mdx.Block {value=OCaml _; contents; _} ->
88-
cells := (ocaml contents) :: !cells
89-
| Mdx.Block {value=Toplevel _; contents; file; column; line; _ } ->
90-
let blocks = Mdx.Toplevel.of_lines ~syntax ~file ~column ~line contents in
91-
let newcells = List.rev_map toplevel blocks in
92-
cells := newcells @ !cells
93-
| Mdx.Block {value=Raw _; contents; _} ->
94-
cells := (raw contents) :: !cells
95-
| x -> failwith (Printf.sprintf "internal error, cannot handle: %s" (Mdx.to_string [x]))
96-
) (collapse_text items);
97-
"OK"
98-
) >>! fun () -> ();
99-
let notebook = Notebook_t.{
100-
metadata = {
101-
kernelspec = {
102-
display_name = "OCaml 4.07.1";
103-
language = "OCaml";
104-
name = "ocaml-jupyter";
105-
};
106-
language_info = {
107-
name = "OCaml";
108-
version = "4.07.1";
109-
codemirror_mode = Some "text/x-ocaml";
110-
file_extension = ".ml";
111-
mimetype = "text/x-ocaml";
112-
nbconverter_exporter = None;
113-
pygments_lexer = "OCaml";
114-
};
115-
};
116-
nbformat = 4;
117-
nbformat_minor = 2;
118-
cells = List.rev !cells;
119-
} in
66+
| None -> (
67+
match Mdx.Syntax.infer ~file with
68+
| Some s -> s
69+
| None -> failwith "Couldn't get syntax" )
70+
in
71+
let rec collapse_text = function
72+
| Mdx.Text x :: Mdx.Text y :: xs ->
73+
collapse_text (Mdx.Text (x ^ "\n" ^ y) :: xs)
74+
| (Mdx.Section _ as s) :: Mdx.Text y :: xs ->
75+
let s = Mdx.to_string [ s ] in
76+
collapse_text (Mdx.Text (s ^ "\n" ^ y) :: xs)
77+
| (Mdx.Section _ as s) :: xs ->
78+
let s = Mdx.to_string [ s ] in
79+
collapse_text (Mdx.Text s :: xs)
80+
| x :: ys -> x :: collapse_text ys
81+
| [] -> []
82+
in
83+
List.iter
84+
(function
85+
| Mdx.Text "" -> ()
86+
| Mdx.Text x -> cells := txt x :: !cells
87+
| Mdx.Block { value = OCaml _; contents; _ } ->
88+
cells := ocaml contents :: !cells
89+
| Mdx.Block { value = Toplevel _; contents; file; column; line; _ } ->
90+
let blocks =
91+
Mdx.Toplevel.of_lines ~syntax ~file ~column ~line contents
92+
in
93+
let newcells = List.rev_map toplevel blocks in
94+
cells := newcells @ !cells
95+
| Mdx.Block { value = Raw _; contents; _ } ->
96+
cells := raw contents :: !cells
97+
| x ->
98+
failwith
99+
(Printf.sprintf "internal error, cannot handle: %s"
100+
(Mdx.to_string [ x ])))
101+
(collapse_text items);
102+
"OK")
103+
>>! fun () ->
104+
();
105+
let notebook =
106+
Notebook_t.
107+
{
108+
metadata =
109+
{
110+
kernelspec =
111+
{
112+
display_name = "OCaml 4.07.1";
113+
language = "OCaml";
114+
name = "ocaml-jupyter";
115+
};
116+
language_info =
117+
{
118+
name = "OCaml";
119+
version = "4.07.1";
120+
codemirror_mode = Some "text/x-ocaml";
121+
file_extension = ".ml";
122+
mimetype = "text/x-ocaml";
123+
nbconverter_exporter = None;
124+
pygments_lexer = "OCaml";
125+
};
126+
};
127+
nbformat = 4;
128+
nbformat_minor = 2;
129+
cells = List.rev !cells;
130+
}
131+
in
120132
Printf.fprintf stdout "%s" (Notebook_j.string_of_notebook notebook);
121133
0
122134

123-
124-
125-
let cmd: int Term.t * Term.info =
135+
let cmd : int Term.t * Term.info =
126136
let doc = "Convert an mdx file to a jupyter notebook." in
127-
Term.(pure run $ Cli.setup $ Cli.syntax $ Cli.file ),
128-
Term.info "jupyter" ~doc
137+
(Term.(pure run $ Cli.setup $ Cli.syntax $ Cli.file), Term.info "jupyter" ~doc)

0 commit comments

Comments
 (0)