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

Provide a helper function to create metadata yaml block in document body #137

Open
cderv opened this issue Jan 16, 2024 · 4 comments
Open
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@cderv
Copy link
Collaborator

cderv commented Jan 16, 2024

Related to #160

cc @kmasiello

Example

---
title: "Some title"
format: html
---

```{r}
admin <- TRUE
```


```{r}
#| output: asis
cat(
  "---",
  paste0("admin: ", ifelse(admin, "true", "false")),
  "---",
  sep = "\n"
)
```

Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

This is quite long to write, so it could use a helper

Using inline R code

---
title: "Some title"
format: html
---

```{r}
admin <- TRUE
```


```{r}
write_meta <- function(meta) {
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
    })
  res <- yaml::as.yaml(meta, handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}
```


`r write_meta(list(admin = TRUE))`


Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

Or using a cell with option

---
title: "Some title"
format: html
keep-md: true
---

```{r}
admin <- TRUE
```


```{r}
write_meta <- function(meta) {
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
    })
  res <- yaml::as.yaml(meta, handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}
```

```{r}
#| echo: false
#| output: asis
write_meta(list(admin = TRUE))
```

Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

Food for thoughts...

@cderv cderv added the enhancement New feature or request label Jan 16, 2024
@cderv
Copy link
Collaborator Author

cderv commented Jan 16, 2024

More thoughts:

I wonder if we could make that more transparent and internal to Quarto so that a user would not have to write the meta value.

Same way a user don't have to write the HTML dependencies in header - knitr and quarto will do it.

We could maybe leverage the knit_meta field in knitr to pass information about which list object is considered to be quarto metadata so that quarto itself will add them to the rest of metadata. 🤔

POC of what could work to write all meta collected at once
---
title: "Some title"
format: html
keep-md: true
---



Example of helpers

```{r}
as_quarto_meta <- function(meta) {
  yaml_quarto_meta <- structure(meta, class = "quarto_yaml_meta")
  knitr::asis_output(list(), meta = list(yaml_quarto_meta))
}
```

Computing a value in R 

```{r}
admin <- TRUE
```

Setting it as a meta to us

```{r}
# add meta
as_quarto_meta(list(admin = admin))
```


Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

```{r}
as_quarto_meta(list(userid = "abc"))
```


simulate quarto rendering to handle knit_meta as rmarkdown would handle latex dependencies and html dependencies

```{r}
write_meta <- function() {
  quarto_meta <- rmarkdown:::knit_meta_reset("quarto_yaml_meta")
  if (!length(quarto_meta)) return()
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
  })
  res <- yaml::as.yaml(unlist(quarto_meta, recursive = FALSE), handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}

write_meta()
```

Integrating this logic in Quarto directly would not pass by a writing on meta as yaml in the doc, but would be passed directly to quarto...

Not sure it worth it - only if we want to have the exact same logic maybe in jupyter if similar tooling as HTML deps come out one day 🤔

@kmasiello
Copy link

See rich-iannone/quarto-email#9 for a prime use case, but smoothing out these edges will be relevant beyond conditional emails.

@cderv
Copy link
Collaborator Author

cderv commented Feb 27, 2024

@rich-iannone can you look at the POC idea above (#137 (comment)) ?

I believe the function offered would solve the issue right ?

this R package quarto could provide a function to write simply any list as a YAML block in Markdown, but I wonder if the meta idea could be even better for user experience.

Even quarto itself could retrieve the meta defined by any R function and do the YAML metadata addition. We can discuss live too.

Thanks

@cderv
Copy link
Collaborator Author

cderv commented Apr 29, 2024

Listing another usage of this: A good workflow to provide a solution in Quarto for previous !expr logic.

Example:

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

No branches or pull requests

2 participants