From 7f2db7a0406201df0801f4b528fc587ab00c6bde Mon Sep 17 00:00:00 2001 From: Carson Date: Wed, 2 Sep 2020 14:46:03 -0500 Subject: [PATCH 1/2] Add new sass_html_dependencies() function for more convenient htmlDependency bundling --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/sass-dependencies.R | 58 ++++++++++++++++++++++++++ R/sass.R | 2 +- man/sass.Rd | 2 +- man/sass_html_dependencies.Rd | 76 +++++++++++++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 R/sass-dependencies.R create mode 100644 man/sass_html_dependencies.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4defb3d7..fc3b478e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Type: Package Package: sass -Version: 0.2.0.9001 +Version: 0.2.0.9002 Title: Syntactically Awesome Style Sheets ('Sass') Description: An 'SCSS' compiler, powered by the 'LibSass' library. With this, R developers can use variables, inheritance, and functions to generate diff --git a/NAMESPACE b/NAMESPACE index f447bc37..62bf453a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(sass_cache_set) export(sass_file) export(sass_file_cache) export(sass_hash) +export(sass_html_dependencies) export(sass_import) export(sass_layer) export(sass_layer_merge) diff --git a/R/sass-dependencies.R b/R/sass-dependencies.R new file mode 100644 index 00000000..bda99de8 --- /dev/null +++ b/R/sass-dependencies.R @@ -0,0 +1,58 @@ +#' Compile Sass to CSS using LibSass, then return an HTML Dependencies +#' +#' Calls `sass(input, ...)` and wraps the output into a [htmltools::htmlDependency()]. +#' +#' @inheritParams sass +#' @inheritParams htmltools::htmlDependency +#' @param cache_key additional information to include in the `sass_hash()` call used to +#' determine the output directory. This should include any information that could possibly +#' influence the resulting CSS that isn't already captured by `input`. For example, if +#' `input` contains something like `"@import sass_file.scss"` you may want to include the +#' [file.mtime()] of `sass_file.scss` (or, perhaps, a [packageVersion()] if `sass_file.scss` +#' is bundled with an R package). +#' @param stylesheet Absolute file path(s) pointing to stylesheet(s) to include with the dependency +#' +#' @return returns a list of [htmltools::htmlDependency()] objects. The length +#' of the list use 1 unless `input` contains [sass_layer()] object(s) that +#' have `html_deps`. +#' +#' @export +#' @seealso [sass()] +#' @examples +#' str(sass_html_dependencies( +#' input = list(list(color = "red"), "body {color: $color}"), +#' name = "foo", version = "1.0.0", +#' stylesheet = "my-style.css" +#' )) +sass_html_dependencies <- function( + input, name, version, stylesheet, cache_key = NULL, + options = sass_options(), write_attachments = TRUE, cache = sass_cache_get(), + script = NULL, meta = NULL, head = NULL) { + + key <- sass_hash(list(input, options, cache_key)) + outDir <- file.path(tempdir(), paste(name, version, key, sep = "-")) + if (!dir.exists(outDir)) dir.create(outDir) + sass( + input, output = file.path(outDir, stylesheet), options = options, + write_attachments = write_attachments, cache = cache + ) + + if (!is.null(script)) { + if (!all(fs::is_absolute_path(script))) { + stop("script file paths must all be absolute file paths") + } + fs::file_copy(script, outDir, overwrite = TRUE) + script <- basename(script) + } + + c( + htmltools::htmlDependency( + name = name, version = version, + src = c(file = outDir), + stylesheet = stylesheet, + script = script, + meta = meta, head = head + ), + extract_layer(input)$html_deps + ) +} diff --git a/R/sass.R b/R/sass.R index dbe095c5..3babb4c2 100644 --- a/R/sass.R +++ b/R/sass.R @@ -44,7 +44,7 @@ #' @return If \code{output = NULL}, the function returns a string value of the #' compiled CSS. If the output path is specified, the compiled CSS is written #' to that file and \code{invisible()} is returned. -#' @seealso \url{http://sass-lang.com/guide} +#' @seealso \url{http://sass-lang.com/guide}, [sass_html_dependencies()] #' @export #' @examples #' # raw Sass input diff --git a/man/sass.Rd b/man/sass.Rd index 480b772c..1669cfb6 100644 --- a/man/sass.Rd +++ b/man/sass.Rd @@ -87,5 +87,5 @@ sass(list( )) } \seealso{ -\url{http://sass-lang.com/guide} +\url{http://sass-lang.com/guide}, \code{\link[=sass_html_dependencies]{sass_html_dependencies()}} } diff --git a/man/sass_html_dependencies.Rd b/man/sass_html_dependencies.Rd new file mode 100644 index 00000000..c286bc3e --- /dev/null +++ b/man/sass_html_dependencies.Rd @@ -0,0 +1,76 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sass-dependencies.R +\name{sass_html_dependencies} +\alias{sass_html_dependencies} +\title{Compile Sass to CSS using LibSass, then return an HTML Dependencies} +\usage{ +sass_html_dependencies( + input, + name, + version, + stylesheet, + cache_key = NULL, + options = sass_options(), + write_attachments = TRUE, + cache = sass_cache_get(), + script = NULL, + meta = NULL, + head = NULL +) +} +\arguments{ +\item{input}{Accepts raw Sass, a named list of variables, or a list of raw +Sass and/or named variables. See \code{\link{as_sass}} and +\code{\link{sass_import}} / \code{\link{sass_file}} for more details.} + +\item{name}{Library name} + +\item{version}{Library version} + +\item{stylesheet}{Absolute file path(s) pointing to stylesheet(s) to include with the dependency} + +\item{cache_key}{additional information to include in the \code{sass_hash()} call used to +determine the output directory. This should include any information that could possibly +influence the resulting CSS that isn't already captured by \code{input}. For example, if +\code{input} contains something like \code{"@import sass_file.scss"} you may want to include the +\code{\link[=file.mtime]{file.mtime()}} of \code{sass_file.scss} (or, perhaps, a \code{\link[=packageVersion]{packageVersion()}} if \code{sass_file.scss} +is bundled with an R package).} + +\item{options}{Compiler options for Sass. Please specify options using +\code{\link{sass_options}}.} + +\item{write_attachments}{If the input contains \code{\link{sass_layer}} +objects that have file attachments, and \code{output} is not \code{NULL}, +then copy the file attachments to the directory of \code{output}. (Defaults +to \code{NA}, which merely emits a warning if file attachments are present, +but does not write them to disk; the side-effect of writing extra files is +subtle and potentially destructive, as files may be overwritten.)} + +\item{cache}{A \link{FileCache} object created by \code{\link[=sass_file_cache]{sass_file_cache()}}, or \code{NULL} +for no caching.} + +\item{script}{Script(s) to include within the document head (should be +specified relative to the \code{src} parameter).} + +\item{meta}{Named list of meta tags to insert into document head} + +\item{head}{Arbitrary lines of HTML to insert into the document head} +} +\value{ +returns a list of \code{\link[htmltools:htmlDependency]{htmltools::htmlDependency()}} objects. The length +of the list use 1 unless \code{input} contains \code{\link[=sass_layer]{sass_layer()}} object(s) that +have \code{html_deps}. +} +\description{ +Calls \code{sass(input, ...)} and wraps the output into a \code{\link[htmltools:htmlDependency]{htmltools::htmlDependency()}}. +} +\examples{ +str(sass_html_dependencies( + input = list(list(color = "red"), "body {color: $color}"), + name = "foo", version = "1.0.0", + stylesheet = "my-style.css" +)) +} +\seealso{ +\code{\link[=sass]{sass()}} +} From 7dd9962554bbe6ec8cd88f9f4a90035ba8775087 Mon Sep 17 00:00:00 2001 From: Carson Date: Wed, 2 Sep 2020 18:55:55 -0500 Subject: [PATCH 2/2] Correctly collect dependencies --- R/sass-dependencies.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/sass-dependencies.R b/R/sass-dependencies.R index bda99de8..276fdb35 100644 --- a/R/sass-dependencies.R +++ b/R/sass-dependencies.R @@ -46,13 +46,13 @@ sass_html_dependencies <- function( } c( - htmltools::htmlDependency( + list(htmltools::htmlDependency( name = name, version = version, src = c(file = outDir), stylesheet = stylesheet, script = script, meta = meta, head = head - ), + )), extract_layer(input)$html_deps ) }