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

Fix conflict between collapse=TRUE and htmlwidgets #2212

Merged
merged 9 commits into from
Feb 15, 2023
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGES IN knitr VERSION 1.43

## BUG FIXES

- The chunk option `collapse = TRUE` works with HTML widgets now (thanks, @dmurdoch, #2212).

## MINOR CHANGES

- For `.Rnw` documents, dots in figure file paths are no longer sanitized to underscores (thanks, @otoomet, #2213). Other special characters are still sanitized, but this feature can be turned off via `options(knitr.sanitize.paths = FALSE)`.
Expand Down
1 change: 1 addition & 0 deletions R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ hooks_markdown = function(strict = FALSE, fence_char = '`') {
if (isTRUE(options$collapse)) {
r = sprintf('\n([%s]{3,})\n+\\1((\\{[.])?%s[^\n]*)?\n', fence_char, tolower(options$engine))
x = gsub(r, '\n', x)
x = gsub(asis_token, '', x, fixed = TRUE)
}
x = pandoc_div(x, options[['attr.chunk']], options[['class.chunk']])
if (is.null(s <- options$indent)) return(x)
Expand Down
14 changes: 12 additions & 2 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,16 @@ sew.character = function(x, options, ...) {
knit_hooks$get('output')(x, options)
}

asis_token = '<!-- KNITR_ASIS_OUTPUT_TOKEN -->'
wrap_asis = function(x, options) {
x = as.character(x)
if ((n <- length(x)) == 0 || !out_format('markdown') || !isTRUE(options$collapse))
return(x)
x[1] = paste0(asis_token, x[1])
x[n] = paste0(x[n], asis_token)
x
}

# If you provide a custom print function that returns a character object of
# class 'knit_asis', it will be written as is.
#' @export
Expand All @@ -481,10 +491,10 @@ sew.knit_asis = function(x, options, inline = FALSE, ...) {
if (inherits(x, 'knit_asis_htmlwidget')) {
options$fig.cur = plot_counter()
options = reduce_plot_opts(options)
return(add_html_caption(options, x))
return(add_html_caption(options, wrap_asis(x, options)))
}
}
x = as.character(x)
x = wrap_asis(x, options)
if (!out_format('latex') || inline) return(x)
# latex output need the \end{kframe} trick
options$results = 'asis'
Expand Down
4 changes: 2 additions & 2 deletions tests/testit/test-hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ assert('include_graphics() includes custom images correctly', {
hook_src = knit_hooks$get("source")
options_ = list(engine = "r", prompt = FALSE, highlight = TRUE)

assert('Lengh of fences are satisfied', {
assert('Length of fences are satisfied', {
(hook_src("", options_) %==% "\n\n```r\n\n```\n\n")
(hook_src("```", options_) %==% "\n\n````r\n```\n````\n\n")
})
Expand All @@ -54,7 +54,7 @@ assert('class.source and attr.source works also with collapse = TRUE', {

hook_out = knit_hooks$get("output")

assert('Attributes for souce can be specified class.source and attr.source', {
assert('Attributes for source can be specified class.source and attr.source', {
(hook_out("1\n", c(options_, class.output = "a b")) %==%
"\n\n```{.a .b}\n1\n```\n\n")
(hook_out("1\n", c(options_, attr.output = ".a .b")) %==%
Expand Down