Skip to content

Commit 8362f66

Browse files
committed
Include blocks do not require an empty block anymore
1 parent a518016 commit 8362f66

File tree

13 files changed

+109
-1
lines changed

13 files changed

+109
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#### Added
44

55
- Allow to explicitly set the kind of blocks in labels: `ocaml`, `cram`, `toplevel` or `include`. (#237, @gpetiot)
6+
- Include blocks do not require an empty block anymore (#286, @gpetiot)
67

78
#### Changed
89

lib/block.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ module Header = struct
3333
| "bash" -> Some (Shell `Bash)
3434
| "ocaml" -> Some OCaml
3535
| s -> Some (Other s)
36+
37+
let infer_from_file file =
38+
match Filename.(remove_extension (basename file), extension file) with
39+
| ("dune" | "dune-project"), _ -> Some (Other "scheme")
40+
| _, (".ml" | ".mli" | ".mlt" | ".eliom" | ".eliomi") -> Some OCaml
41+
| _, ".sh" -> Some (Shell `Sh)
42+
| _ -> None
3643
end
3744

3845
type section = int * string
@@ -462,6 +469,14 @@ let mk ~line ~file ~column ~section ~labels ~legacy_labels ~header ~contents
462469
value;
463470
}
464471

472+
let mk_include ~line ~file ~column ~section ~labels =
473+
match get_label (function File x -> Some x | _ -> None) labels with
474+
| Some file_inc ->
475+
let header = Header.infer_from_file file_inc in
476+
mk ~line ~file ~column ~section ~labels ~legacy_labels:false ~header
477+
~contents:[] ~errors:[]
478+
| None -> label_required ~label:"file" ~kind:"include"
479+
465480
let is_active ?section:s t =
466481
let active =
467482
match s with

lib/block.mli

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Header : sig
2222
val pp : Format.formatter -> t -> unit
2323

2424
val of_string : string -> t option
25+
26+
val infer_from_file : string -> t option
2527
end
2628

2729
(** Code blocks. *)
@@ -104,6 +106,16 @@ val mk :
104106
errors:Output.t list ->
105107
(t, [ `Msg of string ]) Result.result
106108

109+
val mk_include :
110+
line:int ->
111+
file:string ->
112+
column:int ->
113+
section:section option ->
114+
labels:Label.t list ->
115+
(t, [ `Msg of string ]) Result.result
116+
(** [mk_include] builds an include block from a comment [<!-- $MDX ... -->]
117+
that is not followed by a code block [``` ... ```]. *)
118+
107119
(** {2 Printers} *)
108120

109121
val dump : t Fmt.t

lib/compat.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ module Filename = struct
7777
let extension name =
7878
let l = extension_len name in
7979
if l = 0 then "" else String.sub name (String.length name - l) l
80+
81+
let remove_extension name =
82+
let l = extension_len name in
83+
if l = 0 then name else String.sub name 0 (String.length name - l)
8084
#endif
8185
end
8286

lib/lexer_mdx.mll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ rule text section = parse
6969
List.iter (fun _ -> newline lexbuf) errors;
7070
newline lexbuf);
7171
`Block block :: text section lexbuf }
72+
| "<!--" ws* "$MDX" ws* ([^' ' '\n']* as label_cmt) ws* "-->" ws* eol
73+
{ let labels = labels label_cmt in
74+
let file = lexbuf.Lexing.lex_start_p.Lexing.pos_fname in
75+
let column = lexbuf.Lexing.lex_start_p.Lexing.pos_cnum in
76+
newline lexbuf;
77+
let line = !line_ref in
78+
newline lexbuf;
79+
let block =
80+
match Block.mk_include ~file ~line ~column ~section ~labels with
81+
| Ok block -> block
82+
| Error (`Msg msg) -> failwith msg
83+
in
84+
`Block block :: text section lexbuf }
7285
| ([^'\n']* as str) eol
7386
{ newline lexbuf;
7487
`Text str :: text section lexbuf }

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@
203203
(alias runtest)
204204
(action (diff heredoc/test-case.md heredoc.actual)))
205205

206+
(rule
207+
(target include-block.actual)
208+
(deps (package mdx) (source_tree include-block))
209+
(action
210+
(with-stdout-to %{target}
211+
(chdir include-block
212+
(run ocaml-mdx test --output - test-case.md)))))
213+
214+
(rule
215+
(alias runtest)
216+
(action (diff include-block/test-case.md.expected include-block.actual)))
217+
206218
(rule
207219
(target labels-syntax.actual)
208220
(deps (package mdx) (source_tree labels-syntax))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type t
2+
3+
val empty : t

test/bin/mdx-test/expect/include-block/dune

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
We include files with MDX comments:
2+
3+
<!-- $MDX file=code.mli -->
4+
5+
<!-- $MDX file=dune -->
6+
7+
<!-- $MDX file=../.ocamlformat -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
We include files with MDX comments:
2+
3+
<!-- $MDX file=code.mli -->
4+
```ocaml
5+
type t
6+
7+
val empty : t
8+
```
9+
10+
<!-- $MDX file=dune -->
11+
```scheme
12+
13+
```
14+
15+
<!-- $MDX file=../.ocamlformat -->
16+
```
17+
disable=true
18+
```

0 commit comments

Comments
 (0)