Skip to content

Commit

Permalink
More explicit message when caching a google font (#141)
Browse files Browse the repository at this point in the history
* More explicit message when caching a google font

* Only message for real caches

* Also record path

* Show download message when no cache is present

* Tweak transform

* Update NEWS.md

---------

Co-authored-by: Carson <cpsievert1@gmail.com>
  • Loading branch information
hadley and cpsievert authored Apr 24, 2024
1 parent e58a619 commit 9228fcf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# sass (development version)

- More informative output when `font_google()` downloads google font files (for `font_google(local=TRUE)`).

# sass 0.4.9

- Closed #138: font_google(local = TRUE) now uses woff2 (instead of woff) for a font file type. (#139)
Expand Down
24 changes: 19 additions & 5 deletions R/fonts.R
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ font_dep_google_local <- function(x) {
dir.create(tmpdir, recursive = TRUE)
css_file <- file.path(tmpdir, "font.css")

has_cache <- is_cache_object(x$cache)
x$cache <- resolve_cache(x$cache)

css_key <- hash_with_user_agent(x$href)
Expand All @@ -429,6 +430,7 @@ font_dep_google_local <- function(x) {

# If need be, download the font file(s) that the CSS imports,
# and modify the CSS to point to the local files
needs_download_message <- TRUE
Map(function(url, nm) {
key <- hash_with_user_agent(nm)
f <- file.path(tmpdir, nm)
Expand All @@ -441,7 +443,17 @@ font_dep_google_local <- function(x) {
x$cache$remove(css_key)
return(font_dep_google_local(x))
}
download_file(url, f)

if (needs_download_message) {
needs_download_message <<- FALSE
download_msg <- paste0("Downloading google font ", x$family)
if (has_cache) {
download_msg <- paste0(download_msg, " to local cache (", x$cache$dir(), ")")
}
rlang::inform(download_msg)
}

download_file(url, f, quiet = TRUE)
x$cache$set_file(key, f)
css <<- sub(url, nm, css, fixed = TRUE)
}, urls, basenames)
Expand All @@ -466,7 +478,8 @@ read_gfont_url <- function(url, file) {

download_file(
utils::URLencode(url), file,
headers = c("User-Agent" = gfont_user_agent())
headers = c("User-Agent" = gfont_user_agent()),
quiet = TRUE
)
readLines(file)
}
Expand All @@ -493,7 +506,8 @@ extract_group <- function(x, pattern, which = 1) {
# similar to thematic:::download_file, but also translates headers to curl
#' @importFrom stats na.omit
#' @importFrom utils download.file packageVersion
download_file <- function(url, dest, headers = NULL, ...) {
download_file <- function(url, dest, headers = NULL, quiet = FALSE, ...) {

if (is_installed("curl")) {
if (!curl::has_internet()) {
warning(
Expand All @@ -505,11 +519,11 @@ download_file <- function(url, dest, headers = NULL, ...) {
)
}
handle <- curl::handle_setheaders(curl::new_handle(), .list = headers)
return(curl::curl_download(url, dest, handle = handle, quiet = FALSE, ...))
return(curl::curl_download(url, dest, handle = handle, quiet = quiet, ...))
}

if (capabilities("libcurl")) {
return(download.file(url, dest, method = "libcurl", headers = headers, ...))
return(download.file(url, dest, method = "libcurl", headers = headers, quiet = quiet, ...))
}

stop(
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/_snaps/font-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@

# font_google(local = TRUE) basically works

Code
tagz <- renderTags(tags$style(sass(scss)))
Message
Downloading google font Pacifico to local cache (<temp-cache>)

---

Code
tagz <- renderTags(tags$style(sass(scss)))

---

Code
tagz$html
Output
Expand Down
7 changes: 5 additions & 2 deletions tests/testthat/test-font-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ test_that("font_google(local = TRUE) basically works", {
)

# 1st time rendering font should add files to cache
tagz <- renderTags(tags$style(sass(scss)))
expect_snapshot(
tagz <- renderTags(tags$style(sass(scss))),
transform = function(x) gsub("\\(.*\\)", "(<temp-cache>)", x)
)
size <- cache$size()
expect_true(size > 0)

# 2md time should result in cache hit
tagz <- renderTags(tags$style(sass(scss)))
expect_snapshot(tagz <- renderTags(tags$style(sass(scss))))
expect_true(size == cache$size())

# Make sure the markup structure and files are as expected
Expand Down

0 comments on commit 9228fcf

Please sign in to comment.