-
Notifications
You must be signed in to change notification settings - Fork 25
Bookdown #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Bookdown #32
Changes from all commits
4f1bed5
e07e72f
3660f29
368dbb7
a6f1ca4
0a12313
4554c2a
ce08bdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
|
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`' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?