File: files.R

package info (click to toggle)
r-cran-bslib 0.4.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,332 kB
  • sloc: javascript: 10,075; makefile: 30; sh: 23
file content (69 lines) | stat: -rw-r--r-- 1,933 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
bs3_sass_files <- function(x) {
  bs_sass_files(x, version = 3)
}

bs4_sass_files <- function(x) {
  bs_sass_files(x, version = 4)
}

bs5_sass_files <- function(x) {
  bs_sass_files(x, version = 5)
}

bs_sass_files <- function(files, version) {
  lapply(files, bs_sass_file, version = version)
}

# Search for one file at a time so we can throw informative errors
bs_sass_file <- function(file, version) {
  if (length(file) != 1) stop("file should be of length 1")

  file <- file.path(
    dirname(file),
    paste0("_", basename(file), ".scss")
  )

  f <- switch_version(
    version,
    five = lib_file("bs5", "scss", file),
    four = lib_file("bs4", "scss", file),
    three = lib_file("bs3", "assets", "stylesheets", "bootstrap", file)
  )
  if (f == "") stop("The bootstrap stylesheet '", file, "' doesn't exist.", call. = FALSE)
  sass_file(f)
}

bootswatch_sass_file <- function(theme, file, version = version_default()) {
  if (length(file) > 1) stop("file should be of length 1")
  theme <- match.arg(theme, bootswatch_themes(version))
  file <- paste0("_", file, ".scss")
  f <- file.path(bootswatch_dist(version), theme, file)
  if (file.exists(f)) return(sass_file(f))
  stop("Bootswatch file '", file, "' doesn't exist for theme '", theme, "'.", call. = FALSE)
}


# Given a vector of sass_file()s, create a list of sass_bundles(),
# so each rule may be removed layer (by it's files basename)
rule_bundles <- function(files) {
  files <- lapply(files, as_sass_file)
  paths <- vapply(files, get_sass_file_path, character(1))
  nms <- tools::file_path_sans_ext(basename(paths))
  Map(
    nms, files,
    f = function(nm, f) {
      sass_bundle(!!nm := sass_layer(rules = f))
    }
  )
}

get_sass_file_path <- function(x) {
  path <- attr(x, "sass_file_path")
  if (length(path)) return(path)

  stop("Couldn't find file path")
}

as_sass_file <- function(x) {
  if (inherits(x, "sass_file")) x else sass_file(x)
}