| 1 |
|
| 2 |
cat0 <- function(...) { |
| 3 |
cat(..., sep = "") |
| 4 |
} |
| 5 |
|
| 6 |
#' @export |
| 7 |
#' @noRd |
| 8 |
format.css <- function(x, ...) { |
| 9 |
as.character(x) |
| 10 |
} |
| 11 |
#' @export |
| 12 |
#' @noRd |
| 13 |
format.sass <- function(x, ...) { |
| 14 |
as.character(x) |
| 15 |
} |
| 16 |
#' @export |
| 17 |
#' @noRd |
| 18 |
format.sass_layer <- function(x, ...) { |
| 19 |
format(as_sass(x)) |
| 20 |
} |
| 21 |
#' @export |
| 22 |
#' @noRd |
| 23 |
format.sass_bundle <- function(x, ...) { |
| 24 |
format(as_sass(x)) |
| 25 |
} |
| 26 |
|
| 27 |
#' @export |
| 28 |
#' @noRd |
| 29 |
print.css <- function(x, ...) { |
| 30 |
cat0("/* CSS */\n", format(x), "\n") # nolint |
| 31 |
deps <- htmlDependencies(x) |
| 32 |
if (length(deps)) { |
| 33 |
cat0("\n/* HTML Dependencies */\n") |
| 34 |
utils::str(deps) |
| 35 |
} |
| 36 |
invisible(x) |
| 37 |
} |
| 38 |
#' @export |
| 39 |
#' @noRd |
| 40 |
print.sass <- function(x, ...) { |
| 41 |
cat0("/* Sass */\n", format(x), "\n") # nolint |
| 42 |
deps <- htmlDependencies(x) |
| 43 |
if (length(deps)) { |
| 44 |
cat0("\n/* HTML Dependencies */\n") |
| 45 |
utils::str(deps) |
| 46 |
} |
| 47 |
invisible(x) |
| 48 |
} |
| 49 |
#' @export |
| 50 |
#' @noRd |
| 51 |
print.sass_layer <- function( |
| 52 |
x, ..., |
| 53 |
# currently only used by `print.sass_bundle` |
| 54 |
header_name = "Sass Layer", info_name = "Sass Layer" |
| 55 |
) { |
| 56 |
x_fmt <- format(x) |
| 57 |
if (nchar(x_fmt) == 0) x_fmt <- "/* (empty) */" |
| 58 |
cat0( |
| 59 |
"/* ", header_name, " */\n", |
| 60 |
x_fmt, "\n", |
| 61 |
"/* *** */\n" |
| 62 |
) |
| 63 |
|
| 64 |
x_other <- x[c("html_deps", "file_attachments")] |
| 65 |
if (length(unlist(x_other)) > 0) { |
| 66 |
cat0("\nOther ", info_name, " information:\n") |
| 67 |
utils::str(x_other) |
| 68 |
} |
| 69 |
invisible(x) |
| 70 |
} |
| 71 |
#' @export |
| 72 |
#' @noRd |
| 73 |
print.sass_bundle <- function(x, ..., name = NULL) { |
| 74 |
if (length(x$layers) == 0) { |
| 75 |
cat0("/* Sass Bundle: (empty) *** */\n") |
| 76 |
return(invisible(x)) |
| 77 |
} |
| 78 |
|
| 79 |
named_layers <- setdiff(unique(names2(x$layers)), "") |
| 80 |
named_layer_txt <- |
| 81 |
if (length(named_layers) > 0) { |
| 82 |
paste0(": ", paste0(named_layers, collapse = ", ")) |
| 83 |
} else { |
| 84 |
NULL |
| 85 |
} |
| 86 |
print( |
| 87 |
as_sass_layer(x), |
| 88 |
header_name = paste0("Sass Bundle", named_layer_txt), |
| 89 |
info_name = "Sass Bundle" |
| 90 |
) |
| 91 |
invisible(x) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
knit_print.css <- function(x, options, ...) { |
| 96 |
# Display the CSS as a string |
| 97 |
if (identical(options$class.output, 'css')) { |
| 98 |
return(as.character(x)) |
| 99 |
} |
| 100 |
# Embed the CSS as HTML |
| 101 |
knitr::knit_print(htmltools::tags$style(x)) |
| 102 |
} |
| 103 |
|
| 104 |
knit_print.sass <- function(x, options, ...) { |
| 105 |
if (isTRUE(options$class.output %in% c('scss', 'sass'))) { |
| 106 |
return(as.character(x)) |
| 107 |
} |
| 108 |
knit_print.css(sass(x), options, ...) |
| 109 |
} |
| 110 |
|
| 111 |
# Reusable function for registering a set of methods with S3 manually. The |
| 112 |
# methods argument is a list of character vectors, each of which has the form |
| 113 |
# c(package, genname, class). |
| 114 |
registerMethods <- function(methods) { |
| 115 |
lapply(methods, function(method) { |
| 116 |
pkg <- method[[1]] |
| 117 |
generic <- method[[2]] |
| 118 |
class <- method[[3]] |
| 119 |
func <- get(paste(generic, class, sep=".")) |
| 120 |
if (pkg %in% loadedNamespaces()) { |
| 121 |
registerS3method(generic, class, func, envir = asNamespace(pkg)) |
| 122 |
} |
| 123 |
setHook( |
| 124 |
packageEvent(pkg, "onLoad"), |
| 125 |
function(...) { |
| 126 |
registerS3method(generic, class, func, envir = asNamespace(pkg)) |
| 127 |
} |
| 128 |
) |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
.onLoad <- function(...) { |
| 133 |
# htmltools provides methods for knitr::knit_print, but knitr isn't a Depends or |
| 134 |
# Imports of htmltools, only an Enhances. Therefore, the NAMESPACE file has to |
| 135 |
# declare it as an export, not an S3method. That means that R will only know to |
| 136 |
# use our methods if htmltools is actually attached, i.e., you have to use |
| 137 |
# library(htmltools) in a knitr document or else you'll get escaped HTML in your |
| 138 |
# document. This code snippet manually registers our methods with S3 once both |
| 139 |
# htmltools and knitr are loaded. |
| 140 |
registerMethods(list( |
| 141 |
# c(package, genname, class) |
| 142 |
c("knitr", "knit_print", "css"), |
| 143 |
c("knitr", "knit_print", "sass") |
| 144 |
)) |
| 145 |
} |