Debian Codegraph @

R/as_sass.R

raw
1 #' Convert an R object into Sass code
2 #'
3 #' Converts multiple types of inputs to a single Sass input string for
4 #' [sass()].
5 #'
6 #' @param input Any of the following:
7 #' * A character vector containing Sass code.
8 #' * A named list containing variable names and values.
9 #' * A [sass_file()], [sass_layer()], and/or [sass_bundle()].
10 #' * A [list()] containing any of the above.
11 #'
12 #' @references <https://sass-lang.com/documentation/at-rules/import>
13 #' @return a single character value to be supplied to [sass()].
14 #' @export
15 #' @examples
16 #' # Example of regular Sass input
17 #' as_sass("body { color: \"blue\"; }")
18 #'
19 #' # There is support for adding variables
20 #' as_sass(
21 #' list(
22 #' list(color = "blue"),
23 #' "body { color: $color; }"
24 #' )
25 #' )
26 #'
27 #' \donttest{
28 #' # Add a file name
29 #' someFile <- tempfile("variables")
30 #'
31 #' # Overwrite color to red
32 #' write("$color: \"red\";", someFile)
33 #'
34 #' input <-
35 #' as_sass(
36 #' list(
37 #' list(color = "blue"),
38 #' sass_file(someFile),
39 #' "body { color: $color; }"
40 #' )
41 #' )
42 #'
43 #' input
44 #'
45 #' # The final body color is red
46 #' sass(input)
47 #' }
48 as_sass <- function(input) {
49 attachDependencies(
50 as_html(as_sass_(input), "sass"),
51 find_dependencies(input)
52 )
53 }
54
55 find_dependencies <- function(x) {
56
57 deps <- if (is_sass_bundle_like(x)) {
58 x <- as_sass_layer(x)
59 deps <- x$html_deps
60 x$html_deps <- NULL
61 deps
62 } else if (is_font_collection(x)) {
63 x$html_deps
64 } else {
65 htmlDependencies(x)
66 }
67 childDeps <- NULL
68 # only recurse into generic lists
69 if (is.list(x)) {
70 childDeps <- unlist(
71 lapply(x, find_dependencies),
72 recursive = FALSE, use.names = FALSE
73 )
74 }
75 c(childDeps, deps)
76 }
77
78 discard_dependencies <- function(x) {
79 if (is_sass_bundle_like(x)) {
80 x <- as_sass_layer(x)
81 x$html_deps <- NULL
82 } else if (is_font_collection(x)) {
83 x$html_deps <- NULL
84 } else if (inherits(x, "html_dependency")) {
85 x <- NULL
86 } else {
87 htmlDependencies(x) <- NULL
88 }
89 if (is.list(x)) {
90 lapply(x, discard_dependencies)
91 } else {
92 x
93 }
94 }
95
96 as_sass_ <- function(input) {
97 UseMethod("as_sass_")
98 }
99
100 as_sass_.default <- function(input) {
101 stop("as_sass() not implemented for object of class: '", class(input)[1], "'")
102 }
103
104 as_sass_.NULL <- function(input) {
105 "null"
106 }
107 as_sass_.numeric <- function(input) {
108 as.character(input)
109 }
110 as_sass_.logical <- function(input) {
111 if (input) {
112 "true"
113 } else {
114 "false"
115 }
116 }
117
118 as_sass_.list <- function(input) {
119 sass_vals <- mapply(
120 SIMPLIFY = FALSE,
121 rlang::names2(input),
122 input,
123 FUN = function(name, val) {
124 if (nchar(name) > 0) {
125 # if a name is provided...
126 collapse0("$", name, ": ", as_sass_(val), ";")
127 } else {
128 # if no name is provided
129 as_sass_(val)
130 }
131 }
132 )
133 collapse0(sass_vals)
134 }
135
136 as_sass_.font_collection <- function(input) {
137 if (isTRUE(input$default_flag)) {
138 paste(input$families, "!default")
139 } else {
140 input$families
141 }
142 }
143
144 as_sass_.sass_layer <- function(input) {
145 # concatinate all sass layer content in order
146 collapse0(
147 c(
148 # only collect non-null values
149 if (!is.null(input$functions)) as_sass_(input$functions),
150 if (!is.null(input$defaults)) as_sass_(input$defaults),
151 if (!is.null(input$mixins)) as_sass_(input$mixins),
152 if (!is.null(input$declarations)) as_sass_(input$declarations),
153 if (!is.null(input$rules)) as_sass_(input$rules)
154 )
155 )
156 }
157
158 as_sass_.sass_bundle <- function(input) {
159 if (length(input$layers) == 0) {
160 # if there are no layers, return nothing
161 return("")
162 }
163 as_sass_(as_sass_layer(input))
164 }
165
166 as_sass_.character <- function(input) {
167 if (has_any_name(input)) {
168 warning(
169 "Character vector names are ignored. ",
170 "Instead of a named character vector, use a named list to define Sass variables.",
171 call. = FALSE
172 )
173 }
174 paste(input, collapse = "\n")
175 }
176
177 #' Sass Import
178 #'
179 #' Create an import statement to be used within your Sass file. See
180 #' <https://sass-lang.com/documentation/at-rules/import> for
181 #' more details.
182 #'
183 #' `sass_file()` adds extra checks to make sure an appropriate file path
184 #' exists given the input value.
185 #'
186 #' Note that the LibSass compiler expects .sass files to use the Sass Indented
187 #' Syntax.
188 #'
189 #' @param input Character string to be placed in an import statement.
190 #' @param quote Logical that determines if a double quote is added to the import
191 #' value. Defaults to \code{TRUE}.
192 #' @return Fully defined Sass import string.
193 #' @rdname sass_import
194 #' @export
195 #' @examples
196 #' sass_import("foo")
197 #' sass_import("$foo", FALSE)
198 #' \donttest{
199 #' tmp_scss_file <- tempfile(fileext = ".scss")
200 #' writeLines("$color: red; body{ color: $color; }", tmp_scss_file)
201 #' sass_file(tmp_scss_file)
202 #' sass(sass_file(tmp_scss_file))
203 #' }
204 sass_import <- function(input, quote = TRUE) {
205 quote_val <- (if (isTRUE(quote)) "\"" else "")
206 if (isTRUE(quote)) {
207 input <- gsub("\\", "\\\\", input, fixed = TRUE)
208 input <- gsub('"', '\\"', input, fixed = TRUE)
209 input <- gsub("'", "\\'", input, fixed = TRUE)
210 }
211 paste0("@import ", quote_val, input, quote_val, ";")
212 }
213
214 #' @rdname sass_import
215 #' @export
216 sass_file <- function(input) {
217
218 if (!file.exists(input)) {
219 stop("Could not find file: '", input, "' in dir: ", getwd())
220 }
221
222 input <- normalizePath(input, mustWork = TRUE)
223 # is a file. return an @import statement
224 ret <- sass_import(input)
225
226 # Add metadata so we can stir the file mtime into a cache key on demand
227 class(ret) <- c("sass_file", class(ret))
228 attr(ret, "sass_file_path") <- input
229
230 ret
231 }