Skip to content

Commit

Permalink
fix #2332: pass knitr::asis_output() to the output hook (#2365)
Browse files Browse the repository at this point in the history
then every `output` hook should check if the chunk option `results == 'asis'`
  • Loading branch information
yihui authored Sep 20, 2024
1 parent 607f98c commit 7e9afd1
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- In-chunk references of the form `<<label>>` should not be resolved if `label` is not found in the document (thanks, @jennybc @gadenbuie, #2360).

- `asis_output()` was not passed to the `output` hook (thanks, @cderv, #2332).

## MAJOR CHANGES

- Unbalanced chunk delimiters (fences) in R Markdown documents are strictly prohibited now.
Expand Down
4 changes: 3 additions & 1 deletion R/hooks-asciidoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ hooks_asciidoc = function() {
hook.error = function(x, options) {
sprintf('\n[CAUTION]\n====\n.Error\n%s\n====\n', gsub('^.*Error: ', '', x))
}
hook.output = function(x, options) sprintf('\n----\n%s----\n', x)
hook.output = function(x, options) {
if (output_asis(x, options)) x else sprintf('\n----\n%s----\n', x)
}
list(
source = hook.source, output = hook.output, message = hook.message,
warning = hook.warning, error = hook.error, plot = hook_plot_asciidoc
Expand Down
1 change: 1 addition & 0 deletions R/hooks-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ hooks_html = function() {
hook = function(name) {
force(name)
function(x, options) {
if (name == 'output' && output_asis(x, options)) return(x)
x = if (name == 'source') {
c(hilight_source(x, 'html', options), '')
} else escape_html(x)
Expand Down
6 changes: 5 additions & 1 deletion R/hooks-md.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ hooks_markdown = function(strict = FALSE, fence_char = '`') {
hook.o = function(class) {
force(class)
function(x, options) {
if (class == 'output' && output_asis(x, options)) return(x)
hook.t(x, options[[paste0('attr.', class)]], options[[paste0('class.', class)]])
}
}
Expand Down Expand Up @@ -290,6 +291,9 @@ hooks_jekyll = function(highlight = c('pygments', 'prettify', 'none'), extra = '
hook.r(x, options)
}
merge_list(hook.m, list(
source = source, output = hook.t, warning = hook.t, message = hook.t, error = hook.t
source = source, warning = hook.t, message = hook.t, error = hook.t,
output = function(x, options) {
if (output_asis(x, options)) x else hook.t(x, options)
}
))
}
4 changes: 3 additions & 1 deletion R/hooks-rst.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ hooks_rst = function(strict = FALSE) {
(if (strict) hook.s else hook.t)(x, options)
},
warning = hook.s, error = hook.s, message = hook.s,
output = hook.s, inline = hook.i, plot = hook_plot_rst
inline = hook.i, plot = hook_plot_rst, output = function(x, options) {
if (output_asis(x, options)) x else hook.s(x, options)
}
)
}

Expand Down
1 change: 1 addition & 0 deletions R/hooks-textile.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hooks_textile = function() {
function(x, options) {
if (name == 'source') x = c(hilight_source(x, 'textile', options), '')
x = one_string(x)
if (name == 'output' && output_asis(x, options)) return(x)
sprintf('bc(knitr %s %s#%s).. %s\np(knitr_end). \n\n',
tolower(options$engine), name, options$label, x)
}
Expand Down
3 changes: 1 addition & 2 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ sew.knit_asis = function(x, options, inline = FALSE, ...) {
}
}
x = wrap_asis(x, options)
if (!out_format('latex') || inline) return(x)
# latex output need the \end{kframe} trick
if (inline) return(x)
options$results = 'asis'
knit_hooks$get('output')(x, options)
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ set_parent = function(parent) {

# whether to write results as-is?
output_asis = function(x, options) {
is_blank(x) || options$results == 'asis'
is_blank(x) || identical(options$results, 'asis')
}

# the working directory: use root.dir if specified, otherwise the dir of the
Expand Down

0 comments on commit 7e9afd1

Please sign in to comment.