Debian Codegraph @

R/config.R

raw
1 #' Configure the templating engine
2 #'
3 #' Create an object to configure the templating engine behavior (e.g. customize
4 #' the syntax). The default values have been chosen to match the Jinja defaults.
5 #'
6 #' @note The equivalent Jinja class is `Environment`, but this term has special
7 #' significance in R (see [environment()]).
8 #'
9 #' @param loader How the engine discovers templates. Choices:
10 #' * `NULL` (default), disables search for templates.
11 #' * Path to template directory.
12 #' * A [`loader`] object.
13 #' @param block_open,block_close The opening and closing delimiters
14 #' for control blocks. Default: \verb{"\{\%"} and \verb{"\%\}"}.
15 #' @param variable_open,variable_close The opening and closing delimiters
16 #' for print statements. Default: `"{{"` and `"}}"`.
17 #' @param comment_open,comment_close The opening and closing delimiters
18 #' for comments. Default: `"{#"` and `"#}"`.
19 #' @param line_statement The prefix for an inline statement. If `NULL` (the
20 #' default), inline statements are disabled.
21 #' @param trim_blocks Remove first newline after a block. Default: `FALSE`.
22 #' @param lstrip_blocks Remove inline whitespace before a block. Default: `FALSE`.
23 #' @param ignore_missing_files Ignore `include` or `extends` statements when
24 #' the auxiliary template cannot be found. If `FALSE` (default), then an error
25 #' is raised.
26 #' @return A `"jinjar_config"` object.
27 #'
28 #' @examples
29 #' jinjar_config()
30 #' @export
31 jinjar_config <- function(loader = NULL,
32 block_open = "{%",
33 block_close = "%}",
34 variable_open = "{{",
35 variable_close = "}}",
36 comment_open = "{#",
37 comment_close = "#}",
38 line_statement = NULL,
39 trim_blocks = FALSE,
40 lstrip_blocks = FALSE,
41 ignore_missing_files = FALSE) {
42
43 if (!(is_null(loader) || is_string(loader) || inherits(loader, "jinjar_loader"))) {
44 cli::cli_abort("{.arg loader} must be NULL, a path, or a loader object", arg = "loader")
45 }
46 if (is.character(loader)) {
47 loader <- path_loader(loader)
48 }
49
50 check_string(block_open)
51 check_string(block_close)
52 check_string(variable_open)
53 check_string(variable_close)
54 check_string(comment_open)
55 check_string(comment_close)
56 check_string(line_statement %||% "")
57 check_bool(trim_blocks)
58 check_bool(lstrip_blocks)
59 check_bool(ignore_missing_files)
60
61 delimiters <- c(
62 variable_open = variable_open,
63 variable_close = variable_close,
64 block_open = block_open,
65 block_close = block_close,
66 line_statement = line_statement %||% "",
67 comment_open = comment_open,
68 comment_close = comment_close
69 )
70 if (anyDuplicated(delimiters)) {
71 conflicts <- delimiters[duplicated(delimiters) | duplicated(delimiters, fromLast = TRUE)]
72 cli::cli_abort("Conflicting delimiters: {.arg {names(conflicts)}}")
73 }
74
75 structure(c(as.list(delimiters), list(
76 loader = loader,
77 trim_blocks = trim_blocks,
78 lstrip_blocks = lstrip_blocks,
79 ignore_missing_files = ignore_missing_files
80 )), class = "jinjar_config")
81 }
82
83 #' @export
84 print.jinjar_config <- function(x, ...) {
85 cli::cli({
86 cli::cli_h1("Template configuration")
87
88 if (is.null(x$loader)) {
89 cli::cli_text("{.strong Loader:} disabled")
90 } else {
91 print(x$loader)
92 }
93
94 cli::cli_text(
95 "{.strong Syntax:} ",
96 style_block("{x$block_open} block {x$block_close}"), " ",
97 style_variable("{x$variable_open} variable {x$variable_close}"), " ",
98 style_comment("{x$comment_open} comment {x$comment_close}")
99 )
100 })
101
102 invisible(x)
103 }
104
105 #' @rdname jinjar_config
106 #' @export
107 default_config <- function() {
108 config <- getOption("jinjar.default_config")
109 if (is.null(config)) {
110 config <- jinjar_config()
111 options("jinjar.default_config" = config)
112 }
113
114 config
115 }