Skip to content

Commit 6e2b449

Browse files
committed
Merge branch 'master' of github.com:realworldocaml/mdx into jupyter
2 parents 3155e5c + ddc6c1e commit 6e2b449

25 files changed

+424
-460
lines changed

bin/pp.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,18 @@ let run () file section =
3434
| Mdx.Section _ | Text _ -> ()
3535
| Block b ->
3636
let b = Mdx.Block.eval b in
37-
Log.debug (fun l -> l "pp: %a" Mdx.Block.dump b);
38-
let pp_lines = Fmt.(list ~sep:(unit "\n") string) in
39-
let contents = Mdx.Block.executable_contents b in
40-
match b.value with
41-
| Toplevel _ -> Fmt.pr "%a\n" pp_lines contents
42-
| OCaml ->
43-
Fmt.pr "%a\n%a\n"
44-
Mdx.Block.pp_line_directive (file, b.line)
45-
pp_lines contents
46-
| _ -> ()
37+
if not (Mdx.Block.skip b) then (
38+
Log.debug (fun l -> l "pp: %a" Mdx.Block.dump b);
39+
let pp_lines = Fmt.(list ~sep:(unit "\n") string) in
40+
let contents = Mdx.Block.executable_contents b in
41+
match b.value with
42+
| Toplevel _ -> Fmt.pr "%a\n" pp_lines contents
43+
| OCaml ->
44+
Fmt.pr "%a\n%a\n"
45+
Mdx.Block.pp_line_directive (file, b.line)
46+
pp_lines contents
47+
| _ -> ()
48+
)
4749
) t;
4850
0
4951

bin/rule.ml

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let prepend_root r b = match r with
2929
| None -> b
3030
| Some r -> Filename.concat r b
3131

32-
let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
32+
let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root ~requires options =
3333
let ml_files = String.Set.elements ml_files in
3434
let ml_files = List.map (prepend_root root) ml_files in
3535
let dirs = match root with
@@ -44,6 +44,10 @@ let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
4444
let f (cpt, acc) _ = cpt + 1, ("y" ^ string_of_int cpt) :: acc in
4545
List.fold_left f (0, []) ml_files |> snd
4646
in
47+
let requires = String.Set.elements requires in
48+
let pp_package_deps fmt name =
49+
Fmt.pf fmt "\ (package %s)" name
50+
in
4751
let pp_ml_deps fmt (var_name, ml_file) =
4852
Fmt.pf fmt "\ (:%s %s)" var_name ml_file
4953
in
@@ -57,30 +61,24 @@ let print_rule ~nd ~prelude ~md_file ~ml_files ~dirs ~root options =
5761
let files = String.Set.of_list (List.map prelude_file prelude) in
5862
String.Set.elements files
5963
|> List.map (fun f -> Fmt.strf " %s" f)
60-
|> String.concat ~sep:"\n"
6164
in
6265
let root = match root with None -> "" | Some r -> Fmt.strf "--root=%s " r in
6366
let deps =
64-
let sep1 = if var_names <> [] then "\n" else "" in
65-
let sep2 = if dirs <> [] && prelude <> "" then "\n" else "" in
66-
let x = Fmt.strf "%a%s%a%s%s"
67-
Fmt.(list ~sep:(unit "\n") pp_ml_deps) (List.combine var_names ml_files)
68-
sep1
69-
Fmt.(list ~sep:(unit "\n") pp_dir_deps) dirs
70-
sep2
71-
prelude
72-
in
73-
match x with
74-
| "" -> ""
75-
| s -> "\n" ^ s
67+
match
68+
List.map (Fmt.to_to_string pp_package_deps) requires @
69+
List.map (Fmt.to_to_string pp_ml_deps) (List.combine var_names ml_files) @
70+
List.map (Fmt.to_to_string pp_dir_deps) dirs @
71+
prelude
72+
with
73+
| [] -> ""
74+
| s -> String.concat ~sep:"\n" ("" :: s)
7675
in
7776
let pp name arg =
7877
Fmt.pr
7978
"\
8079
(alias\n\
8180
\ (name %s)\n\
82-
\ (deps (:x %s)\n\
83-
\ (package mdx)%s)\n\
81+
\ (deps (:x %s)%s)\n\
8482
\ (action (progn\n\
8583
\ (run ocaml-mdx test %a %s%s%%{x})\n%a\n\
8684
\ (diff? %%{x} %%{x}.corrected))))\n"
@@ -100,7 +98,7 @@ let pp_direction fmt = function
10098
| `To_ml -> Fmt.pf fmt "--direction=to-ml"
10199

102100
let pp_prelude fmt s = Fmt.pf fmt "--prelude=%s" s
103-
let pp_prelude_str fmt s = Fmt.pf fmt "--prelude-str=%S" s
101+
let pp_prelude_str fmt s = Fmt.pf fmt "--prelude-str %S" s
104102

105103
let add_opt e s = match e with None -> s | Some e -> String.Set.add e s
106104

@@ -117,7 +115,11 @@ let run () md_file section direction prelude prelude_str root =
117115
let on_item acc = function
118116
| Mdx.Section _ | Text _ -> acc
119117
| Block b when active b ->
120-
let files, dirs, nd = acc in
118+
let files, dirs, nd, requires = acc in
119+
let requires =
120+
Mdx.Block.required_packages b
121+
|> List.fold_left (fun s e -> String.Set.add e s) requires
122+
in
121123
let nd = nd || match Mdx.Block.mode b with
122124
| `Non_det _ -> true
123125
| _ -> false
@@ -129,19 +131,20 @@ let run () md_file section direction prelude prelude_str root =
129131
|> String.Set.union source_trees
130132
in
131133
let files = add_opt (Mdx.Block.file b) files in
132-
files, dirs, nd
134+
files, dirs, nd, requires
133135
| Block _ -> acc
134136
in
135137
let on_file file_contents items =
136-
let ml_files, dirs, nd =
137-
List.fold_left on_item (String.Set.empty, String.Set.empty, false) items
138+
let ml_files, dirs, nd, requires =
139+
let empty = String.Set.empty in
140+
List.fold_left on_item (empty, empty, false, empty) items
138141
in
139142
let options =
140143
List.map (Fmt.to_to_string pp_prelude) prelude @
141144
List.map (Fmt.to_to_string pp_prelude_str) prelude_str @
142145
[Fmt.to_to_string pp_direction direction]
143146
in
144-
print_rule ~md_file ~prelude ~nd ~ml_files ~dirs ~root options;
147+
print_rule ~md_file ~prelude ~nd ~ml_files ~dirs ~root ~requires options;
145148
file_contents
146149
in
147150
Mdx.run md_file ~f:on_file;

bin/test/main.ml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,6 @@ let run_toplevel_tests ?root c ppf tests t =
182182
) tests;
183183
Block.pp_footer ppf ()
184184

185-
let trim l =
186-
let rec aux = function
187-
| [] -> []
188-
| h::t -> if String.trim h = "" then aux t else String.trim h :: t
189-
in
190-
let no_head = aux l in
191-
let no_tail = List.rev (aux (List.rev no_head)) in
192-
no_tail
193-
194185
type file = { first: Mdx_top.Part.file; current: Mdx_top.Part.file }
195186

196187
let files: (string, file) Hashtbl.t = Hashtbl.create 8
@@ -228,8 +219,8 @@ let update_block_with_file ppf t file part =
228219
(match part with None -> "" | Some p -> p)
229220
file
230221
| Some lines ->
231-
let lines = trim lines in
232222
let contents = String.concat "\n" lines in
223+
let contents = String.trim contents in
233224
Output.pp ppf (`Output contents);
234225
Block.pp_footer ppf ()
235226

lib/block.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ let labels = [
149149
`Label "skip" , [`None];
150150
`Label "non-deterministic", [`None; `Some "command"; `Some "output"];
151151
`Label "version" , [`Any];
152+
`Label "require-package" , [`Any];
152153
`Prefix "set-" , [`Any];
153154
`Prefix "unset-" , [`None];
154155
]
@@ -290,6 +291,15 @@ let unset_variables t =
290291
in
291292
List.map f (get_prefixed_labels t "unset-")
292293

294+
let required_packages t =
295+
let f = function
296+
| `Eq, "" ->
297+
Fmt.failwith "invalid `require-package` label value: requires a value"
298+
| `Eq, pkg -> pkg
299+
| _ -> Fmt.failwith "invalid `require-package` label value"
300+
in
301+
List.map f (get_labels t "require-package")
302+
293303
let value t = t.value
294304
let section t = t.section
295305
let header t = t.header

lib/block.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ val set_variables: t -> (string * string) list
9494
val unset_variables: t -> string list
9595
(** [unset_variable t] is the list of environment variable to unset *)
9696

97+
val required_packages: t -> string list
98+
(** [required_packages t] is the names of the required packages *)
99+
97100
val skip: t -> bool
98101
(** [skip t] is true iff [skip] is in the labels of [t]. *)
99102

lib/top/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(library
22
(name mdx_top)
33
(public_name mdx.top)
4+
(modes byte)
45
(preprocess (action (run %{bin:cppo} -V OCAML:%{ocaml_version} %{input-file})))
56
(libraries unix mdx compiler-libs compiler-libs.toplevel threads findlib.top))

0 commit comments

Comments
 (0)