From 92490d10b15502ebdfbd9c5e5c2daf3c8c57b30c Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Thu, 24 Nov 2022 14:40:27 -0500 Subject: [PATCH 1/5] Allow concordances to be embedded in Markdown output that will be processed into HTML. --- R/concordance.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/concordance.R b/R/concordance.R index af5a7d0050..9c744cbaab 100644 --- a/R/concordance.R +++ b/R/concordance.R @@ -8,7 +8,8 @@ knit_concord = new_defaults(list( # do not consider child mode for concordance concord_mode = function() { opts_knit$get('concordance') && !child_mode() && - out_format(c('latex', 'sweave', 'listings')) + (out_format(c('latex', 'sweave', 'listings')) || + out_format('markdown') && is_html_output()) } current_lines = function(i) { @@ -44,6 +45,10 @@ concord_gen = function(infile, outfile) { confile = paste(sans_ext(outfile), 'concordance.tex', sep = '-') # write to file - cat('\\Sconcordance{concordance:', outfile, ':', infile, ':%\n', + if (out_format('markdown') && is_html_output()) + cat('\n', sep = '', file = outfile, append = TRUE) + else + cat('\\Sconcordance{concordance:', outfile, ':', infile, ':%\n', concordance, '}\n', sep = '', file = confile) } From fd7fd44e60d968ca7635f30e204a2ab067f42e85 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Fri, 25 Nov 2022 19:43:10 -0500 Subject: [PATCH 2/5] Handle pdf_document as well. --- R/concordance.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/concordance.R b/R/concordance.R index 9c744cbaab..e5d1eb5598 100644 --- a/R/concordance.R +++ b/R/concordance.R @@ -8,8 +8,7 @@ knit_concord = new_defaults(list( # do not consider child mode for concordance concord_mode = function() { opts_knit$get('concordance') && !child_mode() && - (out_format(c('latex', 'sweave', 'listings')) || - out_format('markdown') && is_html_output()) + out_format(c('latex', 'sweave', 'listings', 'markdown')) } current_lines = function(i) { @@ -45,7 +44,7 @@ concord_gen = function(infile, outfile) { confile = paste(sans_ext(outfile), 'concordance.tex', sep = '-') # write to file - if (out_format('markdown') && is_html_output()) + if (is_html_output()) cat('\n', sep = '', file = outfile, append = TRUE) else From 11e2311f61709b8ded9bac620c34d4790edea866 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Sat, 26 Nov 2022 05:59:21 -0500 Subject: [PATCH 3/5] better test for concord_mode --- R/concordance.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/concordance.R b/R/concordance.R index e5d1eb5598..d28fe49576 100644 --- a/R/concordance.R +++ b/R/concordance.R @@ -8,7 +8,7 @@ knit_concord = new_defaults(list( # do not consider child mode for concordance concord_mode = function() { opts_knit$get('concordance') && !child_mode() && - out_format(c('latex', 'sweave', 'listings', 'markdown')) + (is_html_output() || is_latex_output()) } current_lines = function(i) { @@ -42,12 +42,13 @@ concord_gen = function(infile, outfile) { vals = c(1L, as.numeric(rbind(vals$lengths, vals$values))) concordance = paste(strwrap(paste(vals, collapse = ' ')), collapse = ' %\n') - confile = paste(sans_ext(outfile), 'concordance.tex', sep = '-') # write to file if (is_html_output()) cat('\n', sep = '', file = outfile, append = TRUE) - else + else if (is_latex_output()) { + confile = paste(sans_ext(outfile), 'concordance.tex', sep = '-') cat('\\Sconcordance{concordance:', outfile, ':', infile, ':%\n', concordance, '}\n', sep = '', file = confile) + } } From 56513b22117bd28cae9f1f66d417b958c2ccf356 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 18 Jan 2023 17:03:27 -0600 Subject: [PATCH 4/5] remove the checks in concord_mode() since they will be done in concord_gen() --- R/concordance.R | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/R/concordance.R b/R/concordance.R index d28fe49576..6c7c6e6546 100644 --- a/R/concordance.R +++ b/R/concordance.R @@ -7,8 +7,7 @@ knit_concord = new_defaults(list( # do not consider child mode for concordance concord_mode = function() { - opts_knit$get('concordance') && !child_mode() && - (is_html_output() || is_latex_output()) + opts_knit$get('concordance') && !child_mode() } current_lines = function(i) { @@ -43,10 +42,12 @@ concord_gen = function(infile, outfile) { concordance = paste(strwrap(paste(vals, collapse = ' ')), collapse = ' %\n') # write to file - if (is_html_output()) - cat('\n', sep = '', file = outfile, append = TRUE) - else if (is_latex_output()) { + if (is_html_output()) { + cat( + '\n', + sep = '', file = outfile, append = TRUE + ) + } else if (is_latex_output()) { confile = paste(sans_ext(outfile), 'concordance.tex', sep = '-') cat('\\Sconcordance{concordance:', outfile, ':', infile, ':%\n', concordance, '}\n', sep = '', file = confile) From 7b7323bf3f854ba8c5263bcd10e42a2b04b525c9 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Wed, 18 Jan 2023 17:05:32 -0600 Subject: [PATCH 5/5] add news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index a6af834911..309722988a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,8 @@ - The default text progress bar is still written to `stdout()` by default, but can also be written to other connections or `stderr()` now. To do so, set `options(knitr.progress.output = )` to a connection or `stderr()`. +- Allow concordances to be embedded in HTML output (thanks, @dmurdoch, #2200). + ## MAJOR CHANGES - `knit()` no longer prints out chunk options beneath the text progress bar while knitting a document (thanks, @hadley, #1880).