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

Preserving folded blocks when rewriting YAML #137

Open
arnaudgallou opened this issue Dec 26, 2023 · 1 comment
Open

Preserving folded blocks when rewriting YAML #137

arnaudgallou opened this issue Dec 26, 2023 · 1 comment

Comments

@arnaudgallou
Copy link
Contributor

arnaudgallou commented Dec 26, 2023

I'm developing a package that can insert author metadata in YAML header of Quarto files. I do this by rewriting YAML blocks using the following steps:

# extract YAML from file
yaml.load()
# add new data
as.yaml()
# insert YAML back into file

The problem is that some information are lost in the process, including folded blocks:

lines <- "
  bar: >
    Lorem ipsum
    Vivamus quis
"

yaml::yaml.load(lines) |> 
  yaml::as.yaml() |> 
  cat()
#> bar: |
#>   Lorem ipsum Vivamus quis

My question is: is there any way to preserve folded blocks in that case? I'm not aware of a special type I can use to target the > with a handler.

I'm considering to insert author data in a separate YAML header when my insert function detects complex cases (e.g. the use of custom tags, repeated blocks, comments being dropped due to the libyaml dependency, etc.). I would prefer to do that on rare occasions though.

@arnaudgallou arnaudgallou changed the title Preserving folded blocks when rewriting YAML blocks Preserving folded blocks when rewriting YAML Dec 26, 2023
@spgarbet
Copy link
Member

This would require preserving meta-information about the storage format in the converted objects. R could do this utilizing attributes. However, this library utilizes the libyaml library. This meta-information is stripped before it's tokenized. Without diving into the next library down it's not possible.

> lines <- "
+   bar: >
+     Lorem ipsum
+     Vivamus quis
+ "
> 
> invisible(yaml::yaml.load(lines, handlers=list(str=function(x) {print(x); x})))
[1] "bar"
[1] "Lorem ipsum Vivamus quis\n"

The str handler is what passed from the yaml tokenizer. If I get down into that level I'll be finishing yaml 1.2 compliance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants