Skip to content

Commit a7edeed

Browse files
panglesdjonludlam
authored andcommitted
Complete odoc extract-code
1 parent b3e681c commit a7edeed

File tree

3 files changed

+56
-44
lines changed

3 files changed

+56
-44
lines changed

src/odoc/bin/main.ml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,21 +1690,18 @@ module Extract_code = struct
16901690
Arg.(value & flag & info ~doc [ "line-directives" ])
16911691

16921692
let names =
1693-
let doc = "From which name of code blocks to extract content" in
1693+
let doc =
1694+
"From which name(s) of code blocks to extract content. When no names are \
1695+
provided, extract all OCaml code blocks."
1696+
in
16941697
Arg.(value & opt_all string [] & info ~doc [ "name" ])
16951698

16961699
let input =
1697-
let doc = "Input $(i,.cmti), $(i,.cmt), $(i,.cmi) or $(i,.mld) file." in
1700+
let doc = "Input $(i,.mld) file." in
16981701
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"FILE" [])
16991702

17001703
let dst =
1701-
let doc =
1702-
"Output file path. Non-existing intermediate directories are created. If \
1703-
absent outputs a $(i,BASE.odoc) file in the same directory as the input \
1704-
file where $(i,BASE) is the basename of the input file. For mld files \
1705-
the \"page-\" prefix will be added if not already present in the input \
1706-
basename."
1707-
in
1704+
let doc = "Output file path." in
17081705
Arg.(
17091706
value
17101707
& opt (some string) None
@@ -1718,8 +1715,7 @@ module Extract_code = struct
17181715
let info ~docs =
17191716
Term.info "extract-code" ~docs
17201717
~doc:
1721-
"Classify the modules into libraries based on heuristics. Libraries \
1722-
are specified by the --library option."
1718+
"Extract code blocks from mld files in order to be able to execute them"
17231719
end
17241720

17251721
let section_pipeline = "COMMANDS: Compilation pipeline"

src/odoc/extract_code.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ let tags_included_in_names names tags =
1111
fields
1212

1313
let needs_extraction names meta =
14-
let check_language l = String.equal "ocaml" l.Loc.value in
15-
let check_name tags =
16-
if List.is_empty names then true
17-
else
18-
match tags with
19-
| None -> false
20-
| Some tags -> tags_included_in_names names tags.Loc.value
14+
let check_language () =
15+
match meta with
16+
| None -> true
17+
| Some { Ast.language; _ } -> String.equal "ocaml" language.Loc.value
2118
in
22-
match meta with
23-
| None -> false
24-
| Some meta -> check_language meta.Ast.language && check_name meta.tags
19+
let check_name () =
20+
match meta with
21+
| Some { Ast.tags = Some tags; _ } ->
22+
tags_included_in_names names tags.Loc.value
23+
| _ -> false
24+
in
25+
match names with [] -> check_language () | _ :: _ -> check_name ()
2526

2627
let print oc line_directives location value =
2728
if line_directives then (

test/extract_code/mld_extraction.t/run.t

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1-
Without --name argument, all OCaml code blocks are extracted
1+
----------- Names ---------------------------------------------
2+
3+
Without --name argument, all **OCaml** code blocks are extracted
24

35
$ odoc extract-code main.mld
6+
7+
(** By default, an odoc code block is assumed to contain OCaml code *)
8+
let () = ()
49
let x = 5
510
let () = print_int x
611

712
let y = x + 6. (* This is a typing error *)
813

914

15+
With --name argument, language does not matter
16+
17+
$ odoc extract-code --name c-quine main.mld
18+
19+
#include <stdio.h>
20+
int main(){
21+
char*a="#include <stdio.h>%cint main(){char*a=%c%s%c;printf(a,10,34,a,34);}";
22+
printf(a,10,34,a,34);
23+
}
24+
25+
Multiple name can be given
26+
27+
$ odoc extract-code --line-directives --name error.ml --name printing main.mld
28+
#18 "main.mld"
29+
let x = 5
30+
#20 "main.mld"
31+
32+
let () = print_int x
33+
34+
#25 "main.mld"
35+
36+
let y = x + 6. (* This is a typing error *)
37+
38+
39+
------- Line directives ---------------------------------------
40+
1041
We can add (OCaml) line directives
1142

1243
$ odoc extract-code --line-directives main.mld
44+
#5 "main.mld"
45+
46+
(** By default, an odoc code block is assumed to contain OCaml code *)
47+
let () = ()
48+
1349
#18 "main.mld"
1450
let x = 5
1551
#20 "main.mld"
@@ -21,7 +57,7 @@ We can add (OCaml) line directives
2157
let y = x + 6. (* This is a typing error *)
2258

2359

24-
We can restrict to a named code blocks
60+
Let's restrict to a named code blocks
2561

2662
$ odoc extract-code --line-directives --name error.ml main.mld
2763
#18 "main.mld"
@@ -56,24 +92,3 @@ Here is the line 26, and the characters 15-17:
5692
let y = x + 6. (* This is a typing error *)
5793
$ sed -n '26p' main.mld | cut -c15-17
5894
6.
59-
60-
We can get content from multiple names
61-
62-
$ odoc extract-code --line-directives --name error.ml --name printing main.mld
63-
#18 "main.mld"
64-
let x = 5
65-
#20 "main.mld"
66-
67-
let () = print_int x
68-
69-
#25 "main.mld"
70-
71-
let y = x + 6. (* This is a typing error *)
72-
73-
$ odoc extract-code --line-directives --name error.ml --name printing -o names.ml main.mld
74-
$ ocaml names.ml
75-
File "main.mld", line 26, characters 15-17:
76-
Error: This expression has type "float" but an expression was expected of type
77-
"int"
78-
5
79-
[2]

0 commit comments

Comments
 (0)