Debian Codegraph @

R/print.R

raw
1 #' Print a template
2 #'
3 #' Once a template has been parsed, it can be printed with color highlighting of
4 #' the templating blocks.
5 #'
6 #' @param x A parsed template (use [parse_template()]).
7 #' @param n Number of lines to show. If `Inf`, will print all lines. Default: `10`.
8 #' @inheritParams rlang::args_dots_empty
9 #' @examples
10 #' input <- '<!DOCTYPE html>
11 #' <html lang="en">
12 #' <head>
13 #' <title>{{ title }}</title>
14 #' </head>
15 #' <body>
16 #' <ul id="navigation">
17 #' {% for item in navigation -%}
18 #' <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
19 #' {% endfor -%}
20 #' </ul>
21 #' {# a comment #}
22 #' </body>
23 #' </html>'
24 #'
25 #' x <- parse_template(input)
26 #'
27 #' print(x)
28 #'
29 #' print(x, n = Inf)
30 #' @rdname print
31 #' @export
32 print.jinjar_template <- function(x, ..., n = 10) {
33 check_dots_empty()
34 check_count(n, inf = TRUE)
35 n_more <- 0L
36
37 # truncate output
38 if (is.finite(n)) {
39 lines <- strsplit(x, "\n", fixed = TRUE)[[1]]
40 n_found <- length(lines)
41
42 if (n_found > n) {
43 attrs <- attributes(x)
44 x <- paste0(lines[1:n], collapse = "\n")
45 attributes(x) <- attrs
46
47 n_more <- n_found - n
48 }
49 }
50
51 cli::cli_verbatim(style_template(x))
52 if (n_more > 0) {
53 dots <- cli::symbol$ellipsis
54 subtle <- cli::make_ansi_style("grey60")
55 cli::cli_alert_info(subtle("{dots} with {n_more} more line{?s}"))
56 }
57
58 invisible(x)
59 }
60
61 #' @importFrom utils head
62 style_template <- function(x) {
63 if (cli::num_ansi_colors() == 1) {
64 return(x)
65 }
66
67 config <- attr(x, "config")
68
69 # find spans
70 spans <- rbind(
71 find_spans(x, "block", config$block_open, config$block_close),
72 find_spans(x, "variable", config$variable_open, config$variable_close),
73 find_spans(x, "comment", config$comment_open, config$comment_close)
74 )
75 if (nchar(config$line_statement) > 0) {
76 spans <- rbind(spans, find_spans(x, "block", config$line_statement, "\n"))
77 }
78
79 # sort spans in order of appearance
80 spans <- spans[order(spans$ix_open),]
81
82 # remove span overlaps
83 latest_ix_close <- c(0, head(cummax(spans$ix_close), -1))
84 spans <- spans[spans$ix_close > latest_ix_close,]
85
86 # gaps are text spans
87 text_spans <- data.frame(
88 type = "text",
89 ix_open = c(1, spans$ix_close + 1),
90 ix_close = c(spans$ix_open - 1, nchar(x))
91 )
92 text_spans <- text_spans[text_spans$ix_open <= text_spans$ix_close,]
93
94 # add text spans and sort again
95 spans <- rbind(spans, text_spans)
96 spans <- spans[order(spans$ix_open),]
97
98 # style spans
99 output <- mapply(style_span, x, spans$type, spans$ix_open, spans$ix_close)
100
101 # stitch spans
102 paste0(output, collapse = "")
103 }
104
105 find_spans <- function(x, type, open, close) {
106 ix_open <- gregexpr(open, x, fixed = TRUE)[[1]]
107 ix_close <- gregexpr(close, x, fixed = TRUE)[[1]]
108
109 # no spans found
110 if (all(ix_open == -1L)) {
111 return(data.frame(type = character(), open = integer(), close = integer()))
112 }
113
114 # ignore unmatched open/close patterns
115 find_matching_close <- function(x) min(ix_close[ix_close > x])
116 ix_close_match <- vapply(ix_open, find_matching_close, FUN.VALUE = 0L)
117
118 ix_open <- ix_open[c(1, diff(ix_close_match)) != 0]
119 ix_close <- ix_close[ix_close %in% ix_close_match]
120 ix_close <- ix_close + nchar(close) - 1
121
122 data.frame(type, ix_open, ix_close)
123 }
124
125 style_span <- function(x, type, ix_open, ix_close) {
126 txt_span <- substr(x, ix_open, ix_close)
127
128 if (type == "comment") {
129 style_comment(txt_span)
130 } else if (type == "block") {
131 style_block(txt_span)
132 } else if (type == "variable") {
133 style_variable(txt_span)
134 } else {
135 txt_span
136 }
137 }
138
139 style_comment <- function(x) cli::style_italic(cli::col_grey(x))
140 style_block <- cli::col_blue
141 style_variable <- cli::col_green