Skip to content
Open
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(print,summary_spellcheck)
export(get_wordlist)
export(spell_check_bookdown)
export(spell_check_files)
export(spell_check_package)
export(spell_check_setup)
Expand Down
103 changes: 103 additions & 0 deletions R/spell-check-bookdown.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' Bookdown Spell Checking
#'
#' Automatically spell-check bookdown source files.
#'
#' Parses and checks bookdown source files, and if these files are present
#' * README.md (when there's no README.Rmd)
#' * NEWS.md
#' * text fields in the `DESCRIPTION` file if present.
#'
#' The preferred spelling language (typically `en-GB` or `en-US`) should be
#' * specified in the `Language` field from your package `DESCRIPTION`,
#' * or entered as the `lang` argument.
#' To whitelist custom words
#' use the bookdown [WORDLIST][get_wordlist] file which will be added to the dictionary
#' when spell checking. See [update_wordlist] to automatically populate and update this
#' file.
#'
#' Hunspell includes dictionaries for `en_US` and `en_GB` by default. Other languages
#' require installation of a custom dictionary, see [hunspell][hunspell::hunspell] for details.
#'
#' @export
#' @rdname spell_check_bookdown
#' @name spell_check_bookdown
#' @aliases spelling
#' @family spelling
#' @param path path to package root directory containing the `DESCRIPTION` file
#' @param use_wordlist ignore words in the package [WORDLIST][get_wordlist] file
#' @param lang set `Language` field in `DESCRIPTION` e.g. `"en-US"` or `"en-GB"`.
#' For supporting other languages, see the [hunspell vignette](https://docs.ropensci.org/hunspell/articles/intro.html#hunspell-dictionaries).
spell_check_bookdown <- function(path = ".", lang = NULL, use_wordlist = TRUE){

# Get language
if (is.null(lang)) {
if (file.exists(file.path(path, "DESCRIPTION"))){
pkg <- as_package(path)

# Get language from DESCRIPTION
lang <- normalize_lang(pkg$language)
} else {
lang <- suppressMessages(normalize_lang(NULL))
}
} else {
lang <- normalize_lang(lang)
}

# Add custom words to the ignore list

add_words <- if(isTRUE(use_wordlist))
get_wordfile(path)

if (file.exists(file.path(path, "DESCRIPTION"))){
pkg <- as_package(path)
author <- if(length(pkg[['authors@r']])){
parse_r_field(pkg[['authors@r']])
} else {
if ("author" %in% names(pkg)) {
strsplit(pkg[['author']], " ", fixed = TRUE)[[1]]
} else{
author <- NULL
}
}

meta <- c(pkg$package, author)
} else {
meta <- NULL
}
ignore <- unique(c(meta, hunspell::en_stats, add_words))

# Create the hunspell dictionary object
dict <- hunspell::dictionary(lang, add_words = sort(ignore))

# Where to check for rmd/md files
bookdown_files <- list.files(file.path(path), pattern = "\\.rmd$",
ignore.case = TRUE, full.names = TRUE, recursive = TRUE)
root_files <- list.files(file.path(path), pattern = "(readme|news|changes).r?md",
ignore.case = TRUE, full.names = TRUE)

# Markdown files
md_files <- sort(normalizePath(c(root_files, bookdown_files)))
md_lines <- lapply(md_files, spell_check_file_md, dict = dict)

all_sources <- md_files
all_lines <- md_lines

# Check 'DESCRIPTION' fields
if (file.exists(file.path(path, "DESCRIPTION"))){
pkg_fields <- c("title", "description")
pkg <- as_package(path)
pkg_lines <- lapply(pkg_fields, function(x){
if (x %in% names(pkg)) {
spell_check_file_text(textConnection(pkg[[x]]), dict = dict)
} else {
spell_check_file_text(textConnection(""), dict = dict)
}
})

all_sources <- c(all_sources, pkg_fields)
all_lines <- c(all_lines, pkg_lines)
}

summarize_words(all_sources, all_lines)
}

1 change: 1 addition & 0 deletions R/spell-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ as_package <- function(pkg){
} else {
normalizePath(file.path(path, "DESCRIPTION"), mustWork = TRUE)
}

pkg <- read.dcf(description)[1,]
Encoding(pkg) = "UTF-8"
pkg <- as.list(pkg)
Expand Down
11 changes: 5 additions & 6 deletions R/wordlist.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#' @family spelling
#' @export
#' @param confirm show changes and ask confirmation before adding new words to the list
#' @param pkg path to package (or bookdown folder)
#' @inheritParams spell_check_package
update_wordlist <- function(pkg = ".", vignettes = TRUE, confirm = TRUE){
pkg <- as_package(pkg)
wordfile <- get_wordfile(pkg$path)
old_words <- sort(get_wordlist(pkg$path))
new_words <- sort(spell_check_package(pkg$path, vignettes = vignettes, use_wordlist = FALSE)$word)
wordfile <- get_wordfile(pkg)
old_words <- sort(get_wordlist(pkg))
new_words <- sort(spell_check_package(pkg, vignettes = vignettes, use_wordlist = FALSE)$word)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how could one make this bookdown-compatible too? another update_wordlist() function?

if(isTRUE(all.equal(old_words, new_words))){
cat(sprintf("No changes required to %s\n", wordfile))
} else {
Expand Down Expand Up @@ -51,8 +51,7 @@ update_wordlist <- function(pkg = ".", vignettes = TRUE, confirm = TRUE){
#' @rdname wordlist
#' @export
get_wordlist <- function(pkg = "."){
pkg <- as_package(pkg)
wordfile <- get_wordfile(pkg$path)
wordfile <- get_wordfile(pkg)
out <- if(file.exists(wordfile))
unlist(strsplit(readLines(wordfile, warn = FALSE, encoding = "UTF-8"), " ", fixed = TRUE))
as.character(out)
Expand Down
11 changes: 11 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
reference:
- title: Utilities
desc: ~
contents:
- '`spell_check_files`'
- '`wordlist`'
- title: Convenience functions
desc: ~
contents:
- '`spell_check_package`'
- '`spell_check_bookdown`'
46 changes: 46 additions & 0 deletions man/spell_check_bookdown.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/spell_check_files.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/spell_check_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/wordlist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.