Skip to content

Commit d7a5963

Browse files
authored
Merge pull request #3 from Julow/metadata-strip-sp
Strip whitespaces from the end of the metadata field
2 parents 626ccc9 + a92eb06 commit d7a5963

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/lexer.mll

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,17 @@ and bad_markup_recovery start_offset input = parse
583583
(Parse_error.bad_markup ("{" ^ rest) ~suggestion);
584584
emit input (`Code_span text) ~start_offset}
585585

586-
(* The second field of the metadata *)
586+
(* The second field of the metadata.
587+
This rule keeps whitespaces and newlines in the 'metadata' field except the
588+
ones just before the '['. *)
587589
and code_block_metadata_tail input = parse
588-
| ((newline | horizontal_space)+ as prefix) (([^ '['])+ as meta) '['
590+
| (space_char+ as prefix)
591+
((space_char* (_ # space_char # ['['])+)+ as meta)
592+
((space_char* '[') as suffix)
589593
{
590-
let meta = with_location_adjustments ~adjust_start_by:prefix ~adjust_end_by:"[" (fun _ -> Loc.at) input meta in
594+
let meta =
595+
with_location_adjustments ~adjust_start_by:prefix ~adjust_end_by:suffix (fun _ -> Loc.at) input meta
596+
in
591597
`Ok (Some meta)
592598
}
593599
| (newline | horizontal_space)* '['

test/test.ml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,7 @@ let%expect_test _ =
25972597
(((f.ml (1 0) (1 46))
25982598
(code_block
25992599
(((f.ml (1 2) (1 7)) ocaml)
2600-
(((f.ml (1 8) (1 29)) "env=f1 version>=4.06 ")))
2600+
(((f.ml (1 8) (1 28)) "env=f1 version>=4.06")))
26012601
((f.ml (1 30) (1 44)) "code goes here")))))
26022602
(warnings ())) |}]
26032603

@@ -2654,10 +2654,32 @@ let%expect_test _ =
26542654
((output
26552655
(((f.ml (1 0) (2 9))
26562656
(code_block
2657-
(((f.ml (1 2) (1 7)) ocaml) (((f.ml (1 8) (2 0)) "kind=toplevel\n")))
2657+
(((f.ml (1 2) (1 7)) ocaml) (((f.ml (1 8) (1 21)) kind=toplevel)))
26582658
((f.ml (2 1) (2 7)) "code ")))))
26592659
(warnings ())) |}]
26602660

2661+
let spaces_after_meta =
2662+
test "{@ocaml kind=toplevel [ code ]}";
2663+
[%expect
2664+
{|
2665+
((output
2666+
(((f.ml (1 0) (1 31))
2667+
(code_block
2668+
(((f.ml (1 2) (1 7)) ocaml) (((f.ml (1 8) (1 21)) kind=toplevel)))
2669+
((f.ml (1 23) (1 29)) "code ")))))
2670+
(warnings ())) |}]
2671+
2672+
let spaces_and_newline_after_meta =
2673+
test "{@ocaml kind=toplevel \n [ code ]}";
2674+
[%expect
2675+
{|
2676+
((output
2677+
(((f.ml (1 0) (2 11))
2678+
(code_block
2679+
(((f.ml (1 2) (1 7)) ocaml) (((f.ml (1 8) (1 21)) kind=toplevel)))
2680+
((f.ml (2 3) (2 9)) "code ")))))
2681+
(warnings ())) |}]
2682+
26612683
let newlines_inside_meta =
26622684
test "{@ocaml kind=toplevel\nenv=e1[ code ]}";
26632685
[%expect

0 commit comments

Comments
 (0)