Skip to content

Add output_format_dependency() - #2462

Merged
cderv merged 12 commits into
rstudio:mainfrom
atusy:deps
Jul 11, 2023
Merged

cderv merged 12 commits into
rstudio:mainfrom
atusy:deps

Conversation

@atusy

@atusy atusy commented Mar 9, 2023

Copy link
Copy Markdown
Collaborator

This PR is an attempt to solve #2347, but in a more generalized way.

In the issue #2347, I suggested pandoc_dependency(), however, this function needs to follow the updates of Pandoc's API changes.

Instead, this PR implements output_format_dependency(), which allows defining some of the arguments in output_format from within a Rmd file.
Supported arguments are pre_processor, post_processor, file_scope, on_exit.
The others are not because dependency can be solved after post_knit, which finalizes knit_meta.

Fortunately, pre_processor can return extra arguments to pass to pandoc, so we can dynamically add lua filters, which covers the motivation of #2347.

output_format_dependency(
  name = "example",
  pre_processor = function(...) {
    pandoc_lua_filter_args(c("example1.lua", "example2.lua"))
  },
  post_processor = function(...) {},
)

Also, this PR should be helpful in the situations like #2314.
In #2314, the issue author suggests knit filed in the YAML front matter to modify the render() function.
This is a powerful solution, but limited to RStudio users...

My proposal is available for R users on any environments.

Note that there is a remaining work to implement attach_output_format_dependencies(), like htmltools::attachDependencies(), but this can be a future work.

@atusy
atusy force-pushed the deps branch 2 times, most recently from 852a4dd to 63fd7e2 Compare March 9, 2023 00:49
@atusy

atusy commented Mar 9, 2023

Copy link
Copy Markdown
Collaborator Author

An example Rmd

Details
---
title: foo
output:
  html_document:
    keep_md: true
---

```{cat, engine.opts=list(file = "example.lua")}
print("====lua filter====")
```

```{r}
rmarkdown::output_format_dependency(
  name = "foo",
  pre_processor = function(...) {
    cat("====pre_processor====\n")
    pandoc_lua_filter_args("example.lua")
  },
  post_processor = function(metadata, input_file, output_file, ...) {
    cat("====post_processor====\n")
    output_file
  }
)
```

@atusy
atusy marked this pull request as ready for review March 9, 2023 00:54
@cderv

cderv commented Mar 9, 2023

Copy link
Copy Markdown
Collaborator

Wow this is an interesting idea - I need to look into that closer so it could be not right now as I am currently focus on Quarto 1.3 release. But as soon as I can make time for some rmarkdown work (which is completely due and should happen soon), I'll give this a try. Maybe @yihui will have more time before.

Anyhow, generalized approach needs more thoughts than simple PR. Your contribution is really appreciated and I wanted to be honest with you with current situation so that you don't think this PR is ignored! Thank you very much for this @atusy !

@cderv cderv self-assigned this Mar 9, 2023
@atusy

atusy commented Mar 9, 2023

Copy link
Copy Markdown
Collaborator Author

No worries! I'm not hurry.

@cderv cderv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That is really clever way to generalize that ! Thank you.

Should we completely make all the output_format argument available that way ? it seems something possible.

At least, I would like to see if we can add a specific handling for some of the pandoc component. args and lua_filters are among the content one would want to modify

I wonder if we shouldn't extend this also to the knitr and pandoc component currently set as knitr = NULL and pandoc = list()

This would mean being possible to do something like

output_format_dependency <- function(name,
                                     pre_processor = NULL,
                                     post_processor = NULL,
                                     file_scope = NULL,
                                     on_exit = NULL,
                                     knitr = list(),
                                     pandoc = list())

so that one can do

output_format_dependency(pandoc = list(lua_filters = "example.lua"))

Did you try making that works ?

Obviously we need to add some unit test for this so that we are sure it merges as we expect, and we don't break it in the future.

Otherwise, all good ! Really exciting work ! Thanks again !

Comment thread R/output_format.R Resolved
@atusy

atusy commented May 23, 2023

Copy link
Copy Markdown
Collaborator Author

@cderv

Thank you so much for your review! I am excited by your reactions!

I will try adding the pandoc argument and unit tests in this or next week.

I wonder if we shouldn't extend this also to the knitr and pandoc component currently set as knitr = NULL and pandoc = list()

About the above comment,

  • knitr is not supproted because

    • the timing of solving output_format_dependency is generally too late because merge_output_format_dependencies() is called after knit().
    • users/developers already have full powers on controlling the options within chunks by setters and getters (e.g., opts_knit$set())
    • Calling setters within output_format_dependency() can be a solution, but the timing of the effect can be ambious. Specifically, there is no way to controll chunk options of the current chunk opts_current$set should error yihui/knitr#1798 .
  • pandoc seems possible. Although I wrote it is not supported for some reason, maybe I was wrong here 😄

    Supported arguments are pre_processor, post_processor, file_scope, on_exit.
    The others are not because dependency can be solved after post_knit, which finalizes knit_meta.

@cderv

cderv commented May 24, 2023

Copy link
Copy Markdown
Collaborator

the timing of solving output_format_dependency is generally too late because merge_output_format_dependencies() is called after knit().

Oh indeed. I missed that. I was too exited by the new feature probably ! 😄

The others are not because dependency can be solved after post_knit, which finalizes knit_meta.

Interesting 🤔 - initial issue was to have a way to add Lua filter more easily as a dependency. I thought we could somehow add to the pandoc = list(lua_filters = ) . Maybe this is the only element of Pandoc that we can use

Otherwise, it is easy enough to do a wrapper of this new function, as lua_filters can be added in pre_processor as you did. But I did it another for some reason (I think to add a format lua filter last or something like that).

I let you look into this and we'll see if we can change things to make it happen.

@atusy

atusy commented May 25, 2023 •

Copy link
Copy Markdown
Collaborator Author

Okay, I confirmed pandoc = list(lua_filters = ...) works.
Here's an example Rmd that a filter echoes ====lua filter==== to stdout.

Rmd
---
title: foo
output:
  html_document:
    keep_md: true
---

```{cat, engine.opts=list(file = "example.lua")}
print("====lua filter====")
```

```{r}
rmarkdown::output_format_dependency(
  name = "foo",
  pandoc = list(
    lua_filters = "example.lua"
  ),
  pre_processor = function(...) {
    cat("====pre_processor====\n")
    # pandoc_lua_filter_args("example.lua")
  },
  post_processor = function(metadata, input_file, output_file, ...) {
    cat("====post_processor====\n")
    output_file
  }
)
```
stdout from render()
processing file: example.Rmd
                                                                                                                                                                                                                                                                                                                                                                                            
                            
output file: example.knit.md

====pre_processor====
/usr/bin/pandoc +RTS -K512m -RTS example.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output example.html --lua-filter /home/atusy/ghq/github.com/rstudio/rmarkdown/inst/rmarkdown/lua/pagebreak.lua --lua-filter /home/atusy/ghq/github.com/rstudio/rmarkdown/inst/rmarkdown/lua/latex-div.lua --lua-filter example.lua --embed-resources --standalone
 --variable bs3=TRUE --section-divs --template /home/atusy/ghq/github.com/rstudio/rmarkdown/inst/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable 'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in-header /tmp/RtmpxmaQr6/rmarkdown-strb7dc26a6dd7a3.html 
====lua filter====
====post_processor====

Output created: example.html

So the last task is to add tests.

@atusy

atusy commented May 26, 2023

Copy link
Copy Markdown
Collaborator Author

To implement tests, I rebased my PR onto #2488 .
I will rebase again to main when #2488 is merged.

@cderv cderv left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All good. You can rebase on main and maybe make the suggested change, and all good to merge !

Thank you

(and sorry again for the delay)

Comment thread R/render.R Resolved
@CLAassistant

CLAassistant commented Jul 11, 2023 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@cderv cderv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Awesome !

Need to be AFK but I'll rebase later if you haven't done it and then I'll merge

Thank you

@atusy

atusy commented Jul 11, 2023

Copy link
Copy Markdown
Collaborator Author

rebased and added news.

@cderv cderv changed the title add output_format_dependency() to modify output format from within Rmd files Add output_format_dependency() Jul 11, 2023
@cderv
cderv merged commit 1d739bd into rstudio:main Jul 11, 2023
j-magnolia pushed a commit to j-magnolia/rmarkdown that referenced this pull request Aug 16, 2023
* rstudio/main:
  start the next version
  CRAN release v2.24
  shinyrmd: Safer dependency extraction from pre-rendered HTML (rstudio#2500)
  quote the version number per CRAN's request
  Add output_format_dependency() (rstudio#2462)
  file_scope is now correctly merged when creating output_format (rstudio#2488)
  Correctly run some tests only on CI
  start the next version
  CRAN release v2.23
  remove broken links
  suggest cleanrmd for e499bf7
  add news
  comparing version numbers with numbers is no longer allowed: https://bugs.r-project.org/show_bug.cgi?id=18548
  `find_external_resources` works with custom format using `theme` (rstudio#2494)
  start the next version
  CRAN release v2.22
  S3 generic/method consistency
  Change the code-folding button text from "Code" to "Show" (rstudio#2489)
  fix: bump jquery-ui to v1.13.2 to fix multiple CVEs (rstudio#2477)
  detecting external resources needs to consider css argument (rstudio#2486)
j-magnolia pushed a commit to j-magnolia/rmarkdown that referenced this pull request Aug 16, 2023
Merge remote-tracking branch 'rstudio/main' into jg-devel

# By Yihui Xie (13) and others
# Via Yihui Xie
* rstudio/main:
  start the next version
  CRAN release v2.24
  shinyrmd: Safer dependency extraction from pre-rendered HTML (rstudio#2500)
  quote the version number per CRAN's request
  Add output_format_dependency() (rstudio#2462)
  file_scope is now correctly merged when creating output_format (rstudio#2488)
  Correctly run some tests only on CI
  start the next version
  CRAN release v2.23
  remove broken links
  suggest cleanrmd for e499bf7
  add news
  comparing version numbers with numbers is no longer allowed: https://bugs.r-project.org/show_bug.cgi?id=18548
  `find_external_resources` works with custom format using `theme` (rstudio#2494)
  start the next version
  CRAN release v2.22
  S3 generic/method consistency
  Change the code-folding button text from "Code" to "Show" (rstudio#2489)
  fix: bump jquery-ui to v1.13.2 to fix multiple CVEs (rstudio#2477)
  detecting external resources needs to consider css argument (rstudio#2486)

# Conflicts:
#	DESCRIPTION
j-magnolia pushed a commit to j-magnolia/rmarkdown that referenced this pull request Aug 16, 2023
* jg-devel: (21 commits)
  Updated NEWS. Patched `merge_output_format_dependency` to ensure that named elements remain in the correct order.
  start the next version
  CRAN release v2.24
  shinyrmd: Safer dependency extraction from pre-rendered HTML (rstudio#2500)
  quote the version number per CRAN's request
  Add output_format_dependency() (rstudio#2462)
  file_scope is now correctly merged when creating output_format (rstudio#2488)
  Correctly run some tests only on CI
  start the next version
  CRAN release v2.23
  remove broken links
  suggest cleanrmd for e499bf7
  add news
  comparing version numbers with numbers is no longer allowed: https://bugs.r-project.org/show_bug.cgi?id=18548
  `find_external_resources` works with custom format using `theme` (rstudio#2494)
  start the next version
  CRAN release v2.22
  S3 generic/method consistency
  Change the code-folding button text from "Code" to "Show" (rstudio#2489)
  fix: bump jquery-ui to v1.13.2 to fix multiple CVEs (rstudio#2477)
  ...
@yihui

yihui commented Aug 22, 2023

Copy link
Copy Markdown
Collaborator

@atusy Do you have time to write a short blog post on this new feature? One or two examples would be enough. I feel many people will be interested in this feature. Feel free to publish the post in your own blog (https://blog.atusy.net), and we can help promote it. Thanks!

@atusy

atusy commented Aug 24, 2023

Copy link
Copy Markdown
Collaborator Author

Yeah sure. I would post one in the next week!

@atusy

atusy commented Aug 28, 2023

Copy link
Copy Markdown
Collaborator Author

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FR] Add a new type of dependency that bundles lua filter and other type of dependencies

4 participants