| 1 |
#' Compiler Options for Sass |
| 2 |
#' |
| 3 |
#' Specify compiler `options` for [sass()]. To customize options, either provide |
| 4 |
#' `sass_options()` directly to a [sass()] call or set options globally via |
| 5 |
#' `sass_options_set()`. When `shiny::devmode()` is enabled, |
| 6 |
#' `sass_options_get()` defaults `source_map_embed` and `source_map_contents` to |
| 7 |
#' `TRUE`. |
| 8 |
#' |
| 9 |
#' @param precision Number of decimal places. |
| 10 |
#' @param output_style Bracketing and formatting style of the CSS output. |
| 11 |
#' Possible styles: `"nested"`, `"expanded"`, `"compact"`, and |
| 12 |
#' `"compressed"`. |
| 13 |
#' @param indented_syntax Enables the compiler to parse Sass Indented Syntax in |
| 14 |
#' strings. Note that the compiler automatically overrides this option to |
| 15 |
#' `TRUE` or `FALSE` for files with .sass and .scss file extensions |
| 16 |
#' respectively. |
| 17 |
#' @param indent_type Specifies the indent type as `"space"` or |
| 18 |
#' `"tab"`. |
| 19 |
#' @param indent_width Number of tabs or spaces used for indentation. Maximum |
| 20 |
#' 10. |
| 21 |
#' @param include_path Vector of paths used to resolve `@import`. Multiple |
| 22 |
#' paths are possible using a character vector of paths. |
| 23 |
#' @param source_comments Annotates CSS output with line and file comments from |
| 24 |
#' Sass file for debugging. |
| 25 |
#' @param linefeed Specifies how new lines should be delimited. Possible values: |
| 26 |
#' `"lf"`, `"cr"`, `"lfcr"`, and `"crlf"`. |
| 27 |
#' @param output_path Specifies the location of the output file. Note: this |
| 28 |
#' option will not write the file on disk. It is only for internal reference |
| 29 |
#' with the source map. |
| 30 |
#' @param source_map_file Specifies the location for Sass to write the source |
| 31 |
#' map. |
| 32 |
#' @param source_map_root Value will be included as source root in the source |
| 33 |
#' map information. |
| 34 |
#' @param source_map_embed Embeds the source map as a data URI. |
| 35 |
#' @param source_map_contents Includes the contents in the source map |
| 36 |
#' information. |
| 37 |
#' @param omit_source_map_url Disable the inclusion of source map information in |
| 38 |
#' the output file. Note: must specify `output_path` when `TRUE`. |
| 39 |
#' |
| 40 |
#' @return List of Sass compiler options to be used with [sass()]. For |
| 41 |
#' `sass_options_set()`, any previously set global options are returned. |
| 42 |
#' |
| 43 |
#' @examples |
| 44 |
#' |
| 45 |
#' x <- "foo { margin: 122px * .001; }" |
| 46 |
#' sass(x) |
| 47 |
#' |
| 48 |
#' # Provide options directly to sass() |
| 49 |
#' sass(x, options = sass_options(precision = 1, output_style = "compact")) |
| 50 |
#' |
| 51 |
#' # Or set some option(s) globally |
| 52 |
#' old_options <- sass_options_set(precision = 1) |
| 53 |
#' sass(x) |
| 54 |
#' |
| 55 |
#' # Specify local options while also respecting global options |
| 56 |
#' sass(x, options = sass_options_get(output_style = "compact")) |
| 57 |
#' |
| 58 |
#' # Restore original state |
| 59 |
#' sass_options_set(old_options) |
| 60 |
#' |
| 61 |
#' @export |
| 62 |
sass_options <- function( |
| 63 |
precision = 5, |
| 64 |
output_style = "expanded", |
| 65 |
indented_syntax = FALSE, |
| 66 |
include_path = "", |
| 67 |
source_comments = FALSE, |
| 68 |
indent_type = "space", |
| 69 |
indent_width = 2, |
| 70 |
linefeed = "lf", |
| 71 |
output_path = "", |
| 72 |
source_map_file = "", |
| 73 |
source_map_root = "", |
| 74 |
source_map_embed = FALSE, |
| 75 |
source_map_contents = FALSE, |
| 76 |
omit_source_map_url = FALSE |
| 77 |
) { |
| 78 |
if (indent_width > 10) { |
| 79 |
warning("Maximum indent width is 10. Setting to 10...") |
| 80 |
indent_width <- 10 |
| 81 |
} else if (indent_width < 0) { |
| 82 |
warning("Minimum indent width is 0. Setting to 0...") |
| 83 |
indent_width <- 0 |
| 84 |
} |
| 85 |
|
| 86 |
indent_type <- switch( |
| 87 |
indent_type, |
| 88 |
tab = "\t", |
| 89 |
space = " ", |
| 90 |
stop("invalid indent type. Please specify \"space\" or \"tab\".") |
| 91 |
) |
| 92 |
|
| 93 |
indent <- strrep(indent_type, indent_width) |
| 94 |
|
| 95 |
output_style <- switch( |
| 96 |
output_style, |
| 97 |
nested = 0, |
| 98 |
expanded = 1, |
| 99 |
compact = 2, |
| 100 |
compressed = 3, |
| 101 |
stop("output style not supported.") |
| 102 |
) |
| 103 |
|
| 104 |
linefeed <- switch( |
| 105 |
linefeed, |
| 106 |
lf = "\n", |
| 107 |
cr = "\r", |
| 108 |
crlf = "\r\n", |
| 109 |
lfcr = "\n\r", |
| 110 |
stop("invalid linefeed.") |
| 111 |
) |
| 112 |
|
| 113 |
sep <- switch( |
| 114 |
.Platform$OS.type, |
| 115 |
unix = ":", |
| 116 |
windows = ";", |
| 117 |
stop("unknown operating system") |
| 118 |
) |
| 119 |
|
| 120 |
include_path <- paste(include_path, collapse = sep) |
| 121 |
|
| 122 |
ret <- list( |
| 123 |
precision = precision, |
| 124 |
output_style = output_style, |
| 125 |
indented_syntax = indented_syntax, |
| 126 |
include_path = include_path, |
| 127 |
source_comments = source_comments, |
| 128 |
indent = indent, |
| 129 |
linefeed = linefeed, |
| 130 |
output_path = output_path, |
| 131 |
source_map_file = source_map_file, |
| 132 |
source_map_root = source_map_root, |
| 133 |
source_map_embed = source_map_embed, |
| 134 |
source_map_contents = source_map_contents, |
| 135 |
omit_source_map_url = omit_source_map_url |
| 136 |
) |
| 137 |
|
| 138 |
class(ret) <- c("sass_options", class(ret)) |
| 139 |
ret |
| 140 |
} |
| 141 |
|
| 142 |
#' @rdname sass_options |
| 143 |
#' @param ... arguments to [sass_options()]. For `sass_options_set()`, the |
| 144 |
#' following values are also acceptable: |
| 145 |
#' * `NULL`, clearing the global options. |
| 146 |
#' * Return value of `sass_options_get()`. |
| 147 |
#' * Return value of `sass_options_set()`. |
| 148 |
#' @importFrom utils modifyList |
| 149 |
#' @export |
| 150 |
sass_options_get <- function(...) { |
| 151 |
|
| 152 |
args <- if (in_devmode()) { |
| 153 |
list(source_map_embed = TRUE, source_map_contents = TRUE) |
| 154 |
} else { |
| 155 |
list() |
| 156 |
} |
| 157 |
|
| 158 |
global <- getOption("sass.options", default = list()) |
| 159 |
args <- modifyList(args, global) |
| 160 |
|
| 161 |
local <- verify_sass_options_args(..., .caller = "sass_options_get()") |
| 162 |
args <- modifyList(args, local) |
| 163 |
|
| 164 |
opts <- do.call(sass_options, args) |
| 165 |
if (length(global)) { |
| 166 |
attr(opts, "sass_options_global") <- global |
| 167 |
} |
| 168 |
opts |
| 169 |
} |
| 170 |
|
| 171 |
#' @rdname sass_options |
| 172 |
#' @export |
| 173 |
sass_options_set <- function(...) { |
| 174 |
opts <- list2(...) |
| 175 |
if (length(opts) == 0) { |
| 176 |
stop("`...` must be non-empty.") |
| 177 |
} |
| 178 |
|
| 179 |
if (length(opts) == 1) { |
| 180 |
# sass_options_set(NULL) clears global state |
| 181 |
if (is.null(`..1`)) { |
| 182 |
return(options(sass.options = NULL)) |
| 183 |
} |
| 184 |
# sass_options_set(sass_options_get()) restores state |
| 185 |
g_opts <- attr(`..1`, "sass_options_global") |
| 186 |
if (length(g_opts) > 0) { |
| 187 |
return(options(sass.options = g_opts)) |
| 188 |
} |
| 189 |
# foo <- sass_options_set(foo = "bar"); sass_options_set(foo) restores state |
| 190 |
if (identical(names(opts[[1]]), "sass.options")) { |
| 191 |
return(options(sass.options = opts[[1]]$sass.options)) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
args <- verify_sass_options_args(..., .caller = "sass_options_set()") |
| 196 |
options(sass.options = args) |
| 197 |
} |
| 198 |
|
| 199 |
verify_sass_options_args <- function(..., .caller) { |
| 200 |
args <- list2(...) |
| 201 |
nms <- names2(args) |
| 202 |
if (any(nms == "")) { |
| 203 |
stop("All arguments to `", .caller, "` must be named.", call. = FALSE) |
| 204 |
} |
| 205 |
opts <- names(formals(sass_options)) |
| 206 |
bad_nms <- setdiff(nms, opts) |
| 207 |
if (length(bad_nms)) { |
| 208 |
stop( |
| 209 |
"The following arguments are not supported by `sass_options()`: ", |
| 210 |
paste(bad_nms, collapse = ", "), |
| 211 |
call. = FALSE |
| 212 |
) |
| 213 |
} |
| 214 |
args |
| 215 |
} |
| 216 |
|
| 217 |
# shiny:::in_devmode |
| 218 |
in_devmode <- function() { |
| 219 |
isTRUE(getOption("shiny.devmode", FALSE)) && |
| 220 |
!identical(Sys.getenv("TESTTHAT"), "true") |
| 221 |
} |