Skip to content

Commit 6cfa3d5

Browse files
committed
Apply HTML Validation
Squashed commit of the following: commit 2e809f9 Author: Brock <[email protected]> Date: Fri Apr 4 13:26:34 2025 +0200 remove debugging change commit ada57e8 Author: Brock <[email protected]> Date: Fri Apr 4 13:20:10 2025 +0200 don't run html validation on mac or windows commit ff9b8e4 Author: Brock <[email protected]> Date: Fri Apr 4 12:27:04 2025 +0200 Enable HTML validation server commit f196a9e Author: Brock <[email protected]> Date: Fri Apr 4 12:13:09 2025 +0200 Add curl to depends commit 2e51ef2 Author: Brock <[email protected]> Date: Fri Apr 4 12:12:36 2025 +0200 fixes to validate html function commit 777f9ec Author: Brock <[email protected]> Date: Thu Apr 3 18:19:45 2025 +0200 Add API based HTML checking commit 9e4789e Author: Brock <[email protected]> Date: Thu Apr 3 13:15:16 2025 +0200 extra tests
1 parent 758da9f commit 6cfa3d5

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-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: 13 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() {
@@ -9,10 +10,21 @@ if (Sys.getenv('NOT_CRAN') == 'true') local({
910
for (f in list.files('rmd', '[.]Rmd$', full.names = TRUE)) {
1011
rmarkdown::render(f, envir = globalenv(), quiet = TRUE)
1112
}
13+
14+
html_issues = simplify_html_validation(
15+
validate_html(list.files("rmd", ".html$", full.names = TRUE))
16+
)
17+
if(nrow(html_issues) > 0)
18+
stop("HTML issues detected in ", paste(html_issues$file, collapse = ', '))
1219

1320
# split by section works correctly
1421
## id is used for html file name
15-
sections_files = c("section-1.html", "subsection-1.html", "section-2.html", "sub2.html")
22+
sections_files = c(
23+
"section-1.html", "subsection-1.html",
24+
"section-2.html", "sub2.html", "subsection-22.html",
25+
"section-3.html", "subsection-3.html"
26+
)
27+
1628
if (any(!file.exists(file.path("rmd", sections_files))))
1729
stop("Failed to generate sections files")
1830
## reference is working correctly (see #787)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
validate_html <- function(
2+
files,
3+
# using same env variable old package to keep some compatibility
4+
base_url = Sys.getenv('W3C_MARKUP_VALIDATOR_BASEURL')
5+
) {
6+
if(base_url == '' || length(files) == 0) return(NULL)
7+
lapply(files, function(file) {
8+
h <- curl::new_handle()
9+
curl::handle_setform(handle = h, out = 'json', file = curl::form_file(file, "text/html"))
10+
jsonlite::fromJSON(rawToChar(
11+
curl::curl_fetch_memory(base_url, h)$content
12+
))
13+
})
14+
}
15+
16+
simplify_html_validation <- function(results) {
17+
if(is.null(results) || length(results) == 0) return(
18+
data.frame(file=character(0), messages=character(0))
19+
)
20+
expected_errors <- c(
21+
"Attribute “number” not allowed on element “div” at this point.",
22+
"CSS: “border-top”: “solid\\9” is not a “color” value.",
23+
"CSS: “border-bottom”: “solid\\9” is not a “color” value."
24+
25+
)
26+
do.call(rbind, lapply(results,
27+
function(result) {
28+
if(is.null(result)) return(
29+
data.frame(file=character(0), messages=character(0))
30+
)
31+
messages <- result$messages$message[result$messages$type == 'error']
32+
messages <- messages[!messages %in% expected_errors]
33+
data.frame(
34+
file = if (length(messages) > 0) result$url else character(0),
35+
messages = messages
36+
)
37+
}
38+
))
39+
}

0 commit comments

Comments
 (0)