Skip to content

Commit

Permalink
Make save_html() an S3 generic method (#411)
Browse files Browse the repository at this point in the history
Co-authored-by: cpsievert <[email protected]>
  • Loading branch information
cpsievert and cpsievert authored Oct 6, 2023
1 parent 6dddb58 commit 2a26d1f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: htmltools
Title: Tools for HTML
Version: 0.5.6.9000
Version: 0.5.6.9001
Authors@R: c(
person("Joe", "Cheng", , "[email protected]", role = "aut"),
person("Carson", "Sievert", , "[email protected]", role = c("aut", "cre"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ S3method(print,shiny.tag)
S3method(print,shiny.tag.env)
S3method(print,shiny.tag.list)
S3method(print,shiny.tag.query)
S3method(save_html,default)
S3method(str,shiny.tag.env)
export("htmlDependencies<-")
export(HTML)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# htmltools (development version)

## New Features

* `save_html()` is now an S3 generic, allowing for more customization over how certain classes are saved to an HTML file. (#411)

## Improvements

* Fill items no longer set `overflow: auto` or `width: 100%` by default. (#401)
Expand Down
23 changes: 16 additions & 7 deletions R/html_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,27 @@ html_print <- function(html, background = "white", viewer = getOption("viewer",

#' Save an HTML object to a file
#'
#' Save the specified HTML object to a file, copying all of it's
#' dependencies to the directory specified via `libdir`.
#' An S3 generic method for saving an HTML-like object to a file. The default
#' method copies dependency files to the directory specified via `libdir`.
#'
#' @param html HTML content to print
#' @param background Background color for web page
#' @param html HTML content to print.
#' @param file File path or connection. If a file path containing a
#' sub-directory, the sub-directory must already exist.
#' @param libdir Directory to copy dependencies to
#' @param lang Value of the `<html>` `lang` attribute
#' @param ... Further arguments passed to other methods.
#'
#' @export
save_html <- function(html, file, background = "white", libdir = "lib", lang = "en") {
save_html <- function(html, file, ...) {
UseMethod("save_html")
}

#' @rdname save_html
#' @param background Background color for web page.
#' @param libdir Directory to copy dependencies to.
#' @param lang Value of the `<html>` `lang` attribute.
#' @export
save_html.default <- function(html, file, background = "white", libdir = "lib", lang = "en", ...) {
rlang::check_dots_empty()

force(html)
force(background)
force(libdir)
Expand Down
19 changes: 12 additions & 7 deletions man/save_html.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,13 @@ test_that("save_html() can write to a file connection", {
grepl("<h2>Howdy</h2>", paste(readLines(f), collapse = " "))
)
})

test_that("save_html.default() throws when undefined arguments are provided", {
expect_error(
save_html(div(), tempfile(), foo = "bar")
)

expect_error(
save_html(div(), tempfile(), background = "white", libdir = "lib", lang = "en", "bar")
)
})

0 comments on commit 2a26d1f

Please sign in to comment.