Skip to content

Commit 1ef6743

Browse files
committed
Allow restricting image formats
This is currently opt in. If you set `restrict_image_formats` to `TRUE`, then SVG images are HTML-only and PDF images are PDF-only. Other file types are not restricted. We could try to make it opt out, because SVGs do not work in LaTeX and PDFs most of the time do not work in HTML. If it is opt in, then we don't necessarily need a NEWS item to try this out, but if it ends up being opt out, then we do and we also need to document it, Closes #1398.
1 parent 79372ca commit 1ef6743

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

R/markdown.R

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,44 @@ mdxml_link_text <- function(xml_contents, state) {
424424
paste0(text, collapse = "")
425425
}
426426

427-
mdxml_image = function(xml) {
427+
mdxml_image <- function(xml) {
428428
dest <- xml_attr(xml, "destination")
429429
title <- xml_attr(xml, "title")
430+
fmt <- get_image_format(dest)
430431
paste0(
432+
if (fmt == "html") "\\if{html}{",
433+
if (fmt == "pdf") "\\if{pdf}{",
431434
"\\figure{", dest, "}",
432-
if (nchar(title)) paste0("{", title, "}")
435+
if (nchar(title)) paste0("{", title, "}"),
436+
if (fmt %in% c("html", "pdf")) "}"
437+
)
438+
}
439+
440+
get_image_format <- function(path) {
441+
should_restrict <- roxy_meta_get("restrict_image_formats") %||% FALSE
442+
if (!should_restrict) {
443+
return("all")
444+
}
445+
446+
path <- tolower(path)
447+
rx <- default_image_formats()
448+
html <- grepl(rx$html, path)
449+
pdf <- grepl(rx$pdf, path)
450+
if (html && pdf) {
451+
"all"
452+
} else if (html) {
453+
"html"
454+
} else if (pdf) {
455+
"pdf"
456+
} else {
457+
"all"
458+
}
459+
}
460+
461+
default_image_formats <- function() {
462+
list(
463+
html = "[.](jpg|jpeg|gif|png|svg)$",
464+
pdf = "[.](jpg|jpeg|gif|png|pdf)$"
433465
)
434466
}
435467

tests/testthat/_snaps/markdown.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@
8888
}
8989
9090

91+
# image formats work
92+
93+
Code
94+
roc_proc_text(rd_roclet(),
95+
"\n #' Title\n #'\n #' ![](example.svg \"Plot title 1\")\n #' ![](example.pdf \"Plot title 2\")\n #' ![](example.PNG \"Plot title 3\")\n #' @md\n foo <- function() { }\n ")
96+
Output
97+
$foo.Rd
98+
% Generated by roxygen2: do not edit by hand
99+
% Please edit documentation in ./<text>
100+
\name{foo}
101+
\alias{foo}
102+
\title{Title}
103+
\usage{
104+
foo()
105+
}
106+
\description{
107+
\if{html}{\figure{example.svg}{Plot title 1}}
108+
\if{pdf}{\figure{example.pdf}{Plot title 2}}
109+
\figure{example.PNG}{Plot title 3}
110+
}
111+
112+

tests/testthat/test-markdown.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,19 @@ test_that("can override default options", {
648648
")[[1]]
649649
expect_match(out$get_section("description")$value, "###", fixed = TRUE)
650650
})
651+
652+
test_that("image formats work", {
653+
local_roxy_meta_set("restrict_image_formats", TRUE)
654+
655+
expect_snapshot(
656+
roc_proc_text(rd_roclet(), "
657+
#' Title
658+
#'
659+
#' ![](example.svg \"Plot title 1\")
660+
#' ![](example.pdf \"Plot title 2\")
661+
#' ![](example.PNG \"Plot title 3\")
662+
#' @md
663+
foo <- function() { }
664+
")
665+
)
666+
})

0 commit comments

Comments
 (0)