Skip to content

Commit

Permalink
feat(beamer): support latex_dependencies (#2558)
Browse files Browse the repository at this point in the history
- Switch testthat to version 3 officially
- Add tests for pdf_document and beamer_presentation
  • Loading branch information
atusy committed Jun 17, 2024
1 parent e1c93a9 commit 4b2f342
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 43 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Suggests:
xml2
VignetteBuilder: knitr
Config/Needs/website: rstudio/quillt, pkgdown
Config/testthat/edition: 3
Encoding: UTF-8
RoxygenNote: 7.3.1
SystemRequirements: pandoc (>= 1.14) - http://pandoc.org
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rmarkdown 2.28
================================================================================


- `beamer_presentation` support handling latex dependencies via the new `extra_dependencies` argument and declarations within chunks (e.g., `knitr::asis_output("", meta = list(rmarkdown::latex_dependency("longtable")))`) (thanks, @cderv, @atusy, #2478).

rmarkdown 2.27
================================================================================
Expand Down
9 changes: 8 additions & 1 deletion R/beamer_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ beamer_presentation <- function(toc = FALSE,
self_contained = TRUE,
includes = NULL,
md_extensions = NULL,
pandoc_args = NULL) {
pandoc_args = NULL,
extra_dependencies = NULL) {

# base pandoc options for all beamer output
args <- c()
Expand Down Expand Up @@ -144,6 +145,12 @@ beamer_presentation <- function(toc = FALSE,
# https://github.com/rstudio/rmarkdown/issues/2294
args <- append_in_header(process_header_includes(metadata))

if (length(extra_dependencies) || has_latex_dependencies(knit_meta)) {
extra_dependencies <- latex_dependencies(extra_dependencies)
all_dependencies <- append(extra_dependencies, flatten_latex_dependencies(knit_meta))
args <- c(args, append_in_header(latex_dependencies_as_string(all_dependencies)))
}

# no-op other than caching dir location
args
}
Expand Down
10 changes: 9 additions & 1 deletion man/beamer_presentation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/testthat/test-beamer_presentation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("beamer_presentation() incorporates latex dependencies", {
expected_dependencies <- c(
"\\usepackage{longtable}", # from extra_dependencies
"\\usepackage{hyperref}" # from knit_meta
)

extra_dependencies <- list(
# do not include non-latex dependencies
# as pre processor does not care the kind of extra dependencies
latex_dependency("longtable")
)
knit_meta <- list(
latex_dependency("hyperref"),
html_dependency_jquery() # pre_processor should remove html dependencies
)

fmt <- beamer_presentation(extra_dependencies = extra_dependencies)

pandoc_args <- fmt$pre_processor(
list(),
tempfile(),
"static",
knit_meta,
tempdir(),
tempdir()
)

included <- pandoc_args[which(pandoc_args == "--include-in-header") + 1L]
expect_length(included, 1L)
expect_true(file.exists(included))
expect_identical(readLines(included), expected_dependencies)
})
3 changes: 0 additions & 3 deletions tests/testthat/test-formats.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("formats successfully produce a document", {

testFormat <- function(output_format, df_print = NULL) {
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-github_document.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("toc has correct identifier", {
skip_on_cran() # avoid pandoc issue on CRAN
skip_if_not_pandoc("2.10.1") # changes in gfm writer break this test for earlier versions
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-html_dependencies.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("Correct anchor_sections style is used", {
deps <- html_dependency_anchor_sections
expect_s3_class(deps(), "html_dependency")
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-html_document.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("anchor_sections() adds correct component", {
skip_if_not_pandoc("2.0")
res <- list(args = NULL, lua_filters = NULL, extra_dependencies = NULL)
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-html_document_base.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("Absolute path for image from output directory are made relative to output directory", {
skip_if_not_pandoc("2.0")
skip_if_not_installed("xml2")
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-ioslides_presentation.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("Only mathjax is supported", {
expect_error(ioslides_presentation(math_method = "katex"))
})
3 changes: 0 additions & 3 deletions tests/testthat/test-lua-filters.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

.generate_md_and_convert <- function(content, output_format) {
input_file <- local_rmd_file(c("---\ntitle: Test\n---\n", content))
res <- .render_and_read(input_file, output_format = output_format)
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-math.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("check_math_argument()", {
# valid value
expect_null(check_math_argument(NULL))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-md_document.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local_edition(3)

test_that("adapt_md_variant() adds extensions to markdown variants", {
expect_identical(adapt_md_variant("markdown"), "markdown-yaml_metadata_block-pandoc_title_block")
expect_identical(adapt_md_variant("markdown_phpextra"), "markdown_phpextra-yaml_metadata_block")
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-pandoc.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("build highlight args for pandoc correctly", {
hl_style <- function(name) c("--highlight-style", name)
expect_equal(pandoc_highlight_args(NULL), "--no-highlight")
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-pdf_document.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
test_that("pdf_document() incorporates latex dependencies", {
expected_dependencies <- c(
"\\usepackage{longtable}", # from extra_dependencies
"\\usepackage{hyperref}" # from knit_meta
)

extra_dependencies <- list(
# do not include non-latex dependencies
# as pre processor does not care the kind of extra dependencies
latex_dependency("longtable")
)
knit_meta <- list(
latex_dependency("hyperref"),
html_dependency_jquery() # pre_processor should remove html dependencies
)

fmt <- pdf_document(extra_dependencies = extra_dependencies)

pandoc_args <- fmt$pre_processor(
list(),
tempfile(),
"static",
knit_meta,
tempdir(),
tempdir()
)

included <- pandoc_args[which(pandoc_args == "--include-in-header") + 1L]
expect_length(included, 1L)
expect_true(file.exists(included))
expect_identical(readLines(included), expected_dependencies)
})
3 changes: 0 additions & 3 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: to remove when switching the package to edition 3
local_edition(3)

test_that("Metadata is available before pre_knit", {
message_pre_knit = 'pre_knit handles metadata'
fmt <- md_document()
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-shiny.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: added for new tests - to remove when switching the package to edition 3
local_edition(3)

test_that("file.path.ci returns correctly no matter the case", {
tmp_dir <- withr::local_tempdir()
expect_equal(file.path.ci(tmp_dir, "global.R"), file.path(tmp_dir, "global.R"))
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-shiny_prerendered.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# TODO: added for new tests - to remove when switching the package to edition 3
local_edition(3)

test_that("HTML template contains special comment when in shiny prerendered", {
skip_if_not_pandoc()
special_comment <- "<!-- HEAD_CONTENT -->"
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-site.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local_edition(3)

# copy part of our demo site to a tempdir
local_create_site <- function(files, env = parent.frame()) {
site_dir <- tempfile()
Expand Down

0 comments on commit 4b2f342

Please sign in to comment.