/
register.R
97 lines (88 loc) · 3 KB
/
register.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
#' Register main language in Quarto config
#'
#' @param main_language Main language code (character, like `"en"`)
#' @inheritParams render_book
#' @return Nothing
#' @export
register_main_language <- function(main_language = "en", project_path = ".") {
config_path <- file.path(project_path, "_quarto.yml")
config <- yaml::read_yaml(config_path)
if (!is.null(config[["babelquarto"]][["mainlanguage"]])) {
if (config[["babelquarto"]][["mainlanguage"]] != main_language) {
cli::cli_abort(
c(
"Can't register {main_language} as main language.",
'Main language registered as {config[["babelquarto"]][["mainlanguage"]]}.'
)
)
} else {
cli::cli_alert_info("Main language already registered.")
return(invisible())
}
}
config_lines <- brio::read_lines(config_path)
config_lines <- c(
config_lines,
"",
"babelquarto:",
" languagecodes:",
sprintf(" - name: %s", main_language),
sprintf(' text: "Version in %s"', main_language),
sprintf(" mainlanguage: '%s'", main_language),
sprintf("lang: %s", main_language)
)
brio::write_lines(config_lines, path = config_path)
}
#' Register further languages in Quarto config
#'
#' @param further_languages Languages to be registered (character vector)
#' @inheritParams render_book
#'
#' @return Nothing
#' @export
#'
register_further_languages <- function(further_languages, project_path = ".") {
config_path <- file.path(project_path, "_quarto.yml")
config <- yaml::read_yaml(config_path)
if (!is.null(config[["babelquarto"]][["languages"]])) {
if (all(further_languages %in% config[["babelquarto"]][["languages"]])) {
cli::cli_alert_info("All languages already registered.")
return(invisible())
}
}
if (is.null(config[["babelquarto"]][["mainlanguage"]])) {
cli::cli_abort(
c(
"Can't register further languages before a main language",
i = "Call {.fun register_main_language} first."
)
)
}
config_lines <- brio::read_lines(config_path)
config_lines <- config_lines[!grepl("languages\\:", config_lines)]
which_main <- which(grepl("mainlanguage\\:", trimws(config_lines)))
languages <- sprintf("'%s'",union(config[["babelquarto"]][["languages"]], further_languages))
config_lines <- append(
config_lines,
c(
sprintf(" languages: [%s]", toString(languages)),
purrr::map_chr(further_languages, ~sprintf("title-%s: title in %s", .x, .x)),
purrr::map_chr(further_languages, ~sprintf("description-%s: description in %s", .x, .x)),
purrr::map_chr(further_languages, ~sprintf("author-%s: author in %s", .x, .x))
),
after = which_main
)
which_codes <- which(trimws(config_lines) == "languagecodes:")
config_lines <- append(
config_lines,
unlist(purrr::map(
further_languages,
~list(
sprintf(" - name: %s", .x),
sprintf(' text: "Version in %s"', .x)
)
)),
after = which_codes
)
brio::write_lines(config_lines, path = config_path)
}