These function extracts all YAML fragments from a file, returning a list of character vectors containing the extracted fragments.
extract_yaml_fragments(
text,
file,
delimiterRegEx = "^---$",
ignoreOddDelimiters = FALSE,
encoding = "UTF-8",
silent = TRUE
)
As text
or file
, you can specify a file
to read with
encoding encoding
, which will then be read using base::readLines()
. If the
argument is named text
, whether it is the path to an existing file is checked
first, and if it is, that file is read. If the argument is named file
, and it
does not point to an existing file, an error is produced (useful if calling
from other functions). A text
should be a character vector where every
element is a line of the original source (like provided by base::readLines()
);
although if a character vector of one element and including at least one
newline character (\\n
) is provided as text
, it is split at the newline
characters using base::strsplit()
. Basically, this behavior means that the
first argument can be either a character vector or the path to a file; and if
you're specifying a file and you want to be certain that an error is thrown if
it doesn't exist, make sure to name it file
.
The regular expression used to locate YAML fragments.
Whether to throw an error (FALSE) or delete the last delimiter (TRUE) if an odd number of delimiters is encountered.
The encoding to use when calling readLines()
. Set to
NULL to let readLines()
guess.
Whether to be silent (TRUE
) or informative (FALSE
).
A list of character vectors, where each vector corresponds to one YAML fragment in the source file or text.
extract_yaml_fragments(text="
---
First: YAML fragment
id: firstFragment
---
Outside of YAML
---
Second: YAML fragment
id: secondFragment
parentId: firstFragment
---
Also outside of YAML
");
#> [[1]]
#> [1] "---" "First: YAML fragment" " id: firstFragment"
#> [4] "---"
#> attr(,"class")
#> [1] "yamlFragment"
#>
#> [[2]]
#> [1] "---" "Second: YAML fragment"
#> [3] " id: secondFragment" " parentId: firstFragment"
#> [5] "---"
#> attr(,"class")
#> [1] "yamlFragment"
#>
#> attr(,"class")
#> [1] "yamlFragments" "list"