Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check readme images after building reference and articles #2494

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* `build_home()` no longer checks if the README is missing any images. This check is now performed in `build_site()`, after `build_articles()` so you can refer to images created by vignettes with warnings (#2194).
* `build_home()` now includes the contents of `inst/AUTHORS` on the authors page (#2506).
* If you put a dropdown menu (e.g. articles) on the right hand side of the navbar, it will now be right aligned. This makes longer titles more likely to stay on the page (#2421).
* The title for the "Reference" page is now "Package index" since this page might contain more than just function details (#2181).
Expand Down
39 changes: 11 additions & 28 deletions R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
build_home_index <- function(pkg = ".", quiet = TRUE) {
pkg <- section_init(pkg, depth = 0L)

src_path <- path_first_existing(
pkg$src_path,
c("pkgdown/index.md",
"index.md",
"README.md"
)
)
src_path <- path_index(pkg)
dst_path <- path(pkg$dst_path, "index.html")
data <- data_home(pkg)

Expand All @@ -34,12 +28,19 @@ build_home_index <- function(pkg = ".", quiet = TRUE) {
logo = logo_path(pkg, depth = 0)
)

copy_figures(pkg)
check_missing_images(pkg, path_rel(src_path, pkg$src_path), "index.html")

invisible()
}

path_index <- function(pkg) {
path_first_existing(
pkg$src_path,
c("pkgdown/index.md",
"index.md",
"README.md"
)
)
}

data_home <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

Expand Down Expand Up @@ -206,21 +207,3 @@ cran_link <- memoise(function(pkg) {

NULL
})


check_missing_images <- function(pkg, src_path, dst_path) {
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
src <- xml2::xml_attr(xml2::xml_find_all(html, ".//img"), "src")

rel_src <- src[xml2::url_parse(src)$scheme == ""]
rel_path <- fs::path_norm(path(fs::path_dir(dst_path), rel_src))
exists <- fs::file_exists(path(pkg$dst_path, rel_path))

if (any(!exists)) {
paths <- rel_src[!exists]
cli::cli_warn(c(
"Missing images in {.file {src_path}}: {.file {paths}}",
i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
))
}
}
2 changes: 2 additions & 0 deletions R/build.R
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ build_site_local <- function(pkg = ".",
build_search(pkg, override = override)
}

check_built_site(pkg)

cli::cli_rule("Finished building pkgdown site for package {.pkg {pkg$package}}")
preview_site(pkg, preview = preview)
}
27 changes: 27 additions & 0 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ check_pkgdown <- function(pkg = ".") {

cli::cli_inform(c("v" = "No problems found."))
}

check_built_site <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)

cli::cli_rule("Checking for problems")
index_path <- path_index(pkg)
if (!is.null(index_path)) {
check_missing_images(pkg, index_path, "index.html")
}
}

check_missing_images <- function(pkg, src_path, dst_path) {
hadley marked this conversation as resolved.
Show resolved Hide resolved
html <- xml2::read_html(path(pkg$dst_path, dst_path), encoding = "UTF-8")
src <- xml2::xml_attr(xml2::xml_find_all(html, ".//img"), "src")

rel_src <- src[xml2::url_parse(src)$scheme == ""]
rel_path <- fs::path_norm(path(fs::path_dir(dst_path), rel_src))
exists <- fs::file_exists(path(pkg$dst_path, rel_path))

if (any(!exists)) {
paths <- rel_src[!exists]
cli::cli_warn(c(
"Missing images in {.file {path_rel(src_path, pkg$src_path)}}: {.file {paths}}",
i = "pkgdown can only use images in {.file man/figures} and {.file vignettes}"
))
}
}
2 changes: 1 addition & 1 deletion R/rmarkdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ render_rmarkdown <- function(pkg, input, output, ..., seed = NULL, copy_images =
dir_create(unique(path_dir(dst)))
file_copy(src, dst, overwrite = TRUE)
}
check_missing_images(pkg, input, output)
check_missing_images(pkg, input_path, output)

invisible(path)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Writing `articles/html-vignette.html`
Condition
Warning:
Missing images in 'vignettes/html-vignette.Rmd': 'foo.png'
Missing images in 'vignettes/html-vignette.Rmd': 'kitten.jpg'
i pkgdown can only use images in 'man/figures' and 'vignettes'

# articles don't include header-attrs.js script
Expand Down
14 changes: 0 additions & 14 deletions tests/testthat/_snaps/build-home.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@
Writing `authors.html`
Writing `404.html`

# warns about missing images

Code
build_home(pkg)
Message
-- Building home ---------------------------------------------------------------
Writing `authors.html`
Condition
Warning:
Missing images in 'README.md': 'foo.png'
i pkgdown can only use images in 'man/figures' and 'vignettes'
Message
Writing `404.html`

# can build site even if no Authors@R present

Code
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/_snaps/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@
Message
v No problems found.

# warn about missing images in readme

Code
check_built_site(pkg)
Message
-- Checking for problems -------------------------------------------------------
Condition
Warning:
Missing images in 'README.md': 'articles/kitten.jpg'
i pkgdown can only use images in 'man/figures' and 'vignettes'

2 changes: 1 addition & 1 deletion tests/testthat/assets/bad-images/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

![](foo.png)
![](vignettes/kitten.jpg)
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ title: "test"
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
```

![](foo.png)
![](kitten.jpg)
3 changes: 3 additions & 0 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ test_that("image links relative to output", {
})

test_that("warns about missing images", {
# Added in #2509: I can't figure out why this is necessary :(
skip_on_covr()

pkg <- local_pkgdown_site(test_path("assets/bad-images"))
expect_snapshot(build_articles(pkg))
})
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/test-build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ test_that("intermediate files cleaned up automatically", {
)
})

test_that("warns about missing images", {
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
expect_snapshot(build_home(pkg))
})

test_that("can build site even if no Authors@R present", {
skip_if_no_pandoc()

Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,26 @@ test_that("informs if everything is ok", {
pkg <- local_pkgdown_site(test_path("assets/reference"))
expect_snapshot(check_pkgdown(pkg))
})

# built site ---------------------------------------------------------------

test_that("warn about missing images in readme", {
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
suppressMessages(build_home(pkg))

expect_snapshot(check_built_site(pkg))
})

test_that("readme can use images from vignettes", {
pkg <- local_pkgdown_site(test_path("assets/bad-images"))
file_copy(
test_path("assets/articles-images/man/figures/kitten.jpg"),
path(pkg$src_path, "vignettes/kitten.jpg")
)
withr::defer(unlink(path(pkg$src_path, "vignettes/kitten.jpg")))

suppressMessages(build_home(pkg))
suppressMessages(build_articles(pkg))

suppressMessages(expect_no_warning(check_built_site(pkg)))
})
Loading