Skip to content

Commit

Permalink
Media: fix warning when a non-asset ref is a media target
Browse files Browse the repository at this point in the history
Signed-off-by: Paul-Elliot <[email protected]>
  • Loading branch information
panglesd committed Jan 9, 2024
1 parent 8892a68 commit b9bb771
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
50 changes: 29 additions & 21 deletions src/model/semantics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -285,30 +285,38 @@ let rec nestable_block_element :
(kind, { value = `Reference href; location = href_location }, content, m);
location;
} -> (
let fallback error =
Error.raise_warning error;
let placeholder =
match kind with
| `Simple -> `Code_span href
| `With_text -> `Styled (`Emphasis, content)
in
`Paragraph
(inline_elements status [ placeholder |> Location.at location ])
|> Location.at location
in
match Error.raise_warnings (Reference.parse href_location href) with
| Result.Ok target ->
| Result.Ok target -> (
let text = inline_elements status content in
let target =
match target with
| `Asset _ as a -> a
| `Root (_, `TAsset) as a -> a
| `Root (s, `TUnknown) -> `Root (s, `TAsset)
| `Root _ -> failwith "a"
| `Dot (_, s) -> failwith s
| `Resolved _ -> failwith "todo2"
| _ -> failwith "todo"
in
`Media (`Reference target, m, text) |> Location.at location
| Result.Error error ->
Error.raise_warning error;
let placeholder =
match kind with
| `Simple -> `Code_span href
| `With_text -> `Styled (`Emphasis, content)
let asset_ref_of_ref :
Paths.Reference.t -> (Paths.Reference.Asset.t, _) result =
function
| `Asset _ as a -> Ok a
| `Root (_, `TAsset) as a -> Ok a
| `Root (s, `TUnknown) -> Ok (`Root (s, `TAsset))
| `Dot (p, s) -> Ok (`Dot (p, s))
| _ ->
Error
(not_allowed ~suggestion:"Use a reference to an asset"
href_location ~what:"Non-asset reference"
~in_what:"media target")
in
`Paragraph
(inline_elements status [ placeholder |> Location.at location ])
|> Location.at location)
match asset_ref_of_ref target with
| Error error -> fallback error
| Ok target ->
`Media (`Reference target, m, text) |> Location.at location)
| Result.Error error -> fallback error)

and nestable_block_elements status elements =
List.map (nestable_block_element status) elements
Expand Down
6 changes: 5 additions & 1 deletion test/pages/medias.t/index.mld
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ Some image:

{2 Links}

{video:https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm}
{video:https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm}

{1 Errors}

- Wrong qualification:{image!module-x}
8 changes: 6 additions & 2 deletions test/pages/medias.t/run.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
We need to odoc-compile the package mld file, listing its children

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

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

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

To test visually, indent:
$ cp -r html /tmp/
$ firefox /tmp/html/index/index.html
$ cp -r html /tmp/
$ firefox /tmp/html/index/index.html

Testing the working references:

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

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

0 comments on commit b9bb771

Please sign in to comment.