Skip to content

Commit b9bb771

Browse files
committed
Media: fix warning when a non-asset ref is a media target
Signed-off-by: Paul-Elliot <[email protected]>
1 parent 8892a68 commit b9bb771

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

src/model/semantics.ml

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -285,30 +285,38 @@ let rec nestable_block_element :
285285
(kind, { value = `Reference href; location = href_location }, content, m);
286286
location;
287287
} -> (
288+
let fallback error =
289+
Error.raise_warning error;
290+
let placeholder =
291+
match kind with
292+
| `Simple -> `Code_span href
293+
| `With_text -> `Styled (`Emphasis, content)
294+
in
295+
`Paragraph
296+
(inline_elements status [ placeholder |> Location.at location ])
297+
|> Location.at location
298+
in
288299
match Error.raise_warnings (Reference.parse href_location href) with
289-
| Result.Ok target ->
300+
| Result.Ok target -> (
290301
let text = inline_elements status content in
291-
let target =
292-
match target with
293-
| `Asset _ as a -> a
294-
| `Root (_, `TAsset) as a -> a
295-
| `Root (s, `TUnknown) -> `Root (s, `TAsset)
296-
| `Root _ -> failwith "a"
297-
| `Dot (_, s) -> failwith s
298-
| `Resolved _ -> failwith "todo2"
299-
| _ -> failwith "todo"
300-
in
301-
`Media (`Reference target, m, text) |> Location.at location
302-
| Result.Error error ->
303-
Error.raise_warning error;
304-
let placeholder =
305-
match kind with
306-
| `Simple -> `Code_span href
307-
| `With_text -> `Styled (`Emphasis, content)
302+
let asset_ref_of_ref :
303+
Paths.Reference.t -> (Paths.Reference.Asset.t, _) result =
304+
function
305+
| `Asset _ as a -> Ok a
306+
| `Root (_, `TAsset) as a -> Ok a
307+
| `Root (s, `TUnknown) -> Ok (`Root (s, `TAsset))
308+
| `Dot (p, s) -> Ok (`Dot (p, s))
309+
| _ ->
310+
Error
311+
(not_allowed ~suggestion:"Use a reference to an asset"
312+
href_location ~what:"Non-asset reference"
313+
~in_what:"media target")
308314
in
309-
`Paragraph
310-
(inline_elements status [ placeholder |> Location.at location ])
311-
|> Location.at location)
315+
match asset_ref_of_ref target with
316+
| Error error -> fallback error
317+
| Ok target ->
318+
`Media (`Reference target, m, text) |> Location.at location)
319+
| Result.Error error -> fallback error)
312320

313321
and nestable_block_elements status elements =
314322
List.map (nestable_block_element status) elements

test/pages/medias.t/index.mld

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ Some image:
2626

2727
{2 Links}
2828

29-
{video:https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm}
29+
{video:https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm}
30+
31+
{1 Errors}
32+
33+
- Wrong qualification:{image!module-x}

test/pages/medias.t/run.t

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
We need to odoc-compile the package mld file, listing its children
22

33
$ odoc compile index.mld --child asset-caml.gif
4+
File "index.mld", line 33, characters 30-38:
5+
Warning: Non-asset reference is not allowed in media target.
6+
Suggestion: Use a reference to an asset
47

58
This will have produced a file called 'page-index.odoc'.
69

@@ -15,8 +18,8 @@ Link and generate the HTML (forgetting the asset!):
1518
$ odoc support-files -o html
1619

1720
To test visually, indent:
18-
$ cp -r html /tmp/
19-
$ firefox /tmp/html/index/index.html
21+
$ cp -r html /tmp/
22+
$ firefox /tmp/html/index/index.html
2023

2124
Testing the working references:
2225

@@ -58,6 +61,7 @@ Testing latex and manpages
5861
\ocamlinlinecode{https://picsum.\allowbreak{}photos/200/300}
5962
\ocamlinlinecode{https://upload.\allowbreak{}wikimedia.\allowbreak{}org/wikipedia/commons/f/f1/Cri\_\allowbreak{}du\_\allowbreak{}chameau.\allowbreak{}ogg}
6063
\ocamlinlinecode{https://interactive-examples.\allowbreak{}mdn.\allowbreak{}mozilla.\allowbreak{}net/media/cc0-videos/flower.\allowbreak{}webm}
64+
\ocamlinlinecode{module-x}
6165

6266
$ odoc man-generate -o man page-index.odocl
6367
$ cat man/index.3o | grep gif

0 commit comments

Comments
 (0)