Skip to content

Commit 5703b34

Browse files
committed
Fix up knitr paths relative to the output directory
For some unknown reason, `knitr_print()` methods sometimes produces paths are relative to the input directory, rather than to the output directory. This PR re-parents those to paths to be relative to the output directory making them work again. Fixes r-lib#2334. Fixes r-lib#2341.
1 parent 2dc1877 commit 5703b34

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

R/rmarkdown.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =
5656
update_html(
5757
path,
5858
tweak_rmarkdown_html,
59-
input_path = path_dir(input_path),
59+
output_dir = path_dir(output_path),
60+
input_dir = path_dir(input_path),
6061
pkg = pkg
6162
)
6263
}

R/tweak-page.R

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,37 @@ tweak_page <- function(html, name, pkg = list(bs_version = 3)) {
3737
}
3838
}
3939

40-
tweak_rmarkdown_html <- function(html, input_path, pkg = list(bs_version = 3)) {
40+
tweak_rmarkdown_html <- function(html, input_dir, output_dir, pkg = list(bs_version = 3)) {
4141
# Tweak classes of navbar
4242
toc <- xml2::xml_find_all(html, ".//div[@id='tocnav']//ul")
4343
xml2::xml_attr(toc, "class") <- "nav nav-pills nav-stacked"
4444

45-
# Make sure all images use relative paths
4645
img <- xml2::xml_find_all(html, "//img")
4746
src <- xml2::xml_attr(img, "src")
47+
48+
# Drop the logo and any inline images
49+
is_ok <- !grepl("^data:", src) & xml2::xml_attr(img, "class", default = "") != "logo"
50+
img <- img[is_ok]
51+
src <- src[is_ok]
52+
53+
# Fix up absoluate paths to be relative to input_dir
4854
abs_src <- is_absolute_path(src)
4955
if (any(abs_src)) {
5056
purrr::walk2(
5157
img[abs_src],
52-
path_rel(src[abs_src], input_path),
58+
path_rel(src[abs_src], input_dir),
59+
xml2::xml_set_attr,
60+
attr = "src"
61+
)
62+
}
63+
64+
# Fix up paths that are relative to input_dir instead of output_dir
65+
input_abs_path <- path_tidy(path(input_dir, src))
66+
up_path <- !abs_src & path_has_parent(input_abs_path, output_dir)
67+
if (any(up_path)) {
68+
purrr::walk2(
69+
img[up_path],
70+
path_rel(path(input_dir, src[up_path]), output_dir),
5371
xml2::xml_set_attr,
5472
attr = "src"
5573
)

tests/testthat/test-tweak-page.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ test_that("h1 section headings adjusted to h2 (and so on)", {
125125
<h1>2</h1>
126126
</div>
127127
")
128-
tweak_rmarkdown_html(html)
128+
tweak_rmarkdown_html(html, input_dir = ".", output_dir = ".")
129129
expect_equal(xpath_text(html, ".//h1"), "Title")
130130
expect_equal(xpath_text(html, ".//h2"), c("1", "2"))
131131
expect_equal(xpath_text(html, ".//h3"), "1.1")

vignettes/img-path.Rmd

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: "imgpathdebug"
3+
---
4+
5+
```{r}
6+
plot(1:5)
7+
```
8+
9+
```{r}
10+
knitr::include_graphics("test/bacon.jpg")
11+
```
12+
13+
```{r}
14+
# magick:::`knit_print.magick-image`()
15+
magick::image_read("https://jeroen.github.io/images/frink.png")
16+
```
17+
18+
```{r}
19+
library(ggplot2)
20+
library(gganimate)
21+
22+
p <- ggplot(mtcars, aes(factor(cyl), mpg)) +
23+
geom_point() +
24+
# Here comes the gganimate code
25+
transition_states(gear)
26+
27+
animate(p, nframes = 1)
28+
```
29+

0 commit comments

Comments
 (0)