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

Allow to set the lang attribute, will close #82 #185

Merged
merged 2 commits into from
Oct 8, 2020
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
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ htmltools 0.5.0.9000

* `print(as.tags(x))` no longer results in error when `x` is a generic `list()` of tag-like objects. (#181)

* `save_html()` now has a `lang` parameter that can be used to set the lang attribute of `<html>`. (@ColinFay, #185)

htmltools 0.5.0
--------------------------------------------------------------------------------

Expand Down
5 changes: 3 additions & 2 deletions R/html_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ html_print <- function(html, background = "white", viewer = getOption("viewer",
#' @param background Background color for web page
#' @param file File to write content to
#' @param libdir Directory to copy dependencies to
#' @param lang Value of the `<html>` `lang` attribute
#'
#' @export
save_html <- function(html, file, background = "white", libdir = "lib") {
save_html <- function(html, file, background = "white", libdir = "lib", lang = "en") {
force(html)
force(background)
force(libdir)
Expand Down Expand Up @@ -96,7 +97,7 @@ save_html <- function(html, file, background = "white", libdir = "lib") {

# build the web-page
html <- c("<!DOCTYPE html>",
"<html>",
sprintf('<html lang="%s">', lang),
"<head>",
"<meta charset=\"utf-8\"/>",
sprintf("<style>body{background-color:%s;}</style>", htmlEscape(background)),
Expand Down
6 changes: 4 additions & 2 deletions man/save_html.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ test_that("Special characters are not re-encoded", {
}
)
})


test_that("save_html() language parameter is set", {
output <- tempfile(fileext = ".html")
# test for default
save_html("<h2>Howdy</h2>", output)
output_read <- readLines(output)
expect_true(
grepl("<html lang=\"en\">", paste(output_read, collapse = " "))
)
# test for fr
save_html("<h2>Howdy</h2>", output, lang = "fr")
output_read <- readLines(output)
expect_true(
grepl("<html lang=\"fr\">", paste(output_read, collapse = " "))
)
wch marked this conversation as resolved.
Show resolved Hide resolved
})