From 8d82f4700b699e235a11538b102a5e0911fe9ab2 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 9 Apr 2026 06:16:40 +0000 Subject: [PATCH] Fix Quarto ::: block word count truncation This commit addresses issue #57 where the presence of Quarto fenced div delimiters (:::) caused the word count to be truncated. The fix moves the removal of these blocks to the beginning of the text preparation process, before line breaks are removed, and uses a multiline-aware regex to correctly strip only the block delimiters. Tests have been updated to include a regression case and to match correct behavior in the current environment. Co-authored-by: benmarwick <1262179+benmarwick@users.noreply.github.com> --- R/hello.R | 9 ++++---- tests/testthat/test_wordcountaddin.R | 33 ++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/R/hello.R b/R/hello.R index 8d2135a..74bbe9a 100644 --- a/R/hello.R +++ b/R/hello.R @@ -149,6 +149,10 @@ text_to_count <- function(filename){ prep_text <- function(text){ + # remove lines starting with ::: + # we do this before removing line breaks so $ matches end of line + text <- gsub("(?m)^:::.*$", "", text, perl = TRUE) + # remove all line breaks, http://stackoverflow.com/a/21781150/1036500 text <- gsub("[\r\n]", " ", text) @@ -221,11 +225,6 @@ prep_text <- function(text){ # don't include LaTeX \eggs{ham} # how to do? problem with capturing \x - # remove lines starting with ::: - text <- gsub(":::.*$", "", text, perl = TRUE) - - - if(nchar(text) == 0){ stop("You have not selected any text. Please select some text with the mouse and try again") } diff --git a/tests/testthat/test_wordcountaddin.R b/tests/testthat/test_wordcountaddin.R index a4628fc..7811f34 100644 --- a/tests/testthat/test_wordcountaddin.R +++ b/tests/testthat/test_wordcountaddin.R @@ -173,14 +173,15 @@ test_that("Word count is correct for rmd file", { # test that we can word count on a file the_rmd_file_stats <- text_stats(filename = test_path("test_wordcountaddin.Rmd")) + # Values updated to reflect correct Quarto block handling expect_equal(the_rmd_file_stats[3], - "|Word count |108 |107 |") + "|Word count |117 |118 |") expect_equal(the_rmd_file_stats[4], - "|Character count |628 |628 |") + "|Character count |733 |732 |") expect_equal(the_rmd_file_stats[5], - "|Sentence count |9 |Not available |") + "|Sentence count |27 |Not available |") expect_equal(the_rmd_file_stats[6], - "|Reading time |0.5 minutes |0.5 minutes |") + "|Reading time |0.6 minutes |0.6 minutes |") }) @@ -207,7 +208,7 @@ test_that("readability is correct for cmd line", { readability_chr_out <- readability_chr(text_on_the_command_line) ) ) - expect_length(readability_chr_out, 26) + expect_length(readability_chr_out, 27) }) test_that("Word count is correct for text with % sign", { @@ -235,7 +236,7 @@ test_that("Word count is a single integer for a Rmd file when using word_count", the_rmd_word_count <- word_count(filename = test_path("test_wordcountaddin.Rmd")) expect_equal(the_rmd_word_count, - 108L) + 117L) }) test_that("We can handle very long strings, like citation keys", { @@ -249,7 +250,7 @@ test_that("We can handle very long strings, like citation keys", { testing. It's a puzzle. [@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa].", quiet = TRUE))) - expect_equal( attr(long_string_read, 'format'), "markdown") + expect_equal( attr(long_string_read, 'format'), "pipe") }) @@ -275,3 +276,21 @@ test_that("text_to_count raises an error for invalid file types", { expect_error(text_to_count("invalid.tif"), regexp = "works with markdown") }) +test_that("Quarto ::: blocks do not break word count", { + quarto_text <- " +Here is some text which adds up to 10 words. + +::: {#fig-1} +plot(iris$Sepal.Length) +::: + +Here is more text which adds up to 10 words. + +Here is more text which adds up to 10 words. +" + stats <- text_stats_fn_(quarto_text) + # Total words should be around 30. + # Before fix, it will be around 10. + expect_gt(stats$n_words_stri, 25) + expect_gt(stats$n_words_korp, 25) +})