Skip to content

Commit f7b9271

Browse files
authored
Web API dependent HTML validation (#1495)
1 parent 7ce33d4 commit f7b9271

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ jobs:
6666
actions: read
6767

6868
steps:
69+
- name: Start HTML validation server
70+
run: |
71+
docker run --rm -p 8888:8888 -d ghcr.io/validator/validator:latest &&
72+
echo "W3C_MARKUP_VALIDATOR_BASEURL=http://0.0.0.0:8888" >> "$GITHUB_ENV"
73+
if: runner.os == 'Linux'
74+
shell: bash
75+
6976
- uses: actions/checkout@v4
7077

7178
- uses: r-lib/actions/setup-r@v2

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Suggests:
7373
downlit (>= 0.4.0),
7474
htmlwidgets,
7575
jsonlite,
76+
curl,
7677
rstudioapi,
7778
miniUI,
7879
rsconnect (>= 0.4.3),

tests/rmd/split-section.Rmd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ See chapter 2 now at \@ref(section-2)
1818

1919
# Section 2
2020

21-
## subsection 2 {#sub2}
21+
## subsection 21 {#sub2}
2222

2323
```{r iris-plot, fig.cap = "A plot"}
2424
plot(iris)
2525
```
2626

2727
See figure \@ref(fig:iris-plot)
28+
29+
# subsection 22
30+
31+
# Section 3
32+
33+
## subsection 3

tests/test-rmd.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
source('./testthat/helper-validate_html.R')
12
# only run this when NOT_CRAN is true (e.g., on Travis CI)
23
if (Sys.getenv('NOT_CRAN') == 'true') local({
34
all_files = function() {
@@ -10,9 +11,16 @@ if (Sys.getenv('NOT_CRAN') == 'true') local({
1011
rmarkdown::render(f, envir = globalenv(), quiet = TRUE)
1112
}
1213

14+
validate_html(list.files("rmd", ".html$", full.names = TRUE))
15+
1316
# split by section works correctly
1417
## id is used for html file name
15-
sections_files = c("section-1.html", "subsection-1.html", "section-2.html", "sub2.html")
18+
sections_files = c(
19+
"section-1.html", "subsection-1.html",
20+
"section-2.html", "sub2.html", "subsection-22.html",
21+
"section-3.html", "subsection-3.html"
22+
)
23+
1624
if (any(!file.exists(file.path("rmd", sections_files))))
1725
stop("Failed to generate sections files")
1826
## reference is working correctly (see #787)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# the HTML validation server was started in GHA
2+
validate_html = function(files, server = Sys.getenv('W3C_MARKUP_VALIDATOR_BASEURL')) {
3+
if (server == '') return()
4+
res = lapply(files, function(f) {
5+
h = curl::new_handle()
6+
curl::handle_setform(handle = h, out = 'json', file = curl::form_file(f, 'text/html'))
7+
jsonlite::fromJSON(rawToChar(curl::curl_fetch_memory(server, h)$content))
8+
})
9+
expected_errors = c(
10+
'Attribute “number” not allowed on element “div” at this point.',
11+
'CSS: “border-top”: “solid\\9” is not a “color” value.',
12+
'CSS: “border-bottom”: “solid\\9” is not a “color” value.'
13+
)
14+
res = do.call(rbind, lapply(res, function(x) {
15+
m <- x$messages$message[x$messages$type == 'error']
16+
m <- setdiff(m, expected_errors)
17+
if (length(m)) data.frame(file = x$url, messages = m)
18+
}))
19+
if (NROW(res) > 0) stop(
20+
'HTML issues detected:\n',
21+
paste0(' ', res$file, ': ', res$messages, collapse = '\n')
22+
)
23+
}

0 commit comments

Comments
 (0)