Skip to content

Commit

Permalink
Add png, pdf and jpeg image support to the latex backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Octachron committed Jan 28, 2025
1 parent 2e03395 commit 064b992
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
22 changes: 19 additions & 3 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 @@ -283,9 +284,24 @@ 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 ]
| Image (Internal (Resolved x), alt) ->
let dir, file = Link.get_dir_and_file x.page in
begin match Fpath.(get_ext @@ v file) with
| "" | ".jpeg" | ".png" | ".pdf" ->
let fpath = Fpath.v (String.concat Fpath.dir_sep (dir @ [file])) in
[ Image fpath ]
| _ ->
txt ~verbatim:false ~in_source:false [ alt ]
@ if in_source then [] else [ Break Paragraph ]
end
| Image (Internal Unresolved, c) | Audio(Internal _, c) | Video (Internal _, c) ->
txt ~verbatim:false ~in_source:false [ c ]
@ if in_source then [] else [ Break Paragraph ]
| Audio (External l, content) | Video (External l, content)
| Image (External l, content) ->
let text = txt ~verbatim:false ~in_source:false [ content ] in
let break = if in_source then [] else [ Break Paragraph ] in
[ External_ref (l, Some text)] @ break
| 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

0 comments on commit 064b992

Please sign in to comment.