From 72b0e693bb1fb91d8f556346e878296982c58839 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 30 Apr 2024 15:03:28 +0200 Subject: [PATCH] Revert "Fix up knitr paths relative to the output directory" This reverts commit 5703b34f85bcbfb3ae19e4952b1ceb22012f3f96. # Conflicts: # R/tweak-page.R --- R/rmarkdown.R | 3 +-- R/tweak-page.R | 25 +++---------------------- tests/testthat/test-tweak-page.R | 2 +- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/R/rmarkdown.R b/R/rmarkdown.R index 5f01225051..d8641d986b 100644 --- a/R/rmarkdown.R +++ b/R/rmarkdown.R @@ -56,8 +56,7 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images = update_html( path, tweak_rmarkdown_html, - output_dir = path_dir(output_path), - input_dir = path_dir(input_path), + input_path = path_dir(input_path), pkg = pkg ) } diff --git a/R/tweak-page.R b/R/tweak-page.R index c7ed6c13ef..1deecea4f2 100644 --- a/R/tweak-page.R +++ b/R/tweak-page.R @@ -37,38 +37,19 @@ tweak_page <- function(html, name, pkg = list(bs_version = 3)) { } } -tweak_rmarkdown_html <- function(html, input_dir, output_dir, pkg = list(bs_version = 3)) { +tweak_rmarkdown_html <- function(html, input_path, pkg = list(bs_version = 3)) { # Tweak classes of navbar toc <- xml2::xml_find_all(html, ".//div[@id='tocnav']//ul") xml2::xml_attr(toc, "class") <- "nav nav-pills nav-stacked" + # Make sure all images use relative paths img <- xml2::xml_find_all(html, "//img") src <- xml2::xml_attr(img, "src") - - # Drop the logo and any inline images - is_ok <- !grepl("^data:", src) & xml2::xml_attr(img, "class", default = "") != "logo" - img <- img[is_ok] - src <- src[is_ok] - - # Fix up absoluate paths to be relative to input_dir abs_src <- is_absolute_path(src) if (any(abs_src)) { purrr::walk2( img[abs_src], - path_rel(src[abs_src], input_dir), - xml2::xml_set_attr, - attr = "src" - ) - } - - # Fix up paths that are relative to input_dir instead of output_dir - output_dir <- path_real(output_dir) - input_abs_path <- purrr::map_chr(src, ~ path_abs(., input_dir)) - up_path <- !abs_src & path_has_parent(input_abs_path, output_dir) - if (any(up_path)) { - purrr::walk2( - img[up_path], - path_rel(path_abs(src[up_path], input_dir), output_dir), + path_rel(src[abs_src], input_path), xml2::xml_set_attr, attr = "src" ) diff --git a/tests/testthat/test-tweak-page.R b/tests/testthat/test-tweak-page.R index fe8c3fbeb5..5db30340ce 100644 --- a/tests/testthat/test-tweak-page.R +++ b/tests/testthat/test-tweak-page.R @@ -125,7 +125,7 @@ test_that("h1 section headings adjusted to h2 (and so on)", {

2

") - tweak_rmarkdown_html(html, input_dir = ".", output_dir = ".") + tweak_rmarkdown_html(html) expect_equal(xpath_text(html, ".//h1"), "Title") expect_equal(xpath_text(html, ".//h2"), c("1", "2")) expect_equal(xpath_text(html, ".//h3"), "1.1")