diff --git a/DESCRIPTION b/DESCRIPTION index 1bf9844b98..79996eecc6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: knitr Type: Package Title: A General-Purpose Package for Dynamic Report Generation in R -Version: 1.43.5 +Version: 1.43.6 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Abhraneel", "Sarma", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index e4a4f800ac..f56708e11a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,10 @@ - Use the correct type of progress bar when rendering Quarto documents in RStudio, which takes place in RStudio's background jobs pane (thanks, @hadley, #2271). +## MINOR CHANGES + +- In-body chunk options (`#|`) are now preserved when extracting code from a document via `purl()` (thanks, @LuisLauM, #2268). + # CHANGES IN knitr VERSION 1.43 ## NEW FEATURES diff --git a/R/block.R b/R/block.R index feaf7fe2e1..ff48f77386 100644 --- a/R/block.R +++ b/R/block.R @@ -626,13 +626,12 @@ process_tangle.inline = function(x) { # add a label [and extra chunk options] to a code chunk label_code = function(code, options) { - code = one_string(c('', code, '')) - comments = if (is_quarto()) one_string(options$params$yaml.code) else paste0( + comments = paste0( '## ----', options$params.src, strrep('-', max(getOption('width') - 11L - nchar(options$params.src), 0L)), '----' ) - paste0(comments, code) + one_string(c(comments, options$params.chunk, code, '')) } as.source = function(code) { diff --git a/R/parser.R b/R/parser.R index f7d67a0fa6..0f76ab62e1 100644 --- a/R/parser.R +++ b/R/parser.R @@ -145,7 +145,9 @@ parse_block = function(code, header, params.src, markdown_mode = out_format('mar } } - structure(list(params = params, params.src = params.src), class = 'block') + structure(class = 'block', list( + params = params, params.src = params.src, params.chunk = parts$src) + ) } get_chunk_indent = function(header) { diff --git a/tests/testit/test-block.R b/tests/testit/test-block.R index b00446b975..51d487d970 100644 --- a/tests/testit/test-block.R +++ b/tests/testit/test-block.R @@ -17,13 +17,8 @@ assert('inline_exec only accept character result', { assert('label_code correct adds comment on code for yaml block or parsed param', { oldW = getOption('width') options(width = 20) - old = .knitEnv$is_quarto - .knitEnv$is_quarto = FALSE (label_code("1+1", list(params.src = "test, eval=TRUE")) %==% "## ----test, eval=TRUE----\n1+1\n") options(width = oldW) - .knitEnv$is_quarto = TRUE - (label_code("1+1", - list(params = list(yaml.code = c("#| label: test", "#| eval: true"))) - ) %==% "#| label: test\n#| eval: true\n1+1\n") - .knitEnv$is_quarto = NULL + (label_code("1+1", list(params.chunk = c("#| label: test", "#| eval: true"))) %==% + "## --------\n#| label: test\n#| eval: true\n1+1\n") })