Skip to content

Commit

Permalink
Merge changes on rstudio/rmarkdown@master.
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'rstudio_origin/master' into css-slash-fix

# By Yihui Xie (12) and others
# Via GitHub
* rstudio_origin/master: (22 commits)
  fix rstudio#1905: a chunk with the class "fold-hide" should only hide itself (rstudio#1906)
  bump version after merging rstudio#1899
  lua filter -> Lua filter
  Add an argument `lua_filters` to pandoc_options() to store them in the output format object (rstudio#1899)
  recursve into the parent dir at the end
  also suppor dir input in proj_root()
  list.files() with full.names = TRUE (so the files could be read with corret paths), and pass down the `file`/`pattern` arguments in the recursion
  use our own proj_root() instead of introducing a dependency
  rename package_root() to proj_root()
  generalize the package_root() function to work with more types of projects
  Discover site generators in index.Rmd files in parent directories (rstudio#1898)
  only install pkgdown for website build (rstudio#1900)
  Generalize number sections (rstudio#1879)
  enable figure cropping in pdf_document() only when both pdfcrop and ghostscript are found (yihui/knitr#954), and do not limit this feature to non-Windows platforms
  add the citation entry for the R Markdown Cookbook
  add lua filter vignette to pkgdown website (rstudio#1894)
  close rstudio#1889: evaluate the output argument before changing working directory in pandoc_convert() (rstudio#1890)
  tweak news
  require Pandoc >= 1.14
  xfun 0.16 is on CRAN now
  ...

# Conflicts:
#	NEWS.md
  • Loading branch information
jonathan-g committed Sep 24, 2020
2 parents 669566a + ad2c8fd commit 38812f6
Show file tree
Hide file tree
Showing 44 changed files with 445 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
- r: release
name: R release / RStudio Pandoc
env: NETLIFY_SITE_ID=5d77c13c-e2ee-4a31-87ca-2fe657196160
before_cache:
- Rscript -e 'xfun::pkg_load2("pkgdown")'
after_success:
- "[[ ${TRAVIS_BRANCH} = master ]] || exit 0"
- "[[ -z ${NETLIFY_AUTH_TOKEN} ]] && exit 0"
Expand Down
16 changes: 8 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rmarkdown
Type: Package
Title: Dynamic Documents for R
Version: 2.3.2
Version: 2.3.9
Authors@R: c(
person("JJ", "Allaire", role = "aut", email = "[email protected]"),
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
Expand All @@ -16,7 +16,9 @@ Authors@R: c(
#
# Contributors, ordered alphabetically by first name
person("Andrew", "Dunning", role = "ctb", comment = c(ORCID = "0000-0003-0464-5036")),
person("Atsushi", "Yasumoto", role = "ctb", comment = c(ORCID = "0000-0002-8335-495X")),
person("Atsushi", "Yasumoto", role = c("ctb", "cph"),
comment = c(ORCID = "0000-0002-8335-495X",
cph = "Number sections Lua filter")),
person("Barret", "Schloerke", role = "ctb"),
person("Christophe", "Dervieux", role = "ctb"),
person("Frederik", "Aust", role = "ctb", email = "[email protected]", comment = c(ORCID = "0000-0003-4900-788X")),
Expand Down Expand Up @@ -70,9 +72,9 @@ Authors@R: c(
person("Aidan", "Lister", role = c("ctb", "cph"),
comment = "jQuery StickyTabs"),
person("Benct Philip", "Jonsson", role = c("ctb", "cph"),
comment = "pagebreak lua filter"),
comment = "pagebreak Lua filter"),
person("Albert", "Krewinkel", role = c("ctb", "cph"),
comment = "pagebreak lua filter")
comment = "pagebreak Lua filter")
)
Maintainer: Yihui Xie <[email protected]>
Description: Convert R Markdown documents into a variety of formats.
Expand All @@ -85,7 +87,6 @@ Imports:
yaml (>= 2.1.19),
htmltools (>= 0.3.5),
evaluate (>= 0.13),
base64enc,
jsonlite,
mime,
tinytex (>= 0.11),
Expand All @@ -100,12 +101,11 @@ Suggests:
dygraphs,
tibble,
fs,
pkgdown,
rsconnect
SystemRequirements: pandoc (>= 1.12.3) - http://pandoc.org
SystemRequirements: pandoc (>= 1.14) - http://pandoc.org
URL: https://github.com/rstudio/rmarkdown
BugReports: https://github.com/rstudio/rmarkdown/issues
License: GPL-3
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Encoding: UTF-8
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export(pandoc_exec)
export(pandoc_highlight_args)
export(pandoc_include_args)
export(pandoc_latex_engine_args)
export(pandoc_lua_filter_args)
export(pandoc_metadata_arg)
export(pandoc_options)
export(pandoc_path_arg)
Expand Down
18 changes: 15 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
rmarkdown 2.4
================================================================================

- Fix path separators for "css" parameters in YAML frontmatter for HTML files under Windows. Previously, these were converted to backslashes even if they appeared in the "css" argument as forward slashes. Now hrefs for css links are generated with forward slashes under Windows.
- Lua filters handling has been improved internally with some user-facing changes (#1899):
- New exported function `pandoc_lua_filter_args()` to return the Pandoc command-line argument to add a Lua filter.
- 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`.
- 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).

- Fix path separators for "css" parameters in YAML frontmatter for HTML files under Windows. Previously, these were converted to backslashes even if they appeared in the "css" (thanks, @jonathan-g, #1862)

- 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=""`.

- 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).

- When customizing formats with the `output_format` function, `pre_knit`, `opts_hooks`, and `knit_hooks` can now refer to `rmarkdown::metadata`. Previously, `rmarkdown::metadata` returned `list()` in these functions (thanks, @atusy, #1855).

- 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).

- 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).

- Fixed a bug that a chunk with a class `fold-hide` hides the rest of the chunks even the output format setting `html_document(code_folding = "show")` (thanks, @atusy, #1906).


rmarkdown 2.3
================================================================================

Expand Down Expand Up @@ -146,13 +158,13 @@ rmarkdown 1.16

- Added `self_contained` argument to `html_vignette` to keep intermediate directory if `self_contained = FALSE` (thanks, @cderv, #1641).

- It is now possible to add pagebreak in HTML, Word, LaTeX, and ODT documents using the `\newpage` or `\pagebreak` command in an Rmd file. This is possible thanks to the [Pandoc's pagebreak lua filter](https://github.com/pandoc/lua-filters/tree/master/pagebreak). See `vignette("lua-filters", package = "rmarkdown")` (thanks, @cderv, #1626).
- It is now possible to add pagebreak in HTML, Word, LaTeX, and ODT documents using the `\newpage` or `\pagebreak` command in an Rmd file. This is possible thanks to the [Pandoc's pagebreak Lua filter](https://github.com/pandoc/lua-filters/tree/master/pagebreak). See `vignette("lua-filters", package = "rmarkdown")` (thanks, @cderv, #1626).

- The Pandoc extension `ascii_identifiers` is no longer enabled by default. If you still need it, you may use the argument `md_extensions = "+ascii_identifiers"` in the output format function. However, please note that this will trigger an error in a future version of Pandoc.

- Output formats can be configured by arbitrary YAML files, which used to be restricted to `_output.yml` or `_output.yaml`. They can be specified via the `output_yaml` argument of `render()` or the `output_yaml` top-level parameter of YAML front matter, and the first existing one will be used. If `output_yaml` is specified both for `render()` and YAML front matter, then `render()` has the priority. If none are found, then `_output.yml` or `_output.yaml` will be used if they exist (thanks, @atusy, #1634).

- Added a Pandoc lua filter to convert fenced Divs to LaTeX environments when the output format is `latex` or `beamer`. Basically a fenced Div `::: {.NAME data-latex="[OPTIONS]"}` is converted to `\begin{NAME}[OPTIONS] \end{NAME}` in LaTeX. The attribute `data-latex` must be provided, even if it is an empty string (meaning that the LaTeX environment does not have any optional arguments). For example, `::: {.verbatim data-latex=""}` generates a `verbatim` environment, and `::: {.minipage data-latex="{.5\textwidth}"}` generates `\begin{minipage}{.5\textwidth}`. This lua filter was originally written by @RLesur at https://github.com/yihui/bookdown-crc/issues/1. It will allow users to create custom blocks that work for both HTML and LaTeX output (e.g., info boxes or warning boxes).
- Added a Pandoc Lua filter to convert fenced Divs to LaTeX environments when the output format is `latex` or `beamer`. Basically a fenced Div `::: {.NAME data-latex="[OPTIONS]"}` is converted to `\begin{NAME}[OPTIONS] \end{NAME}` in LaTeX. The attribute `data-latex` must be provided, even if it is an empty string (meaning that the LaTeX environment does not have any optional arguments). For example, `::: {.verbatim data-latex=""}` generates a `verbatim` environment, and `::: {.minipage data-latex="{.5\textwidth}"}` generates `\begin{minipage}{.5\textwidth}`. This Lua filter was originally written by @RLesur at https://github.com/yihui/bookdown-crc/issues/1. It will allow users to create custom blocks that work for both HTML and LaTeX output (e.g., info boxes or warning boxes).

- Added `keep_html` argument to `github_document` so to save a preview HTML file in a working directory (thanks, @atusy, #1650).

Expand Down
23 changes: 2 additions & 21 deletions R/base64.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
base64_encode_file <- function(in_file, encoder) {

file_size <- file.info(in_file)$size

if (file_size <= 0) {
warning(in_file, 'is empty!')
return(in_file)
}
paste0('data:', mime::guess_type(in_file), ';base64,',
encoder(readBin(in_file, 'raw', n = file_size)))
}

# processes an HTML resource, given a regular expression that locates
# instances of that resource
process_html_res <- function(html, reg, processor) {
Expand All @@ -29,21 +17,14 @@ process_images <- function(html, processor) {
processor)
}

base64_encode_images <- function(html, encoder) {
base64_encode_images <- function(html) {
base64_encode_img <- function(img_src, src) {
in_file <- utils::URLdecode(src)
if (length(in_file) && file.exists(in_file)) {
img_src <- sub(src, base64_encode_file(in_file, encoder), img_src,
fixed = TRUE)
img_src <- sub(src, xfun::base64_uri(in_file), img_src, fixed = TRUE)
}
img_src
}
html <- process_images(html, base64_encode_img)
process_html_res(html, "<[^>]*style=\"[^\"]*url\\(([^\\)]+)\\)", base64_encode_img)
}

base64_image_encode <- function(data) {
base64_encode_images(data, base64enc::base64encode)
}


15 changes: 8 additions & 7 deletions R/beamer_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ beamer_presentation <- function(toc = FALSE,
# make sure the graphics package is always loaded
if (identical(template, "default")) args <- c(args, "--variable", "graphics=yes")

args <- c(args, pandoc_lua_filters(c("pagebreak.lua", "latex-div.lua")))

# custom args
args <- c(args, pandoc_args)

Expand All @@ -151,11 +149,14 @@ beamer_presentation <- function(toc = FALSE,
# return format
output_format(
knitr = knitr_options_pdf(fig_width, fig_height, fig_crop, dev),
pandoc = pandoc_options(to = "beamer",
from = from_rmarkdown(fig_caption, md_extensions),
args = args,
latex_engine = latex_engine,
keep_tex = keep_tex),
pandoc = pandoc_options(
to = "beamer",
from = from_rmarkdown(fig_caption, md_extensions),
args = args,
latex_engine = latex_engine,
keep_tex = keep_tex,
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))
),
pre_processor = pre_processor,
intermediates_generator = intermediates_generator,
clean_supporting = !keep_tex,
Expand Down
15 changes: 7 additions & 8 deletions R/context_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ context_document <- function(toc = FALSE,
# content includes
args <- c(args, includes_to_pandoc_args(includes))

# lua filters (added if pandoc > 2)
args <- c(args, pandoc_lua_filters("pagebreak.lua"))

# args args
args <- c(args, pandoc_args)

Expand Down Expand Up @@ -129,11 +126,13 @@ context_document <- function(toc = FALSE,
# return format
output_format(
knitr = knitr_options_pdf(fig_width, fig_height, fig_crop, dev),
pandoc = pandoc_options(to = paste(c("context", output_extensions), collapse = ""),
from = from_rmarkdown(fig_caption, md_extensions),
args = args,
keep_tex = FALSE,
ext = ext),
pandoc = pandoc_options(
to = paste(c("context", output_extensions), collapse = ""),
from = from_rmarkdown(fig_caption, md_extensions),
args = args,
keep_tex = FALSE,
ext = ext,
lua_filters = pkg_file_lua("pagebreak.lua")),
clean_supporting = !isTRUE(keep_tex),
keep_md = keep_md,
df_print = df_print,
Expand Down
12 changes: 8 additions & 4 deletions R/github_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#' @export
github_document <- function(toc = FALSE,
toc_depth = 3,
number_sections = FALSE,
fig_width = 7,
fig_height = 5,
dev = 'png',
Expand All @@ -30,20 +31,23 @@ github_document <- function(toc = FALSE,
keep_html = FALSE) {

# add special markdown rendering template to ensure we include the title fields
# and add an optional feature to number sections
pandoc_args <- c(
pandoc_args, "--template", pkg_file_arg(
"rmarkdown/templates/github_document/resources/default.md")
pandoc_args, "--template",
pkg_file_arg("rmarkdown/templates/github_document/resources/default.md")
)


pandoc2 <- pandoc2.0()
# use md_document as base
variant <- if (pandoc2) "gfm" else "markdown_github"
if (!hard_line_breaks) variant <- paste0(variant, "-hard_line_breaks")

format <- md_document(
variant = variant, toc = toc, toc_depth = toc_depth,
fig_width = fig_width, fig_height = fig_height, dev = dev,
df_print = df_print, includes = includes, md_extensions = md_extensions,
number_sections = number_sections, fig_width = fig_width,
fig_height = fig_height, dev = dev, df_print = df_print,
includes = includes, md_extensions = md_extensions,
pandoc_args = pandoc_args
)

Expand Down
2 changes: 1 addition & 1 deletion R/html_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ html_document <- function(toc = FALSE,
source_file <<- basename(input)
source_code <<- paste0(
'<div id="rmd-source-code">',
base64enc::base64encode(input),
xfun::base64_encode(input),
'</div>')
}
}
Expand Down
7 changes: 3 additions & 4 deletions R/html_document_base.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ html_document_base <- function(theme = NULL,

preserved_chunks <<- extract_preserve_chunks(input_file)

# a lua filters added if pandoc2.0
args <- c(args, pandoc_lua_filters(c("pagebreak.lua", "latex-div.lua")))

args
}

Expand Down Expand Up @@ -170,7 +167,9 @@ html_document_base <- function(theme = NULL,

output_format(
knitr = NULL,
pandoc = pandoc_options(to = "html", from = NULL, args = args),
pandoc = pandoc_options(
to = "html", from = NULL, args = args,
lua_filters = pkg_file_lua(c("pagebreak.lua", "latex-div.lua"))),
keep_md = FALSE,
clean_supporting = FALSE,
pre_knit = pre_knit,
Expand Down
2 changes: 1 addition & 1 deletion R/html_notebook.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ parse_html_notebook <- function(path) {
if (!identical(c(matches), -1L)) {
start <- c(attr(matches, "capture.start"))
end <- start + c(attr(matches, "capture.length")) - 1
decoded <- rawToChar(base64enc::base64decode(substring(line, start, end)))
decoded <- rawToChar(xfun::base64_decode(substring(line, start, end)))
rmd_contents <- strsplit(decoded, "\\r?\\n", perl = TRUE)[[1]]
next
}
Expand Down
2 changes: 1 addition & 1 deletion R/html_notebook_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ html_notebook_render_base64_data <- function(path = NULL,
# read (if necessary) and encode data
if (is.null(bytes))
bytes <- read_file(path, binary = TRUE)
encoded <- base64enc::base64encode(bytes)
encoded <- xfun::base64_encode(bytes)

# generate html attributes
sprintf(format, to_html_attributes(attributes), encoded)
Expand Down
9 changes: 7 additions & 2 deletions R/ioslides_presentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@
#' To create a PDF version of a presentation you can use Print to PDF
#' from Google Chrome.
#' @export
ioslides_presentation <- function(logo = NULL,
ioslides_presentation <- function(number_sections = FALSE,
logo = NULL,
slide_level = 2,
incremental = FALSE,
fig_width = 7.5,
Expand Down Expand Up @@ -343,6 +344,10 @@ ioslides_presentation <- function(logo = NULL,
# add any custom pandoc args
args <- c(args, pandoc_args)

# number sections
if (number_sections)
args <- c(args, pandoc_lua_filter_args(pkg_file_lua("number-sections.lua")))

lua_writer <- file.path(dirname(input_file), "ioslides_presentation.lua")
# The input directory may not be writable (on e.g. Shiny Server), so write
# to the output directory in this case. We don't always do this since
Expand Down Expand Up @@ -412,7 +417,7 @@ ioslides_presentation <- function(logo = NULL,

# base64 encode if needed
if (self_contained) {
slides_lines <- base64_image_encode(slides_lines)
slides_lines <- base64_encode_images(slides_lines)
}

# read the output file
Expand Down
11 changes: 7 additions & 4 deletions R/md_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ md_document <- function(variant = "markdown_strict",
preserve_yaml = FALSE,
toc = FALSE,
toc_depth = 3,
number_sections = FALSE,
fig_width = 7,
fig_height = 5,
fig_retina = NULL,
Expand Down Expand Up @@ -75,10 +76,12 @@ md_document <- function(variant = "markdown_strict",
# return format
output_format(
knitr = knitr_options_html(fig_width, fig_height, fig_retina, FALSE, dev),
pandoc = pandoc_options(to = variant,
from = from_rmarkdown(extensions = md_extensions),
args = args,
ext = ext),
pandoc = pandoc_options(
to = variant,
from = from_rmarkdown(extensions = md_extensions),
args = args,
ext = ext,
lua_filters = if (number_sections) pkg_file_lua("number-sections.lua")),
clean_supporting = FALSE,
df_print = df_print,
post_processor = post_processor
Expand Down
15 changes: 8 additions & 7 deletions R/odt_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#' render("input.Rmd", odt_document(highlight = "zenburn"))
#' }
#' @export
odt_document <- function(fig_width = 5,
odt_document <- function(number_sections = FALSE,
fig_width = 5,
fig_height = 4,
fig_caption = TRUE,
template = "default",
Expand Down Expand Up @@ -62,9 +63,6 @@ odt_document <- function(fig_width = 5,
# reference odt
args <- c(args, reference_doc_args("odt", reference_odt))

# lua filters (added if pandoc > 2)
args <- c(args, pandoc_lua_filters("pagebreak.lua"))

# pandoc args
args <- c(args, pandoc_args)

Expand All @@ -81,9 +79,12 @@ odt_document <- function(fig_width = 5,
# return output format
output_format(
knitr = knitr,
pandoc = pandoc_options(to = "odt",
from = from_rmarkdown(fig_caption, md_extensions),
args = args),
pandoc = pandoc_options(
to = "odt",
from = from_rmarkdown(fig_caption, md_extensions),
args = args,
lua_filters = pkg_file_lua(c(
"pagebreak.lua", if (number_sections) "number-sections.lua"))),
keep_md = keep_md,
pre_processor = pre_processor,
intermediates_generator = intermediates_generator
Expand Down
Loading

0 comments on commit 38812f6

Please sign in to comment.