Skip to content

Commit b671d0c

Browse files
committed
changelog: infer date from slug if not in metadata
The date is present in 2 places: in the slug and in the yaml metadata. This is redundant and creates extra work for contributors. This makes the yaml metadata optional; in that case the date is inferred from the slug.
1 parent c22de61 commit b671d0c

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

tool/ood-gen/lib/changelog.ml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type metadata = {
22
title : string;
3-
date : string;
3+
date : string option;
44
tags : string list;
55
authors : string list option;
66
description : string option;
@@ -20,9 +20,23 @@ type t = {
2020
[@@deriving
2121
stable_record ~version:metadata
2222
~add:[ authors; changelog; description ]
23-
~remove:[ slug; changelog_html; body_html; body ],
23+
~remove:[ slug; changelog_html; body_html; body ]
24+
~modify:[ date ],
2425
show { with_path = false }]
2526

27+
let re_date_slug =
28+
let open Re in
29+
compile
30+
(seq
31+
[
32+
bos;
33+
group (seq [ rep1 digit; char '-'; rep1 digit; char '-'; rep1 digit ]);
34+
char '-';
35+
])
36+
37+
let parse_date_from_slug s =
38+
Option.map (fun g -> Re.Group.get g 1) (Re.exec_opt re_date_slug s)
39+
2640
let decode (fname, (head, body)) =
2741
let slug = Filename.basename (Filename.remove_extension fname) in
2842
let metadata =
@@ -45,7 +59,17 @@ let decode (fname, (head, body)) =
4559
|> Hilite.Md.transform
4660
|> Cmarkit_html.of_doc ~safe:false)
4761
in
48-
of_metadata ~slug ~changelog_html ~body ~body_html metadata)
62+
let modify_date = function
63+
| Some x -> x
64+
| None -> (
65+
match parse_date_from_slug slug with
66+
| Some x -> x
67+
| None ->
68+
failwith
69+
"date is not present in metadata and could not be parsed \
70+
from slug")
71+
in
72+
of_metadata ~slug ~changelog_html ~body ~body_html ~modify_date metadata)
4973
metadata
5074

5175
let all () =

tool/ood-gen/lib/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
lambdasoup
1717
fpath
1818
ppx_stable
19-
hilite)
19+
hilite
20+
re)
2021
(preprocess
2122
(pps ppx_deriving_yaml ppx_stable ppx_show)))
2223

tool/ood-gen/test/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(test
2+
(name test_ood_gen)
3+
(libraries alcotest ood_gen))

tool/ood-gen/test/test_ood_gen.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
let test_parse_date_from_slug =
2+
let test ~name ~expected s =
3+
( name,
4+
`Quick,
5+
fun () ->
6+
let got = Ood_gen.Changelog.parse_date_from_slug s in
7+
Alcotest.check (Alcotest.option Alcotest.string) __LOC__ expected got )
8+
in
9+
[
10+
test ~name:"ok" "2020-03-02-something.md" ~expected:(Some "2020-03-02");
11+
test ~name:"no date" "something.md" ~expected:None;
12+
]
13+
14+
let tests = [ ("parse_date_from_slug", test_parse_date_from_slug) ]
15+
let () = Alcotest.run __FILE__ tests

0 commit comments

Comments
 (0)