Skip to content

Commit 7ef641f

Browse files
authored
Add an argument lua_filters to pandoc_options() to store them in the output format object (#1899)
close #1897
1 parent fbd0b1f commit 7ef641f

23 files changed

+204
-78
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export(pandoc_exec)
6262
export(pandoc_highlight_args)
6363
export(pandoc_include_args)
6464
export(pandoc_latex_engine_args)
65+
export(pandoc_lua_filter_args)
6566
export(pandoc_metadata_arg)
6667
export(pandoc_options)
6768
export(pandoc_path_arg)

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
rmarkdown 2.4
22
================================================================================
33

4+
- Lua filters handling has been improved internally with some user-facing changes (#1899):
5+
- New exported function `pandoc_lua_filter_args()` to return the Pandoc command-line argument to add a Lua filter.
6+
- New argument `lua_filters` in `pandoc_options()` to pass the Lua filter paths to use with a format. This allow output format authors to add filters for a custom format using the `pandoc` argument of `output_format()` and to get filters from a format using `fmt$pandoc$lua_filters`.
7+
- The Lua filters of an output format are now passed to Pandoc in `render()`. By default, they are passed to Pandoc before any other format-defined or user-defined Pandoc arguments (usually via the `pandoc_args` option of an output format). This ensures that filters of an output format are executed first. To change the default, you need to deal with it in a custom format (i.e., modify the elements in `fmt$pandoc$lua_filters`, such as reordering them).
8+
49
- Since **rmarkdown** 1.16, Pandoc's fenced `Div`'s are converted to LaTeX environments when the output format is LaTeX, e.g., `::: {.center data-latex=""}` is converted to `\begin{center}`. The attribute `data-latex` of the `Div` was mandatory, even if it is empty. In **rmarkdown** 2.2, we silently drop this requirement, which means `::: {.center}` is converted to `\begin{center}`. This turns out to be a bad idea, because users have no control over which Div's to be converted to LaTeX environments. Previously, they could opt-in by the `data-latex` attribute, but with **rmarkdown** 2.3, all Div's are converted to LaTeX environments unconditionally. What's more, this change led to bugs like https://stackoverflow.com/q/62340425/559676 and https://github.com/rstudio/bookdown/issues/883. Therefore the `data-latex` attribute became mandatory again in this version. If the LaTeX environment does not need arguments, you may use `data-latex=""`.
510

611
- The two Lua fitlers `pagebreak.lua` and `latex-div.lua` (introduced in **rmarkdown** 1.16) are also applied to the output format `beamer_presentation` now (thanks, @XiangyunHuang, #1815).
@@ -9,7 +14,6 @@ rmarkdown 2.4
914

1015
- Added the `number_sections` argument to following formats: `github_document`, `ioslides_presentation`, `md_document`, `odt_document`, `powerpoint_presentation`, `rtf_document`, `slidy_presentation`, `word_document`. These are powered by a lua filter and requires Pandoc > 2.0. It will silently have no effect has before with previous pandoc version (thanks @atusy 1893). Pandoc >= 2.10.1 adds `--number-sections` for docx format, and thus `word_document` prefers the native feature to the lua filter (thanks, @jooyoungseo, #1869).
1116

12-
1317
- For the output format `pdf_document`, the option `fig_crop` will not be enabled unless both the programs `pdfcrop` and `ghostscript` are found (thanks, @dalupus, yihui/knitr#954).
1418

1519

R/beamer_presentation.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ beamer_presentation <- function(toc = FALSE,
126126
# make sure the graphics package is always loaded
127127
if (identical(template, "default")) args <- c(args, "--variable", "graphics=yes")
128128

129-
args <- c(args, pandoc_lua_filters(c("pagebreak.lua", "latex-div.lua")))
130-
131129
# custom args
132130
args <- c(args, pandoc_args)
133131

@@ -151,11 +149,14 @@ beamer_presentation <- function(toc = FALSE,
151149
# return format
152150
output_format(
153151
knitr = knitr_options_pdf(fig_width, fig_height, fig_crop, dev),
154-
pandoc = pandoc_options(to = "beamer",
155-
from = from_rmarkdown(fig_caption, md_extensions),
156-
args = args,
157-
latex_engine = latex_engine,
158-
keep_tex = keep_tex),
152+
pandoc = pandoc_options(
153+
to = "beamer",
154+
from = from_rmarkdown(fig_caption, md_extensions),
155+
args = args,
156+
latex_engine = latex_engine,
157+
keep_tex = keep_tex,
158+
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))
159+
),
159160
pre_processor = pre_processor,
160161
intermediates_generator = intermediates_generator,
161162
clean_supporting = !keep_tex,

R/context_document.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ context_document <- function(toc = FALSE,
9090
# content includes
9191
args <- c(args, includes_to_pandoc_args(includes))
9292

93-
# lua filters (added if pandoc > 2)
94-
args <- c(args, pandoc_lua_filters("pagebreak.lua"))
95-
9693
# args args
9794
args <- c(args, pandoc_args)
9895

@@ -129,11 +126,13 @@ context_document <- function(toc = FALSE,
129126
# return format
130127
output_format(
131128
knitr = knitr_options_pdf(fig_width, fig_height, fig_crop, dev),
132-
pandoc = pandoc_options(to = paste(c("context", output_extensions), collapse = ""),
133-
from = from_rmarkdown(fig_caption, md_extensions),
134-
args = args,
135-
keep_tex = FALSE,
136-
ext = ext),
129+
pandoc = pandoc_options(
130+
to = paste(c("context", output_extensions), collapse = ""),
131+
from = from_rmarkdown(fig_caption, md_extensions),
132+
args = args,
133+
keep_tex = FALSE,
134+
ext = ext,
135+
lua_filters = pkg_file_lua("pagebreak.lua")),
137136
clean_supporting = !isTRUE(keep_tex),
138137
keep_md = keep_md,
139138
df_print = df_print,

R/github_document.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ github_document <- function(toc = FALSE,
3333
# add special markdown rendering template to ensure we include the title fields
3434
# and add an optional feature to number sections
3535
pandoc_args <- c(
36-
pandoc_args, "--template", pkg_file_arg(
37-
"rmarkdown/templates/github_document/resources/default.md"),
38-
if (number_sections) pandoc_lua_filters("number-sections.lua")
36+
pandoc_args, "--template",
37+
pkg_file_arg("rmarkdown/templates/github_document/resources/default.md")
3938
)
4039

4140

@@ -46,8 +45,9 @@ github_document <- function(toc = FALSE,
4645

4746
format <- md_document(
4847
variant = variant, toc = toc, toc_depth = toc_depth,
49-
fig_width = fig_width, fig_height = fig_height, dev = dev,
50-
df_print = df_print, includes = includes, md_extensions = md_extensions,
48+
number_sections = number_sections, fig_width = fig_width,
49+
fig_height = fig_height, dev = dev, df_print = df_print,
50+
includes = includes, md_extensions = md_extensions,
5151
pandoc_args = pandoc_args
5252
)
5353

R/html_document_base.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,6 @@ html_document_base <- function(theme = NULL,
106106

107107
preserved_chunks <<- extract_preserve_chunks(input_file)
108108

109-
# a lua filters added if pandoc2.0
110-
args <- c(args, pandoc_lua_filters(c("pagebreak.lua", "latex-div.lua")))
111-
112109
args
113110
}
114111

@@ -170,7 +167,9 @@ html_document_base <- function(theme = NULL,
170167

171168
output_format(
172169
knitr = NULL,
173-
pandoc = pandoc_options(to = "html", from = NULL, args = args),
170+
pandoc = pandoc_options(
171+
to = "html", from = NULL, args = args,
172+
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))),
174173
keep_md = FALSE,
175174
clean_supporting = FALSE,
176175
pre_knit = pre_knit,

R/ioslides_presentation.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ ioslides_presentation <- function(number_sections = FALSE,
345345
args <- c(args, pandoc_args)
346346

347347
# number sections
348-
if (number_sections) args <- c(args, pandoc_lua_filters("number-sections.lua"))
348+
if (number_sections)
349+
args <- c(args, pandoc_lua_filter_args(pkg_file_lua("number-sections.lua")))
349350

350351
lua_writer <- file.path(dirname(input_file), "ioslides_presentation.lua")
351352
# The input directory may not be writable (on e.g. Shiny Server), so write

R/md_document.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ md_document <- function(variant = "markdown_strict",
6060
# pandoc args
6161
args <- c(args, pandoc_args)
6262

63-
# number sections with lua filter
64-
if (number_sections) args <- c(args, pandoc_lua_filters("number-sections.lua"))
65-
6663
# add post_processor for yaml preservation
6764
post_processor <- if (preserve_yaml && variant != 'markdown') {
6865
function(metadata, input_file, output_file, clean, verbose) {
@@ -79,10 +76,12 @@ md_document <- function(variant = "markdown_strict",
7976
# return format
8077
output_format(
8178
knitr = knitr_options_html(fig_width, fig_height, fig_retina, FALSE, dev),
82-
pandoc = pandoc_options(to = variant,
83-
from = from_rmarkdown(extensions = md_extensions),
84-
args = args,
85-
ext = ext),
79+
pandoc = pandoc_options(
80+
to = variant,
81+
from = from_rmarkdown(extensions = md_extensions),
82+
args = args,
83+
ext = ext,
84+
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")),
8685
clean_supporting = FALSE,
8786
df_print = df_print,
8887
post_processor = post_processor

R/odt_document.R

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,9 @@ odt_document <- function(number_sections = FALSE,
6363
# reference odt
6464
args <- c(args, reference_doc_args("odt", reference_odt))
6565

66-
# lua filters (added if pandoc > 2)
67-
args <- c(args, pandoc_lua_filters("pagebreak.lua"))
68-
6966
# pandoc args
7067
args <- c(args, pandoc_args)
7168

72-
# number sections with lua filter
73-
if (number_sections) args <- c(args, pandoc_lua_filters("number-sections.lua"))
74-
7569
saved_files_dir <- NULL
7670
pre_processor <- function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
7771
saved_files_dir <<- files_dir
@@ -85,9 +79,12 @@ odt_document <- function(number_sections = FALSE,
8579
# return output format
8680
output_format(
8781
knitr = knitr,
88-
pandoc = pandoc_options(to = "odt",
89-
from = from_rmarkdown(fig_caption, md_extensions),
90-
args = args),
82+
pandoc = pandoc_options(
83+
to = "odt",
84+
from = from_rmarkdown(fig_caption, md_extensions),
85+
args = args,
86+
lua_filters = pkg_file_lua(c(
87+
"pagebreak.lua", if (number_sections) "number-sections.lua"))),
9188
keep_md = keep_md,
9289
pre_processor = pre_processor,
9390
intermediates_generator = intermediates_generator

R/output_format.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ merge_pandoc_options <- function(base,
198198

199199
res <- merge_lists(base, overlay, recursive = FALSE)
200200
res$args <- c(base$args, overlay$args)
201+
res$lua_filters <- c(base$lua_filters, overlay$lua_filters)
201202
res
202203
}
203204

@@ -290,6 +291,10 @@ knitr_options_pdf <- function(fig_width,
290291
#' chooses default based on \code{to}). This is typically used to force
291292
#' the final output of a latex or beamer conversion to be \code{.tex}
292293
#' rather than \code{.pdf}.
294+
#' @param lua_filters Character vector of file paths to lua filters to use with
295+
#' this format. They will be added to pandoc command line call using
296+
#' \code{--lua-filter} argument. See \code{vignette("lua-filters", package =
297+
#' "rmarkdown")} to know more about lua filters.
293298
#' @return An list that can be passed as the \code{pandoc} argument of the
294299
#' \code{\link{output_format}} function.
295300
#' @seealso \link{output_format}, \link{rmarkdown_format}
@@ -299,13 +304,15 @@ pandoc_options <- function(to,
299304
args = NULL,
300305
keep_tex = FALSE,
301306
latex_engine = c("pdflatex", "lualatex", "xelatex"),
302-
ext = NULL) {
307+
ext = NULL,
308+
lua_filters = NULL) {
303309
list(to = to,
304310
from = from,
305311
args = args,
306312
keep_tex = keep_tex,
307313
latex_engine = match.arg(latex_engine),
308-
ext = ext)
314+
ext = ext,
315+
lua_filters = lua_filters)
309316
}
310317

311318
#' R Markdown input format definition

0 commit comments

Comments
 (0)