Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert chunk option to YAML #2151

Merged
merged 10 commits into from
Jul 26, 2022
25 changes: 23 additions & 2 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,8 @@ convert_chunk_header = function(
) {

type = match.arg(type)
if (type == 'yaml') stop('Convertion to YAML chunk header not implemented yet.')
if (type == "yaml" && !xfun::pkg_available("yaml"))
stop("Converting to yaml format require the yaml R package.")

# extract fenced header information
text = xfun::read_utf8(input)
Expand Down Expand Up @@ -858,7 +859,27 @@ convert_chunk_header = function(
strwrap(params3, width, prefix = prefix)
}
} else {
# YAML
params3 = parse_params(params2, label = FALSE)
cderv marked this conversation as resolved.
Show resolved Hide resolved

# 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) {
result <- ifelse(x, "true", "false")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think simply tolower(x) would work. Correct me if I'm wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes you're right it should work woth the automatic logical to character conversion.

class(result) <- "verbatim"
return(result)
}), line.sep = "\n"), "\n")[[1]]
params3 = paste0(prefix, params3)
}

if (nzchar(opt_chars$end)) params3 = paste0(params3, opt_chars$end)
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ escape_html = highr:::escape_html
#' @author Yihui Xie and Peter Ruckdeschel
#' @export
#' @examples library(knitr)
#' \donttest{# relies on r-forge.r-project.org being accessible
#' \dontrun{# 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 = function(path, project, extra = '') {
Expand Down
2 changes: 1 addition & 1 deletion man/read_rforge.Rd

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