Skip to content

Commit d2d45ac

Browse files
committed
add an argument strict divide_chunk() to disallow falling back to using #| when # is not the comment char of the engine (yihui/knitr#2225), and also add argument ... to make it possible to not use the yaml package when parsing YAML options (i.e., divide_chunk(use_yaml = FALSE))
1 parent e2197a9 commit d2d45ac

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: xfun
22
Type: Package
33
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
4-
Version: 0.51.2
4+
Version: 0.51.3
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
77
person("Wush", "Wu", role = "ctb"),

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Fixed a bug in `taml_load()` that would convert character values in arrays to lowercase.
44

5+
- Added arguments `strict` and `...` to `divide_chunk()` (thanks, @cderv, yihui/knitr#2225).
6+
57
# CHANGES IN xfun VERSION 0.51
68

79
- `tojson()` supports more types of data now, and will indent sub-elements for lists. See the help page [`?xfun::tojson`](https://git.yihui.org/xfun/manual.html#sec:man-tojson) for details.

R/knitr.R

+13-4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ get_option_comment = function(engine) {
9393
#' @param engine The name of the language engine (to determine the appropriate
9494
#' comment character).
9595
#' @param code A character vector (lines of code).
96+
#' @param strict If `FALSE`, allow chunk options to be written after `#|` even
97+
#' if `#` is not the comment character of the engine (e.g., when `engine =
98+
#' 'js'`), otherwise throw an error if `#|` is detected but `#` is not the
99+
#' comment character.
100+
#' @param ... Arguments to be passed to [yaml_load()].
96101
#' @return A list with the following items:
97102
#'
98103
#' - `options`: The parsed options (if there are any) as a list.
@@ -113,7 +118,7 @@ get_option_comment = function(engine) {
113118
#' csv_like = c("#| mine, echo = TRUE, fig.width = 8, foo = 'bar'", "1 + 1")
114119
#' writeLines(csv_like)
115120
#' xfun::divide_chunk("r", csv_like)
116-
divide_chunk = function(engine, code) {
121+
divide_chunk = function(engine, code, strict = FALSE, ...) {
117122
res = list(options = NULL, src = NULL, code = code)
118123
# mask out empty blocks
119124
if (length(code) == 0) return(res)
@@ -125,9 +130,13 @@ divide_chunk = function(engine, code) {
125130
# check for option comments
126131
i1 = startsWith(code, s1)
127132
# if "commentChar| " is not found, try "#| " instead
128-
if (!i1[1] && s1 != '#|') {
133+
if (!i1[1] && s1 != '#| ') {
134+
i1 = startsWith(code, '#| ')
135+
if (strict && i1[1]) stop2(
136+
"The chunk options should start with '", s1, "' instead of '#| '",
137+
if (s2 != '') c(", and end with '", s2, "'"), '.'
138+
)
129139
s1 = '#| '; s2 = ''
130-
i1 = startsWith(code, s1)
131140
}
132141
# must have at least one matched line at the beginning
133142
if (!i1[[1]]) return(res)
@@ -150,7 +159,7 @@ divide_chunk = function(engine, code) {
150159
meta = substr(src, c1, c2)
151160
# see if the metadata looks like YAML or CSV
152161
if (grepl('^[^ :]+:($|\\s)', meta[1])) {
153-
meta = yaml_load(meta, envir = FALSE)
162+
meta = yaml_load(meta, envir = FALSE, ...)
154163
if (!is.list(meta) || length(names(meta)) == 0) {
155164
warning('Invalid YAML option format in chunk: \n', one_string(meta), '\n')
156165
meta = list()

man/divide_chunk.Rd

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)