Skip to content

Commit

Permalink
make sure ioslides_presentation is properly self-contained when req…
Browse files Browse the repository at this point in the history
…uested (#2428)

Co-authored-by: Yihui Xie <[email protected]>
  • Loading branch information
mnazarov and yihui authored Jan 18, 2023
1 parent 9cf7877 commit 75ccc96
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ rmarkdown 2.20

- Make sure to avoid creating invalid paths when copying resources (thanks, @mnazarov, #2429).

- Make sure `logo` is properly embedded in `ioslides_presentation()` when `self_contained = TRUE` (thanks, @mnazarov, #2428).


rmarkdown 2.19
================================================================================
Expand Down
15 changes: 11 additions & 4 deletions R/ioslides_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ ioslides_presentation <- function(number_sections = FALSE,
if (!length(grep('--wrap', pandoc_args)))
pandoc_args <- c('--wrap', 'none', pandoc_args)

logo_placeholder <- "data:,LOGO"

# pre-processor for arguments that may depend on the name of the
# the input file (e.g. ones that need to copy supporting files)
pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir,
Expand Down Expand Up @@ -360,7 +362,8 @@ ioslides_presentation <- function(number_sections = FALSE,
file.copy(from = logo, to = logo_path)
logo_path <- normalized_relative_to(output_dir, logo_path)
} else {
logo_path <- pandoc_path_arg(logo_path)
# placeholder, will be replaced by base64-encoded logo in post_processor
logo_path <- logo_placeholder
}
args <- c(args, "--variable", paste("logo=", logo_path, sep = ""))
}
Expand Down Expand Up @@ -450,14 +453,18 @@ ioslides_presentation <- function(number_sections = FALSE,
# read the slides
slides_lines <- read_utf8(output_tmpfile)

# read the output file
output_lines <- read_utf8(output_file)

# base64 encode if needed
if (self_contained) {
slides_lines <- base64_encode_images(slides_lines)
if (!is.null(logo)) {
logo_base64 <- if (grepl("^data:", logo)) logo else xfun::base64_uri(logo)
output_lines <- gsub(logo_placeholder, logo_base64, output_lines, fixed = TRUE)
}
}

# read the output file
output_lines <- read_utf8(output_file)

# substitute slides for the sentinel line
sentinel_line <- grep("^RENDERED_SLIDES$", output_lines)
if (length(sentinel_line) == 1) {
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-ioslides.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,31 @@ test_ioslides_presentation_css <- function() {


test_that("ioslides presentation is styled", test_ioslides_presentation_css())

test_ioslides_presentation_logo <- function() {

outputdir <- tempfile()
dir.create(outputdir)
on.exit(unlink(outputdir), add = TRUE)

# Generate mock md file with logo
void <- file.copy("resources/empty.png", file.path(outputdir, "logo.png"))
mdtext <- c("# Slide One\n")
mock <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = TRUE, logo = "logo.png")
html <- mock$html_file

slide_lines <- c(
!any(grepl("logo\\.png", html))
, any(grep("favIcon: 'data:image/png;base64,[^']*'", html))
, any(grepl("background: url\\(data:image/png;base64,[^\\)]*)", html))
)
expect_true(all(slide_lines), info = "slide lines - self contained logo")

# if logo is passed as base64 string, do not re-encode
logobase64 <- xfun::base64_uri("resources/empty.png")
mock2 <- mock_markdown(mdtext = mdtext, outputdir = outputdir, self_contained = TRUE, logo = logobase64)
html2 <- mock2$html_file
expect_equal(html, html2)
}

test_that("ioslides presentation embeds logo", test_ioslides_presentation_logo())

0 comments on commit 75ccc96

Please sign in to comment.