Skip to content

Commit a037f90

Browse files
Merge pull request #423 from jonludlam/add-mld-support-v2
Add mld support v2
2 parents 2dffff5 + e854248 commit a037f90

File tree

12 files changed

+124
-30
lines changed

12 files changed

+124
-30
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#### Added
88

9+
- Added support for `mld` files (#423, @jonludlam)
10+
911
#### Changed
1012

1113
#### Deprecated

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## MDX
44

5-
MDX allows to execute code blocks inside markdown and mli documentation
5+
MDX allows to execute code blocks inside markdown and mli/mld documentation
66
to help keeping them up to date.
77

88
Use the
@@ -20,7 +20,7 @@ If you want to contribute to the project, please see the
2020

2121
### Basic Usage
2222

23-
You can use MDX with your Markdown or `.mli` documentation, which ensures
23+
You can use MDX with your Markdown or `.ml{i,d}` documentation, which ensures
2424
code in multi-line or verbatim code blocks is correct.
2525

2626
To enable MDX on specific files you must first enable it for your project by
@@ -113,7 +113,7 @@ dune promote
113113
Now the documentation is up-to-date and running `dune runtest` again should be
114114
successful!
115115

116-
Note that to use the `dune runtest/promote` workflow with `mli` files,
116+
Note that to use the `dune runtest/promote` workflow with `mli` or `mld` files,
117117
you will need to adjust the `mdx` stanza in the `dune` file, as by
118118
[default](https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4),
119119
Dune only checks markdown files with `mdx`. E.g.,
@@ -138,7 +138,7 @@ block it is attached to.
138138
```ocaml
139139
```
140140

141-
The `.mli` syntax for this is is slightly different to match the conventions of
141+
The `.mli` and `.mld` syntax for this is is slightly different to match the conventions of
142142
OCaml documentation comments:
143143

144144
(** This is an documentation comment with an ocaml block

lib/block.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ let pp_errors ppf t =
171171

172172
let pp_footer ?syntax ppf _ =
173173
match syntax with
174-
| Some Syntax.Mli -> Fmt.string ppf "]}"
174+
| Some Syntax.Mli | Some Syntax.Mld -> Fmt.string ppf "]}"
175175
| Some Syntax.Cram -> Fmt.string ppf "\n"
176176
| Some Syntax.Markdown | None -> Fmt.string ppf "```\n"
177177

@@ -181,7 +181,8 @@ let pp_legacy_labels ppf = function
181181

182182
let pp_labels ?syntax ppf labels =
183183
match syntax with
184-
| Some Syntax.Mli -> Fmt.(list ~sep:(any ",") Label.pp) ppf labels
184+
| Some Syntax.Mli | Some Syntax.Mld ->
185+
Fmt.(list ~sep:(any ",") Label.pp) ppf labels
185186
| Some Syntax.Cram -> (
186187
match labels with
187188
| [] -> ()
@@ -199,7 +200,7 @@ let pp_labels ?syntax ppf labels =
199200

200201
let pp_header ?syntax ppf t =
201202
match syntax with
202-
| Some Syntax.Mli ->
203+
| Some Syntax.Mli | Some Syntax.Mld ->
203204
let lang_headers, other_labels =
204205
List.partition
205206
(function Label.Language_tag _ -> true | _ -> false)

lib/mdx.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ let parse l =
7171
let parse_lexbuf syntax Misc.{ string; lexbuf } =
7272
match syntax with
7373
| Syntax.Mli -> Mli_parser.parse_mli string
74+
| Syntax.Mld -> Mli_parser.parse_mld string
7475
| Markdown ->
7576
Util.Result.to_error_list @@ Lexer_mdx.markdown_token lexbuf >>= parse
7677
| Cram -> Util.Result.to_error_list @@ Lexer_mdx.cram_token lexbuf >>= parse
@@ -80,6 +81,7 @@ let parse_file syntax f = Misc.load_file ~filename:f |> parse_lexbuf syntax
8081
let of_string syntax s =
8182
match syntax with
8283
| Syntax.Mli -> Mli_parser.parse_mli s
84+
| Syntax.Mld -> Mli_parser.parse_mld s
8385
| Syntax.Markdown | Syntax.Cram ->
8486
Misc.{ lexbuf = Lexing.from_string s; string = s } |> parse_lexbuf syntax
8587

lib/mli_parser.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ let make_block code_block file_contents =
150150
Block.mk ~loc:code_block.code_block ~section:None ~labels ~header
151151
~contents ~legacy_labels:false ~errors:[]
152152
153-
let parse_mli file_contents =
154-
(* Find the locations of the code blocks within [file_contents], then slice it up into
155-
[Text] and [Block] parts by using the starts and ends of those blocks as
156-
boundaries. *)
157-
let code_blocks = docstring_code_blocks file_contents in
153+
(* Given the locations of the code blocks within [file_contents], then slice it up into
154+
[Text] and [Block] parts by using the starts and ends of those blocks as
155+
boundaries. *)
156+
let extract_blocks code_blocks file_contents =
158157
let cursor, tokens =
159158
List.fold_left
160159
(fun (cursor, code_blocks) (code_block : Code_block.t) ->
@@ -183,5 +182,16 @@ let parse_mli file_contents =
183182
else tokens
184183
185184
let parse_mli file_contents =
186-
try Ok (parse_mli file_contents)
185+
try
186+
let code_blocks = docstring_code_blocks file_contents in
187+
Ok (extract_blocks code_blocks file_contents)
187188
with exn -> Error [ `Msg (Printexc.to_string exn) ]
189+
190+
let parse_mld ?(filename = "_none_") file_contents =
191+
let location =
192+
Lexing.{ pos_bol = 0; pos_lnum = 1; pos_cnum = 0; pos_fname = filename }
193+
in
194+
let code_blocks =
195+
extract_code_block_info [] ~location ~docstring:file_contents |> List.rev
196+
in
197+
Ok (extract_blocks code_blocks file_contents)

lib/mli_parser.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
val parse_mli : string -> (Document.line list, [ `Msg of string ] list) result
22
(** Slice an mli file into its [Text] and [Block] parts. *)
3+
4+
val parse_mld :
5+
?filename:string ->
6+
string ->
7+
(Document.line list, [ `Msg of string ] list) result
8+
(** Slice an mld file into its [Text] and [Block] parts. *)

lib/syntax.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
type t = Markdown | Cram | Mli
1+
type t = Markdown | Cram | Mli | Mld
22

33
let pp fs = function
44
| Markdown -> Fmt.string fs "markdown"
55
| Cram -> Fmt.string fs "cram"
66
| Mli -> Fmt.string fs "mli"
7+
| Mld -> Fmt.string fs "mld"
78

89
let equal x y = x = y
910

@@ -12,10 +13,12 @@ let infer ~file =
1213
| ".t" -> Some Cram
1314
| ".md" -> Some Markdown
1415
| ".mli" -> Some Mli
16+
| ".mld" -> Some Mld
1517
| _ -> None
1618

1719
let of_string = function
1820
| "markdown" | "normal" -> Some Markdown
1921
| "cram" -> Some Cram
2022
| "mli" -> Some Mli
23+
| "mld" -> Some Mld
2124
| _ -> None

lib/syntax.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type t = Markdown | Cram | Mli
1+
type t = Markdown | Cram | Mli | Mld
22

33
val pp : Format.formatter -> t -> unit
44
val equal : t -> t -> bool

test/bin/gen_rule_helpers/gen_rule_helpers.ml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ let get_files dir =
3737
read_dir dir |> List.filter is_file
3838

3939
let cwd_options_file = "test-case.opts"
40-
let cwd_test_file_md = "test-case.md"
41-
let cwd_test_file_t = "test-case.t"
42-
let cwd_test_file_mli = "test-case.mli"
40+
41+
let cwd_test_files =
42+
[ "test-case.md"; "test-case.t"; "test-case.mli"; "test-case.mld" ]
43+
4344
let cwd_enabled_if_file = "test-case.enabled-if"
4445

4546
type dir = {
@@ -52,20 +53,14 @@ type dir = {
5253
}
5354

5455
let test_file ~dir_name files =
55-
let is_test_file f =
56-
f = cwd_test_file_md || f = cwd_test_file_t || f = cwd_test_file_mli
57-
in
56+
let is_test_file f = List.mem f cwd_test_files in
5857
match List.filter is_test_file files with
5958
| [ test_file ] -> test_file
60-
| [] ->
61-
Printf.eprintf "No test file for %s\n" dir_name;
62-
Printf.eprintf "There should be one of %s, %s, or %s\n" cwd_test_file_md
63-
cwd_test_file_t cwd_test_file_mli;
64-
exit 1
65-
| _ ->
66-
Printf.eprintf "More than one test file for %s\n" dir_name;
67-
Printf.eprintf "There should be only one of %s, %s, or %s\n"
68-
cwd_test_file_md cwd_test_file_t cwd_test_file_mli;
59+
| found_files ->
60+
Format.eprintf "Invalid number of test file for %s (found %a)\n" dir_name
61+
pp_string_list found_files;
62+
Format.eprintf "There should be exactly one of [%a]\n" pp_string_list
63+
cwd_test_files;
6964
exit 1
7065

7166
let expected_file ~dir_name ~test_file files =

test/bin/mdx-test/expect/dune.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,18 @@
467467
(alias runtest)
468468
(action (diff shell-file-inc/test-case.md.expected shell-file-inc.actual)))
469469

470+
(rule
471+
(target simple-mld.actual)
472+
(deps (package mdx) (source_tree simple-mld))
473+
(action
474+
(with-stdout-to %{target}
475+
(chdir simple-mld
476+
(run ocaml-mdx test --output - --syntax=mld test-case.mld)))))
477+
478+
(rule
479+
(alias runtest)
480+
(action (diff simple-mld/test-case.mld simple-mld.actual)))
481+
470482
(rule
471483
(target simple-mli.actual)
472484
(deps (package mdx) (source_tree simple-mli))

0 commit comments

Comments
 (0)