Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add png, pdf and jpeg image support to the latex backend #1297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- Added support for images, videos, audio and other assets
The syntax is `{image!/reference/to/asset}` or `{image:URL}` for images.
The syntax for `{video...}` and `{audio...}` is the same.
(@panglesd, @EmileTrotignon, #1170, #1171, #1184, #1185)
(@panglesd, @EmileTrotignon, #1170, #1171, #1184, #1185, #1297)

- Search using Sherlodoc (@panglesd, @EmileTrotignon, @Julow)
A new search bar that supports full-text and type-based search.
Expand Down
26 changes: 22 additions & 4 deletions src/latex/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ let elt_size (x : elt) =
| Code_fragment _ | Tag _ | Break _ | Ligaturable _ ->
Small
| List _ | Section _ | Verbatim _ | Raw _ | Code_block _ | Indented _
| Description _ ->
| Description _ | Image _ ->
Large
| Table _ | Layout_table _ -> Huge

Expand Down Expand Up @@ -169,6 +169,7 @@ let rec pp_elt ppf = function
| Indented x -> Raw.indent pp ppf x
| Ligaturable s -> Fmt.string ppf s
| Tag (s, t) -> tag s ppf t
| Image target -> Raw.includegraphics Fpath.pp ppf target

and pp ppf = function
| [] -> ()
Expand Down Expand Up @@ -279,13 +280,30 @@ let non_empty_code_fragment c =
let s = source (inline ~verbatim:false ~in_source:true) c in
match s with [] -> [] | _ :: _ as l -> [ Code_fragment l ]

let alt_text ~in_source (target : Target.t) alt =
let text = txt ~verbatim:false ~in_source:false [ alt ] in
let break = if in_source then [] else [ Break Paragraph ] in
match target with
| Internal _ -> text @ break
| External l -> [ External_ref (l, Some text) ] @ break

let image ~in_source (internal_url : Url.t) alt =
let dir, file = Link.get_dir_and_file internal_url.page in
match Fpath.(get_ext @@ v file) with
(* list imported from pdftex.def *)
| "" | ".pdf" | ".png" | ".jpg" | ".mps" | ".jpeg" | ".jbig2" | ".jb2"
| ".PDF" | ".PNG" | ".JPG" | ".JPEG" | ".JBIG2" | ".JB2" ->
let fpath = Fpath.v (String.concat Fpath.dir_sep (dir @ [ file ])) in
[ Image fpath ]
| _ -> alt_text ~in_source (Internal (Resolved internal_url)) alt

let rec block ~in_source (l : Block.t) =
let one (t : Block.one) =
match t.desc with
| Inline i -> inline ~verbatim:false ~in_source:false i
| Audio (_, content) | Video (_, content) | Image (_, content) ->
txt ~verbatim:false ~in_source:false [ content ]
@ if in_source then [] else [ Break Paragraph ]
| Image (Internal (Resolved x), alt) -> image ~in_source x alt
| Image (t, alt) | Audio (t, alt) | Video (t, alt) ->
alt_text ~in_source t alt
| Paragraph i ->
inline ~in_source:false ~verbatim:false i
@ if in_source then [] else [ Break Paragraph ]
Expand Down
2 changes: 2 additions & 0 deletions src/latex/raw.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ let code_block pp ppf x =
Fmt.cut ppf ();
mend ppf name

let includegraphics pp = create "includegraphics" pp

let section pp = create "section" pp

let subsection pp = create "subsection" pp
Expand Down
2 changes: 2 additions & 0 deletions src/latex/raw.mli
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ val small_table : ('a, Types.alignment list option * 'a list list) tr

val input : Fpath.t Fmt.t

val includegraphics : 'a t

(** {1 Required OCaml-specific primitives }
All the macro should be implemented as "ocaml"-suffixed macro
in the latex preamble
Expand Down
1 change: 1 addition & 0 deletions src/latex/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type elt =
| Layout_table of layout_table
| Table of table
| Ligaturable of string
| Image of Fpath.t

and section = { level : int; label : string option; content : t }

Expand Down
2 changes: 1 addition & 1 deletion test/pages/medias.t/index.mld
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Some image:

- Without alt text:{image!caml.gif}
- With an alt text: {{image!caml.gif}With alt text and {b emphasis}}
- With an alt text: {{image!caml.png}With alt text and {b emphasis}}
- Unresolved without alt text: {image!caqzdqzdml.gif}
- Unresolved with alt text: {{image!camezfzeffl.gif}With alt text and {b emphasis}}

Expand Down
8 changes: 6 additions & 2 deletions test/pages/medias.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ We need to odoc-compile the package mld file, listing its children
$ odoc compile index.mld --parent-id pkg1/ --output-dir _odoc

$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name caml.gif
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name caml.png
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name Cri_du_chameau.ogg
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name flower.webm

Expand All @@ -28,8 +29,8 @@ Testing the working references:
$ cat html/pkg1/index.html | grep img
<a href="caml.gif" class="img-link">
<img src="caml.gif" alt="caml.gif"/>
<a href="caml.gif" class="img-link">
<img src="caml.gif" alt="With alt text and {b emphasis}"/>
<a href="caml.png" class="img-link">
<img src="caml.png" alt="With alt text and {b emphasis}"/>
<a href="https://picsum.photos/200/300" class="img-link">
<img src="https://picsum.photos/200/300" alt="reference"/>
<a href="https://picsum.photos/200/300" class="img-link">
Expand Down Expand Up @@ -72,6 +73,9 @@ Testing latex and manpages
caml.gif
./caqzdqzdml.gif

$ cat latex/pkg1/index.tex | grep png
\includegraphics{pkg1/caml.png}}%

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