Skip to content

Commit

Permalink
Check that articles include alt-text for all images
Browse files Browse the repository at this point in the history
Need to merge after #2518 (since the vignette will need a small update) and #2516 (since `check_built()` has moved).

Fixes #2357
  • Loading branch information
hadley committed May 8, 2024
1 parent 21447ca commit fc75ddf
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 12 deletions.
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_articles()` now reports if you are missing alt-text for any images (#2357).
* `build_reference()` now supports `\Sexpr[results=verbatim]` (@bastistician, #2510).
* `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).
Expand Down
18 changes: 14 additions & 4 deletions R/check.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,27 @@ check_built_site <- function(pkg = ".") {

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")
img <- xml2::xml_find_all(html, ".//img")

src <- xml2::xml_attr(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}}",
cli::cli_inform(c(
x = "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}"
))
}

alt <- xml2::xml_attr(img, "alt")
if (any(is.na(alt))) {
problems <- src[is.na(alt)]
problems[grepl("^data:image", problems)] <- "<base64 encoded image"
cli::cli_inform(c(
x = "Missing alt-text in {.file {path_rel(src_path, pkg$src_path)}}",
set_names(problems, "*")
))
}
}
15 changes: 12 additions & 3 deletions tests/testthat/_snaps/build-articles.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@
Writing `articles/index.html`
Reading vignettes/html-vignette.Rmd
Writing `articles/html-vignette.html`
Condition
Warning:
Missing images in 'vignettes/html-vignette.Rmd': 'kitten.jpg'
x Missing images in 'vignettes/html-vignette.Rmd': 'kitten.jpg'
i pkgdown can only use images in 'man/figures' and 'vignettes'

# warns about missing alt-text

Code
build_article("missing-images", pkg)
Message
Reading vignettes/missing-images.Rmd
Writing `articles/missing-images.html`
x Missing alt-text in 'vignettes/missing-images.Rmd'
* kitten.jpg
* missing-images_files/figure-html/unnamed-chunk-1-1.png

# articles don't include header-attrs.js script

Code
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/_snaps/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
check_built_site(pkg)
Message
-- Checking for problems -------------------------------------------------------
Condition
Warning:
Missing images in 'README.md': 'articles/kitten.jpg'
x 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 @@

![](vignettes/kitten.jpg)
![picture of a cute kitten](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 = "#>")
```

![](kitten.jpg)
![picture of a cute kitten](kitten.jpg)
1 change: 1 addition & 0 deletions tests/testthat/assets/figure/vignettes/figures.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ title: "Test: Figures"
---

```{r}
#| fig-alt: A scatterplot of the numbers 1-10.
plot(1:10)
```
9 changes: 9 additions & 0 deletions tests/testthat/assets/missing-alt/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Package: testpackage
Version: 1.0.0
Title: A test package
Description: A longer statement about the package.
Authors@R: c(
person("Hadley", "Wickham", , "[email protected]", role = c("aut", "cre")),
person("RStudio", role = c("cph", "fnd"))
)
RoxygenNote: 6.0.1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/testthat/assets/missing-alt/vignettes/missing-images.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: "test"
---

![](kitten.jpg)


```{r}
plot(1:5)
```
1 change: 1 addition & 0 deletions tests/testthat/assets/vignette-with-img.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ vignette: >
Some words, and then an image like this:

```{r, echo = FALSE}
#| fig-alt: The pkgdown logo
knitr::include_graphics("open-graph/logo.png")
```
5 changes: 5 additions & 0 deletions tests/testthat/test-build-articles.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ test_that("warns about missing images", {
expect_snapshot(build_articles(pkg))
})

test_that("warns about missing alt-text", {
pkg <- local_pkgdown_site(test_path("assets/missing-alt"))
expect_snapshot(build_article("missing-images", pkg))
})

test_that("articles don't include header-attrs.js script", {
pkg <- as_pkgdown(test_path("assets/articles"))
withr::defer(clean_site(pkg, quiet = TRUE))
Expand Down

0 comments on commit fc75ddf

Please sign in to comment.