diff --git a/CHANGES.md b/CHANGES.md index 5b863b3463..f714a3a2eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Unreleased +### Added + +- Added support for (local) images in the latex backend (@Octachron, #1297) + ### Changed - Drop support for OCaml < 4.08 (@jonludlam, #1300) diff --git a/src/latex/generator.ml b/src/latex/generator.ml index 6ad4660a74..48808b8168 100644 --- a/src/latex/generator.ml +++ b/src/latex/generator.ml @@ -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 @@ -167,6 +167,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 | [] -> () @@ -277,13 +278,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 ] diff --git a/src/latex/raw.ml b/src/latex/raw.ml index f1781420e4..eb0c5752c0 100644 --- a/src/latex/raw.ml +++ b/src/latex/raw.ml @@ -126,6 +126,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 diff --git a/src/latex/raw.mli b/src/latex/raw.mli index 0616ae350c..6a9d6b448b 100644 --- a/src/latex/raw.mli +++ b/src/latex/raw.mli @@ -66,6 +66,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 *) diff --git a/src/latex/types.ml b/src/latex/types.ml index 3ac77cf730..72b07cc2a9 100644 --- a/src/latex/types.ml +++ b/src/latex/types.ml @@ -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 } diff --git a/test/pages/medias.t/index.mld b/test/pages/medias.t/index.mld index 6ee04f217f..947a00bffb 100644 --- a/test/pages/medias.t/index.mld +++ b/test/pages/medias.t/index.mld @@ -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}} diff --git a/test/pages/medias.t/run.t b/test/pages/medias.t/run.t index 841abe232d..480ed1d00b 100644 --- a/test/pages/medias.t/run.t +++ b/test/pages/medias.t/run.t @@ -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 @@ -28,8 +29,8 @@ Testing the working references: $ cat html/pkg1/index.html | grep img caml.gif - - With alt text and {b emphasis} + + With alt text and {b emphasis} reference @@ -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