Skip to content

Commit

Permalink
fix #2332: pass knitr::asis_output() to the output hook, and every …
Browse files Browse the repository at this point in the history
…`output` hook should check if the chunk option `results == 'asis'`
  • Loading branch information
yihui committed Sep 19, 2024
1 parent 607f98c commit 9d80301
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
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

0 comments on commit 9d80301

Please sign in to comment.