Skip to content

Commit

Permalink
add a parse argument to yaml_body() to make it possible not to pa…
Browse files Browse the repository at this point in the history
…rse YAML
  • Loading branch information
yihui committed Dec 7, 2024
1 parent ae934e2 commit 2488ac8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.49.7
Version: 0.49.8
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
14 changes: 8 additions & 6 deletions R/yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,27 @@ yaml_handlers = function(h, envir) {
#' Partition the YAML metadata and the body in a document
#'
#' Split a document into the YAML metadata (which starts with `---` in the
#' beginning of the document) and the body. The YAML metadata will be parsed.
#' beginning of the document) and the body.
#' @param x A character vector of the document content.
#' @param ... Arguments to be passed to `yaml_load()`.
#' @param parse Whether to parse the YAML data.
#' @export
#' @return A list of components `yaml` (the parsed YAML data), `lines` (starting
#' and ending line numbers of YAML), and `body` (a character vector of the
#' body text). If YAML metadata does not exist in the document, the components
#' @return A list of components `yaml` (the YAML data), `lines` (starting and
#' ending line numbers of YAML), and `body` (a character vector of the body
#' text). If YAML metadata does not exist in the document, the components
#' `yaml` and `lines` will be missing.
#' @examples
#' xfun::yaml_body(c('---', 'title: Hello', 'output: litedown::html_format', '---', '', 'Content.'))
yaml_body = function(x, ...) {
yaml_body = function(x, ..., parse = TRUE) {
n = length(x)
res = if (length(i <- locate_yaml(x)) == 0) {
list(body = x)
} else list(
yaml = x[i[1]:i[2]], body = c(rep('', i[2]), tail(x, n - i[2])), lines = i
)
if ((n <- length(res$yaml)) >= 2) {
res['yaml'] = list(yaml_load(res$yaml[-c(1, n)], ...))
res$yaml = res$yaml[-c(1, n)]
if (parse) res['yaml'] = list(yaml_load(res$yaml, ...))
}
res
}
Expand Down
12 changes: 7 additions & 5 deletions man/yaml_body.Rd

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

0 comments on commit 2488ac8

Please sign in to comment.