Skip to content

Commit 8824c26

Browse files
authored
Export pkg_file_lua for package author to use (#1904)
1 parent 08ab63d commit 8824c26

File tree

13 files changed

+94
-17
lines changed

13 files changed

+94
-17
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export(pandoc_variable_arg)
7373
export(pandoc_version)
7474
export(parse_html_notebook)
7575
export(pdf_document)
76+
export(pkg_file_lua)
7677
export(powerpoint_presentation)
7778
export(publish_site)
7879
export(relative_to)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ rmarkdown 2.4
55
- New exported function `pandoc_lua_filter_args()` to return the Pandoc command-line argument to add a Lua filter.
66
- 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`.
77
- 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+
- New exported function `pkg_file_lua()` to get the full system path of a
9+
Lua filter included in a package source within `inst/rmarkdown/lua` folder.
10+
(thanks, @atusy, #1903)
811

912
- Fixed the path separators for the `css` parameter in YAML frontmatter for HTML output files under Windows. Previously, forward slashes in `css` paths were converted to backslashes (thanks, @jonathan-g, #1862).
1013

R/context_document.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ context_document <- function(toc = FALSE,
132132
args = args,
133133
keep_tex = FALSE,
134134
ext = ext,
135-
lua_filters = pkg_file_lua("pagebreak.lua")),
135+
lua_filters = pkg_file_lua("pagebreak.lua")
136+
),
136137
clean_supporting = !isTRUE(keep_tex),
137138
keep_md = keep_md,
138139
df_print = df_print,

R/html_document_base.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ html_document_base <- function(theme = NULL,
169169
knitr = NULL,
170170
pandoc = pandoc_options(
171171
to = "html", from = NULL, args = args,
172-
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))),
172+
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))
173+
),
173174
keep_md = FALSE,
174175
clean_supporting = FALSE,
175176
pre_knit = pre_knit,

R/md_document.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ md_document <- function(variant = "markdown_strict",
8181
from = from_rmarkdown(extensions = md_extensions),
8282
args = args,
8383
ext = ext,
84-
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")),
84+
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")
85+
),
8586
clean_supporting = FALSE,
8687
df_print = df_print,
8788
post_processor = post_processor

R/odt_document.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ odt_document <- function(number_sections = FALSE,
8383
to = "odt",
8484
from = from_rmarkdown(fig_caption, md_extensions),
8585
args = args,
86-
lua_filters = pkg_file_lua(c(
87-
"pagebreak.lua", if (number_sections) "number-sections.lua"))),
86+
lua_filters = pkg_file_lua(
87+
c("pagebreak.lua", if (number_sections) "number-sections.lua"))
88+
),
8889
keep_md = keep_md,
8990
pre_processor = pre_processor,
9091
intermediates_generator = intermediates_generator

R/pdf_document.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ pdf_document <- function(toc = FALSE,
199199
args = args,
200200
latex_engine = latex_engine,
201201
keep_tex = keep_tex,
202-
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))),
202+
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))
203+
),
203204
clean_supporting = !keep_tex,
204205
keep_md = keep_md,
205206
df_print = df_print,

R/rtf_document.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ rtf_document <- function(toc = FALSE,
7777
to = "rtf",
7878
from = from_rmarkdown(extensions = md_extensions),
7979
args = args,
80-
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")),
80+
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")
81+
),
8182
keep_md = keep_md,
8283
pre_processor = pre_processor,
8384
post_processor = post_processor

R/util.R

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,46 @@ pandoc_output_ext <- function(ext, to, input) {
3333
paste0(".", to)
3434
}
3535

36-
pkg_file <- function(...) {
37-
system.file(..., package = "rmarkdown")
38-
}
39-
40-
pkg_file_arg <- function(...) {
41-
pandoc_path_arg(pkg_file(...))
42-
}
43-
44-
pkg_file_lua <- function(...) {
45-
pkg_file("rmd", "lua", ...)
36+
pkg_file <- function(..., package = "rmarkdown") {
37+
system.file(..., package = package)
38+
}
39+
40+
pkg_file_arg <- function(..., package = "rmarkdown") {
41+
pandoc_path_arg(pkg_file(..., package = package))
42+
}
43+
44+
#' Get Lua filters full paths in a package
45+
#'
46+
#' Lua filters stored in a source package in \code{inst/rmarkdown/lua} will installed in
47+
#' \code{rmarkdown/lua} in the package path. This function finds the full path of the
48+
#' Lua filters on the system for installed packages.
49+
#'
50+
#'
51+
#' @param filters Character vector of filename for the Lua filter to retrieve in
52+
#' \code{rmarkdown/lua} folder of the package. By default, if none is
53+
#' provided, it returns the folder path.
54+
#' @param package Package name in which to look for the filters. It is set to
55+
#' \code{rmarkdown} by default
56+
#' @return Character vector of absolute file paths for the Lua filter from the
57+
#' package. If a filter is not found, it does not error. The returned path
58+
#' will already be escaped correctly using \code{\link{pandoc_path_arg}} to be
59+
#' used by Pandoc.
60+
#' @export
61+
#' @examples
62+
#' # List all Lua filters stored in the rmarkdown package
63+
#' pkg_file_lua()
64+
#' # or in a specific package
65+
#' pkg_file_lua(package = "bookdown")
66+
#' # get a specific filter
67+
#' pkg_file_lua(c("pagebreak.lua", "latex_div.lua"), package = "rmarkdown")
68+
#' # do not error if not found but return an empty path
69+
#' pkg_file_lua("donotexist.lua", package = "rmarkdown")
70+
pkg_file_lua <- function(filters = NULL, package = "rmarkdown") {
71+
if (is.null(package) || length(package) > 1)
72+
stop("One package name in which to look for Lua filters must be provided.", call. = FALSE)
73+
lua_folder <- pkg_file("rmarkdown", "lua", package = package)
74+
if (is.null(filters)) filters <- list.files(lua_folder, "[.]lua$")
75+
pandoc_path_arg(file.path(lua_folder, filters))
4676
}
4777

4878
#' @rdname rmarkdown_format
File renamed without changes.

0 commit comments

Comments
 (0)