Skip to content

Commit

Permalink
Merge pull request #290 from carpentries/yaml-options
Browse files Browse the repository at this point in the history
allow extra yaml items to be preserved in config and passed to varnish
  • Loading branch information
zkamvar authored May 23, 2022
2 parents 6c4f83c + aaa64d0 commit 38308ec
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sandpaper
Title: Create and Curate Carpentries Lessons
Version: 0.5.4
Version: 0.5.5
Authors@R: c(
person(given = "Zhian N.",
family = "Kamvar",
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# sandpaper 0.5.5

MISC
----

* New YAML items can now be added at-will and will be available to varnish in
a `{{#yaml}}` context.
* internal function `set_dropdown()` will preserve the yaml items that are not
explicitly coded in the config menu.

# sandpaper 0.5.4

BUG FIX
Expand Down
3 changes: 3 additions & 0 deletions R/set_dropdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ set_dropdown <- function(path = ".", order = NULL, write = FALSE, folder) {
error_missing_config(order, real_files, folder)
}
yaml <- quote_config_items(get_config(path))

# account for extra items not yet known to our config
yaml$custom_items <- yaml_list(yaml[!names(yaml) %in% known_yaml_items])
sched <- yaml[[folder]]
sched <- if (is.null(sched) && folder == "episodes") yaml[["schedule"]] else sched
sched_folders <- c("episodes", "learners", "instructors", "profiles")
Expand Down
6 changes: 4 additions & 2 deletions R/template.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ generate_template_function <- function(f) {
#nocov end

# copy template on disk
copy_template <- function(template, path, name, values = NULL) {
copy_template <- function(template, path = NULL, name = NULL, values = NULL) {
template <- eval(parse(text = paste0("template_", template, "()")))
out <- if (is.null(path)) NULL else fs::path(path, name)
if (!is.null(values)) {
temp <- readLines(template, encoding = "UTF-8")
writeLines(whisker::whisker.render(temp, values), fs::path(path, name))
res <- whisker::whisker.render(template = temp, data = values)
if (length(out)) writeLines(res, out) else return(out)
} else {
fs::file_copy(template, new_path = fs::path(path, name))
}
Expand Down
37 changes: 29 additions & 8 deletions R/utils-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ create_pkgdown_yaml <- function(path) {
NULL
)
)
structure(yaml::yaml.load(yaml, eval.expr = FALSE), header = get_information_header(yaml))
rendered <- yaml::yaml.load(yaml, eval.expr = FALSE)
items <- names(rendered$template$params)
rendered$template$params <- c(rendered$template$params,
c(usr[!names(usr) %in% items]))
structure(rendered, header = get_information_header(yaml))
}

update_site_timestamp <- function(path) {
Expand All @@ -205,14 +209,31 @@ get_navbar_info <- function(i) {
)
}

known_yaml_keys <- c(
"title",
"carpentry",
"life_cycle",
"license",
"source",
"branch",
"contact",
"created",
"keywords"
)

known_yaml_items <- c(
known_yaml_keys,
"episodes",
"instructors",
"learners",
"profiles"
)


quote_config_items <- function(yaml) {
yaml$title <- siQuote(yaml$title)
yaml$carpentry <- siQuote(yaml$carpentry)
yaml$life_cycle <- siQuote(yaml$life_cycle)
yaml$license <- siQuote(yaml$license)
yaml$source <- siQuote(yaml$source)
yaml$branch <- siQuote(yaml$branch)
yaml$contact <- siQuote(yaml$contact)
for (i in known_yaml_keys) {
yaml[[i]] <- siQuote(yaml[[i]])
}
yaml
}

7 changes: 7 additions & 0 deletions inst/templates/config-template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ instructors: {{ instructors }}
# Learner Profiles
profiles: {{ profiles }}

# Customisation ---------------------------------------------
#
# This space below is where custom yaml items (e.g. pinning
# sandpaper and varnish versions) should live

{{{ custom_items }}}

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/set_dropdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
→ title: Lesson Title -> title: 'test: title'
→ license: CC-BY 4.0 -> license: 'CC0'

# custom keys can be modified by set_config()

Code
set_config(c(`test-key` = "!yeh"), path = tmp, write = TRUE)
Message
i Writing to '[redacted]/lesson-example/config.yaml'
> test-key: 'hey!' -> test-key: '!yeh'

# set_episodes() will display the modifications if write is not specified [plain]

Code
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/_snaps/utils-yaml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# yaml_list() processes nested lists


b:
- a
- b
- c
a:
hello:
- a
- b
- c
jello:
c: a
b: b
a: c

16 changes: 16 additions & 0 deletions tests/testthat/test-set_dropdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ cli::test_that_cli("set_config() will write items", {
transform = function(s) mask_tmpdir(s, dirname(tmp)))
})



test_that("custom keys can be modified by set_config()", {
writeLines(c("test-key: 'hey!'", readLines(fs::path(tmp, "config.yaml"))),
fs::path(tmp, "config.yaml"))
expect_equal(get_config(tmp)[["test-key"]], "hey!")
expect_snapshot(set_config(c("test-key" = "!yeh"), path = tmp, write = TRUE),
transform = function(s) mask_tmpdir(s, dirname(tmp)))
expect_equal(get_config(tmp)[["test-key"]], "!yeh")
expect_equal(readLines(fs::path(tmp, "config.yaml"), n = 1L), "test-key: '!yeh'")
})

test_that("schedule is empty by default", {

cfg <- get_config(tmp)
Expand All @@ -42,6 +54,10 @@ test_that("schedule is empty by default", {
no_episodes <- names(cfg)[names(cfg) != "episodes"]
expect_equal(cfg[no_episodes], get_config(tmp)[no_episodes])

# the config file now has the `test-key` at the bottom
cfgtxt <- readLines(fs::path(tmp, "config.yaml"))
expect_equal(cfgtxt[length(cfgtxt) - 1L], "test-key: '!yeh'")

})


Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-utils-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@ This is not poetry
expect_named(YML, c("a", "b"))

})


test_that("yaml_list() processes nested lists", {

x <- letters[1:3]
nester <- list(b = x, a = list(hello = x, jello = as.list(setNames(x, rev(x)))))
expect_snapshot_output(writeLines(yaml_list(nester)))
})

0 comments on commit 38308ec

Please sign in to comment.