Skip to content

Commit

Permalink
Convert chunk options to YAML (#2151)
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Jul 26, 2022
1 parent 553b692 commit 45289c2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## NEW FEATURES

- Added a function `convert_chunk_header()` to convert the old in-header chunk options to the new in-body chunk options (#2149).
- Added a function `convert_chunk_header()` to convert the old in-header chunk options to the new in-body chunk options (#2149 #2151).

- Added support for a `php` engine like other engines for interpreted languages. It will call `php -r <code>`, with `<code>` being the chunk content (thanks, @ralmond, #2144).

Expand Down
28 changes: 22 additions & 6 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,8 @@ inline_expr = function(code, syntax) {
#' line. Long chunk option values will be wrapped onto several lines, and you
#' can use \code{width = 0} to keep one line per option only. \code{"wrap"}
#' will wrap all chunk options together using
#' \code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} is currently not
#' implemented and here as a placeholder for future support of YAML in-chunk
#' syntax for options.
#' \code{\link[base:strwrap]{base::strwrap}()}. \code{"yaml"} will convert
#' chunk options to YAML.
#' @param width An integer passed to \code{base::strwrap()} for \code{type =
#' "wrap"} and \code{type = "multiline"}. If set to \code{0}, deactivate the
#' wrapping (for \code{type = "multiline"} only).
Expand Down Expand Up @@ -807,8 +806,6 @@ convert_chunk_header = function(
) {

type = match.arg(type)
if (type == 'yaml') stop('Convertion to YAML chunk header not implemented yet.')

# extract fenced header information
text = xfun::read_utf8(input)
ext = xfun::file_ext(input)
Expand Down Expand Up @@ -858,7 +855,26 @@ convert_chunk_header = function(
strwrap(params3, width, prefix = prefix)
}
} else {
# YAML
params3 = parse_params(params2, label = FALSE)

# fix un-evaluated options for yaml by transforming to !expr val
params3 = lapply(params3, function(x) {
if (is.symbol(x) || is.language(x)) {
x = deparse(x, 500L)
attr(x, 'tag') = '!expr'
}
x
})
# convert to yaml and add prefix
params3 = strsplit(yaml::as.yaml(
params3, handlers = list(
# true / false instead of no
logical = function(x) {
x = tolower(x)
class(x) = 'verbatim'
x
}), line.sep = '\n'), '\n')[[1]]
params3 = paste0(prefix, params3)
}

if (nzchar(opt_chars$end)) params3 = paste0(params3, opt_chars$end)
Expand Down
7 changes: 4 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,11 @@ escape_html = highr:::escape_html
#' @return A character vector of the source code.
#' @author Yihui Xie and Peter Ruckdeschel
#' @export
#' @examples library(knitr)
#' \donttest{# relies on r-forge.r-project.org being accessible
#' @examplesIf interactive()
#' library(knitr)
#' # relies on r-forge.r-project.org being accessible
#' read_rforge('rgl/R/axes.R', project = 'rgl')
#' read_rforge('rgl/R/axes.R', project = 'rgl', extra='&revision=519')}
#' read_rforge('rgl/R/axes.R', project = 'rgl', extra='&revision=519')
read_rforge = function(path, project, extra = '') {
base = 'http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg'
read_utf8(sprintf('%s/%s?root=%s%s', base, path, project, extra))
Expand Down
5 changes: 2 additions & 3 deletions man/convert_chunk_header.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/read_rforge.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45289c2

Please sign in to comment.