Skip to content

Commit 70f7250

Browse files
committed
Frontmatter: Warn when a tag is not before any content
1 parent 340c0e7 commit 70f7250

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

src/model/semantics.ml

+13-6
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,28 @@ let top_level_block_elements status ast_elements =
543543
traverse ~top_heading_level [] ast_elements
544544

545545
let strip_internal_tags ast : internal_tags_removed with_location list * _ =
546-
let rec loop tags ast' = function
546+
let rec loop ~start tags ast' = function
547547
| ({ Location.value = `Tag (#Ast.internal_tag as tag); _ } as wloc) :: tl
548548
-> (
549-
let next tag = loop ({ wloc with value = tag } :: tags) ast' tl in
549+
let next tag =
550+
loop ~start ({ wloc with value = tag } :: tags) ast' tl
551+
in
550552
match tag with
551553
| (`Inline | `Open | `Closed | `Hidden) as tag -> next tag
552-
| `Children_order co -> next (`Children_order co)
554+
| `Children_order co ->
555+
if not start then
556+
Error.raise_warning
557+
(Error.make "@children_order tag has to be before any content"
558+
wloc.location);
559+
next (`Children_order co)
553560
| `Canonical { Location.value = s; location = r_location } -> (
554561
match
555562
Error.raise_warnings (Reference.read_path_longident r_location s)
556563
with
557564
| Result.Ok path -> next (`Canonical path)
558565
| Result.Error e ->
559566
Error.raise_warning e;
560-
loop tags ast' tl))
567+
loop ~start tags ast' tl))
561568
| ({
562569
value =
563570
( `Tag #Ast.ocamldoc_tag
@@ -566,10 +573,10 @@ let strip_internal_tags ast : internal_tags_removed with_location list * _ =
566573
_;
567574
} as hd)
568575
:: tl ->
569-
loop tags (hd :: ast') tl
576+
loop ~start:false tags (hd :: ast') tl
570577
| [] -> (List.rev ast', List.rev tags)
571578
in
572-
loop [] [] ast
579+
loop ~start:true [] [] ast
573580

574581
(** Append alerts at the end of the comment. Tags are favoured in case of alerts of the same name. *)
575582
let append_alerts_to_comment alerts
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{0 One frontmatter}
1+
@children_order page1 page2
22

3-
@children_order page1 page2
3+
{0 One frontmatter}

test/frontmatter/frontmatter.t/run.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ When there is one frontmatter, it is extracted from the content:
5959
When there is more than one children order, we raise a warning and keep only the first entry:
6060

6161
$ odoc compile two_frontmatters.mld
62-
File "two_frontmatters.mld", line 5, characters 0-25:
62+
File "two_frontmatters.mld", line 2, characters 0-25:
6363
Warning: Duplicated @children_order entry
6464
File "two_frontmatters.mld":
6565
Warning: Non-index page cannot specify (children _) in the frontmatter.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
{0 Two frontmatters}
2-
31
@children_order bli1 bli2
2+
@children_order bli3 bli4
43

5-
@children_order bli3 bli4
4+
{0 Two frontmatters}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
@children_order content dir1/ dir1/ typo
2+
13
{0 This is the main index}
24

35
Hello
4-
5-
@children_order content dir1/ dir1/ typo

test/frontmatter/toc_order.t/run.t

+22-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
$ odoc link _odoc/pkg/dir1/page-dontent.odoc
1414

1515
$ odoc compile-index -P test:_odoc/pkg
16-
File "index.mld", line 5, characters 30-35:
16+
File "index.mld", line 1, characters 30-35:
1717
Warning: Duplicate 'dir1/' in (children).
18-
File "index.mld", line 5, characters 36-40:
18+
File "index.mld", line 1, characters 36-40:
1919
Warning: 'typo' in (children) does not correspond to anything.
20-
File "index.mld", line 5, characters 0-40:
20+
File "index.mld", line 1, characters 0-40:
2121
Warning: (children) doesn't include 'omitted'.
2222

2323
$ odoc html-generate --indent --index index.odoc-index -o _html _odoc/pkg/page-index.odocl
@@ -76,17 +76,33 @@ Some more parsing test:
7676

7777
$ mkdir errors
7878
$ cat << EOF >> errors/index.mld
79-
> {0 Test1}
8079
> @children_order [Some wrong content]
80+
> {0 Test1}
8181
> EOF
8282
$ odoc compile --parent-id pkg/doc --output-dir _odoc errors/index.mld
83-
File "errors/index.mld", line 2, characters 16-36:
83+
File "errors/index.mld", line 1, characters 16-36:
8484
Warning: Only words are accepted when specifying children order
8585

8686
$ mkdir valid
8787
$ cat << EOF > valid/index.mld
88-
> {0 Test1}
8988
> @children_order a
9089
> b c
90+
> {0 Test1}
9191
> EOF
9292
$ odoc compile --parent-id pkg/doc --output-dir _odoc valid/index.mld
93+
94+
$ cat << EOF > errors/index.mld
95+
> {0 Test1}
96+
> @children_order a
97+
> EOF
98+
$ odoc compile --parent-id pkg/doc --output-dir _odoc errors/index.mld
99+
File "errors/index.mld", line 2, characters 0-17:
100+
Warning: @children_order tag has to be before any content
101+
102+
$ cat << EOF > errors/not_index.mld
103+
> @children_order a
104+
> {0 Test1}
105+
> EOF
106+
$ odoc compile --parent-id pkg/doc --output-dir _odoc errors/not_index.mld
107+
File "errors/not_index.mld":
108+
Warning: Non-index page cannot specify (children _) in the frontmatter.

0 commit comments

Comments
 (0)