/
config.R
163 lines (154 loc) · 5.04 KB
/
config.R
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#' Gallery site configuration
#'
#' Site configuration for the [gallery_site()] generator.
#'
#' @inheritParams rmarkdown::site_config
#'
#' @return The function returns the contents of `_site.yml` as an \R list, with
#' an additional element `$gallery$meta`, a list containing the metadata of
#' the pages to be generated, as read from the `.json`, `.yml` and `yaml`
#' files, where `$gallery$type_field` and `gallery$type_template` (if present)
#' have been already used to lookup the actual `template`. In addition,
#' default field values specified as `gallery$defaults` are also applied. The
#' metadata in `$gallery$meta` also include an additional field `page_name`
#' containing the names of the metadata list itself, serving as names for the
#' HTML pages that will be generated.
#'
#' @export
gallery_site_config <- function(input = ".") {
config <- rmarkdown::site_config(input)
if (!is.null(config$gallery)) {
meta_dir <- config$gallery$meta_dir %||% "meta"
single_meta <- config$gallery$single_meta %||% FALSE
meta_files <- site_meta_files(file.path(input, meta_dir))
meta <- read_meta(meta_files, single_meta)
meta <- with_name_field(meta, "page_name")
meta <- with_type_template(meta, config$gallery)
meta <- with_defaults(meta, config$gallery)
meta <- sort_meta(meta, config$gallery$order_by %||% "page_name")
check_missing_template(meta)
config$gallery$meta <- meta
config$gallery <- with_includes(config$gallery, input)
}
config
}
with_type_template <- function(meta, gallery_config) {
if (!is.null(gallery_config$type_field)) {
meta <- assign_type_template(
meta,
gallery_config$type_field,
gallery_config$type_template
)
}
meta
}
assign_type_template <- function(meta, type_field, type_templates) {
template <- get_meta_field(meta, "template")
type <- get_meta_field(meta, type_field)
with_type <- is.na(template) & !is.na(type)
template_from_type <- get_type_template(type[with_type], type_templates)
miss_template <- is.na(template_from_type)
if (any(miss_template)) {
stop(
"Missing template specification for custom type(s) ",
toQuotedString(unique(type[with_type][miss_template]))
)
}
meta[with_type] <- set_meta_field(
meta[with_type],
"template", template_from_type
)
meta
}
get_type_template <- function(type, type_templates) {
vapply(
type, FUN.VALUE = NA_character_,
function(x) type_templates[[x]] %||% NA_character_
)
}
with_defaults <- function(meta, gallery_config) {
default_fields <- names(gallery_config$defaults)
for (field in default_fields) {
values <- get_meta_field(meta, field)
values <- values %|NA|% gallery_config$defaults[[field]]
meta <- set_meta_field(meta, field, values)
}
meta
}
check_missing_template <- function(meta) {
miss_template <- is.na(get_meta_field(meta, "template"))
if (any(miss_template)) {
stop(
"Missing template specification for: ",
toQuotedString(names(meta)[miss_template])
)
}
invisible(meta)
}
with_includes <- function(gallery_config, path = ".") {
includes <- intersect(
c("include_before", "include_after"),
names(gallery_config)
)
gallery_config[includes] <- lapply(
gallery_config[includes], function(include) {
include_file <- file.path(path, include)
if (!file.exists(include_file)) {
.Deprecated(msg = paste(
"Inline definition of `include_before` and `include_after` is deprecated.",
"They should define the path to a file with the content to be included."
))
include
} else {
paste(readLines(include_file), collapse = "\n")
}
}
)
gallery_config
}
navbar_menu_entry <- function(meta) {
entry <- meta$menu_entry
if (!is.list(entry)) {
entry <- list()
entry$text = meta$menu_entry
entry$icon = meta$menu_icon
}
if (length(entry) > 0L) {
stopifnot(length(meta$page_name) == 1L)
entry$href <- file_with_ext(meta$page_name, "html")
entry
} else {
NULL
}
}
gallery_navbar_menu <- function(meta) {
gallery_entries <- lapply(meta, navbar_menu_entry)
names(gallery_entries) <- NULL
has_entry <- sapply(gallery_entries, length) != 0L
gallery_entries <- gallery_entries[has_entry]
gallery_keys <- vapply(gallery_entries, FUN.VALUE = "", function(x) {
paste(c(x$icon, x$text), collapse = " ")
})
duplicated <- duplicated(gallery_keys)
if (any(duplicated)) {
stop(
"Found duplicate navbar menu entries: ",
toQuotedString(gallery_keys[duplicated]))
}
gallery_entries
}
navbar_with_gallery <- function(config) {
gallery_navbar <- config$gallery$navbar
navbar <- config$navbar
if (!is.null(gallery_navbar)) {
# must have one element, which is the location, e.g. $left
stopifnot(length(gallery_navbar) == 1L)
gallery_navbar[[1L]][[1L]]$menu <- gallery_navbar_menu(config$gallery$meta)
# add to the user-defined navbar from the main site configuration
navbar[[names(gallery_navbar)]] <- c(
navbar[[names(gallery_navbar)]],
gallery_navbar[[1]]
)
}
navbar
}