| 1 |
|
| 2 |
renv_knitr_options_header <- function(text, type) { |
| 3 |
|
| 4 |
# extract the inner options from the header |
| 5 |
patterns <- renv_knitr_patterns() |
| 6 |
rest <- sub(patterns[[type]]$chunk.begin, "\\1", text) |
| 7 |
|
| 8 |
# if this is an R Markdown document, parse the initial engine chunk |
| 9 |
# (default to 'r' when not set) |
| 10 |
engine <- "r" |
| 11 |
if (type == "md") { |
| 12 |
idx <- regexpr("(?:[ ,]|$)", rest) |
| 13 |
engine <- substring(rest, 1, idx - 1) |
| 14 |
rest <- sub("^,*\\s*", "", substring(rest, idx + 1)) |
| 15 |
} |
| 16 |
|
| 17 |
# parse the params |
| 18 |
params <- renv_knitr_options_header_impl(rest) |
| 19 |
|
| 20 |
# ensure an engine is set, if any |
| 21 |
params[["engine"]] <- params[["engine"]] %||% engine |
| 22 |
|
| 23 |
# return parsed params |
| 24 |
params |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
renv_knitr_options_header_impl <- function(rest) { |
| 29 |
|
| 30 |
# extract an unquoted label |
| 31 |
label <- "" |
| 32 |
pattern <- "(^\\s*[^=]+)(,|\\s*$)" |
| 33 |
matches <- regexec(pattern, rest)[[1]] |
| 34 |
if (!identical(c(matches), -1L)) { |
| 35 |
submatches <- regmatches(rest, list(matches))[[1]] |
| 36 |
label <- trimws(submatches[[2L]]) |
| 37 |
rest <- substring(rest, matches[[3L]] + 1L) |
| 38 |
} |
| 39 |
|
| 40 |
# parse as alist |
| 41 |
params <- catch(parse(text = sprintf("alist(%s)", rest))[[1]]) |
| 42 |
if (inherits(params, "error")) |
| 43 |
return(list()) |
| 44 |
|
| 45 |
# insert the label back in |
| 46 |
names(params) <- names(params) %||% rep.int("", length(params)) |
| 47 |
if (length(params) > 1L && identical(names(params)[[2L]], "")) |
| 48 |
names(params)[[2L]] <- "label" |
| 49 |
|
| 50 |
# fix up 'label' if it's a missing value |
| 51 |
if (identical(params[["label"]], quote(expr = ))) |
| 52 |
params[["label"]] <- NULL |
| 53 |
|
| 54 |
# if we parsed a label, add it in |
| 55 |
if (is.null(params[["label"]]) && nzchar(label)) |
| 56 |
params[["label"]] <- label |
| 57 |
|
| 58 |
# evaluate the alist |
| 59 |
eval(params, envir = parent.frame()) |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
renv_knitr_options_chunk <- function(code) { |
| 64 |
|
| 65 |
# find chunk option lines |
| 66 |
pattern <- "^[[:space:]]*#+[|]" |
| 67 |
matches <- grep(pattern, code[nzchar(code)], value = TRUE) |
| 68 |
|
| 69 |
# remove prefix |
| 70 |
text <- gsub(pattern, "", matches) |
| 71 |
|
| 72 |
# try to guess whether it's YAML |
| 73 |
isyaml <- any(grepl("^[[:space:]]*[^[:space:]:]+:", text)) |
| 74 |
|
| 75 |
# first, try to parse as YAML, then as R code |
| 76 |
params <- if (isyaml) { |
| 77 |
|
| 78 |
# validate that we actually have the yaml package available |
| 79 |
if (!renv_dependencies_require("yaml")) |
| 80 |
return(list()) |
| 81 |
|
| 82 |
catch(renv_yaml_load(text)) |
| 83 |
|
| 84 |
} else { |
| 85 |
code <- paste(text, collapse = ", ") |
| 86 |
catch(renv_knitr_options_header_impl(code)) |
| 87 |
} |
| 88 |
|
| 89 |
# check for error and report if this is in dependency discovery |
| 90 |
if (inherits(params, "error")) { |
| 91 |
|
| 92 |
state <- renv_dependencies_state() |
| 93 |
if (!is.null(state)) { |
| 94 |
problem <- list(file = state$path %||% "<unknown>", error = params) |
| 95 |
state$problems$push(problem) |
| 96 |
} |
| 97 |
|
| 98 |
return(list()) |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
# return parsed params |
| 103 |
params |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
renv_knitr_patterns <- function() { |
| 108 |
|
| 109 |
list( |
| 110 |
|
| 111 |
rnw = list( |
| 112 |
chunk.begin = "^\\s*<<(.*)>>=.*$", |
| 113 |
chunk.end = "^\\s*@\\s*(%+.*|)$", |
| 114 |
inline.code = "\\\\Sexpr\\{([^}]+)\\}", |
| 115 |
inline.comment = "^\\s*%.*", |
| 116 |
ref.chunk = "^\\s*<<(.+)>>\\s*$", |
| 117 |
header.begin = "(^|\n)\\s*\\\\documentclass[^}]+\\}", |
| 118 |
document.begin = "\\s*\\\\begin\\{document\\}" |
| 119 |
), |
| 120 |
|
| 121 |
tex = list( |
| 122 |
chunk.begin = "^\\s*%+\\s*begin.rcode\\s*(.*)", |
| 123 |
chunk.end = "^\\s*%+\\s*end.rcode", |
| 124 |
chunk.code = "^\\s*%+", |
| 125 |
ref.chunk = "^%+\\s*<<(.+)>>\\s*$", |
| 126 |
inline.comment = "^\\s*%.*", |
| 127 |
inline.code = "\\\\rinline\\{([^}]+)\\}", |
| 128 |
header.begin = "(^|\n)\\s*\\\\documentclass[^}]+\\}", |
| 129 |
document.begin = "\\s*\\\\begin\\{document\\}" |
| 130 |
), |
| 131 |
|
| 132 |
html = list( |
| 133 |
chunk.begin = "^\\s*<!--\\s*begin.rcode\\s*(.*)", |
| 134 |
chunk.end = "^\\s*end.rcode\\s*-->", |
| 135 |
ref.chunk = "^\\s*<<(.+)>>\\s*$", |
| 136 |
inline.code = "<!--\\s*rinline(.+?)-->", |
| 137 |
header.begin = "\\s*<head>" |
| 138 |
), |
| 139 |
|
| 140 |
md = list( |
| 141 |
chunk.begin = "^[\t >]*```+\\s*\\{([a-zA-Z0-9_]+( *[ ,].*)?)\\}\\s*$", |
| 142 |
chunk.end = "^[\t >]*```+\\s*$", |
| 143 |
ref.chunk = "^\\s*<<(.+)>>\\s*$", |
| 144 |
inline.code = "(?<!(^|\n)``)`r[ #]([^`]+)\\s*`" |
| 145 |
), |
| 146 |
|
| 147 |
rst = list( |
| 148 |
chunk.begin = "^\\s*[.][.]\\s+\\{r(.*)\\}\\s*$", |
| 149 |
chunk.end = "^\\s*[.][.]\\s+[.][.]\\s*$", |
| 150 |
chunk.code = "^\\s*[.][.]", |
| 151 |
ref.chunk = "^\\.*\\s*<<(.+)>>\\s*$", |
| 152 |
inline.code = ":r:`([^`]+)`" |
| 153 |
), |
| 154 |
|
| 155 |
asciidoc = list( |
| 156 |
chunk.begin = "^//\\s*begin[.]rcode(.*)$", |
| 157 |
chunk.end = "^//\\s*end[.]rcode\\s*$", |
| 158 |
chunk.code = "^//+", |
| 159 |
ref.chunk = "^\\s*<<(.+)>>\\s*$", |
| 160 |
inline.code = "`r +([^`]+)\\s*`|[+]r +([^+]+)\\s*[+]", |
| 161 |
inline.comment = "^//.*" |
| 162 |
), |
| 163 |
|
| 164 |
textile = list( |
| 165 |
chunk.begin = "^###[.]\\s+begin[.]rcode(.*)$", |
| 166 |
chunk.end = "^###[.]\\s+end[.]rcode\\s*$", |
| 167 |
ref.chunk = "^\\s*<<(.+)>>\\s*$", |
| 168 |
inline.code = "@r +([^@]+)\\s*@", |
| 169 |
inline.comment = "^###[.].*" |
| 170 |
) |
| 171 |
|
| 172 |
) |
| 173 |
|
| 174 |
} |