Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions R/hello.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
}
Expand Down
33 changes: 26 additions & 7 deletions tests/testthat/test_wordcountaddin.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 |")
})


Expand All @@ -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", {
Expand Down Expand Up @@ -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", {
Expand All @@ -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")

})

Expand All @@ -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)
})
Loading