Skip to content

Commit

Permalink
fix #2213: no longer sanitize dots in figure file paths, and provide …
Browse files Browse the repository at this point in the history
…an option to turn off sanitization
  • Loading branch information
yihui committed Feb 13, 2023
1 parent 77970b0 commit a007b37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.42.1
Version: 1.42.2
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGES IN knitr VERSION 1.43

## MINOR CHANGES

- For `.Rnw` documents, dots in figure file paths are no longer sanitized to underscores (thanks, @otoomet, #2213). Other special characters are still sanitized, but this feature can be turned off via `options(knitr.sanitize.paths = FALSE)`.

# CHANGES IN knitr VERSION 1.42

Expand Down
25 changes: 9 additions & 16 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -490,24 +490,17 @@ fig_path = function(suffix = '', options = opts_current$get(), number) {
if (missing(number)) number = options$fig.cur %n% 1L
if (!is.null(number)) suffix = paste0('-', number, suffix)
path = valid_path(options$fig.path, options$label)
(if (out_format(c('latex', 'sweave', 'listings'))) sanitize_fn else
paste0)(path, suffix)
if (getOption('knitr.sanitize.paths', out_format(c('latex', 'sweave', 'listings'))))
path = sanitize_fn(path)
paste0(path, suffix)
}
# sanitize filename for LaTeX
sanitize_fn = function(path, suffix = '') {
if (grepl('[^~:_./\\[:alnum:]-]', path)) {
warning('replaced special characters in figure filename "', path, '" -> "',
path <- gsub('[^~:_./\\[:alnum:]-]', '_', path), '"')
}
# replace . with _ except ../ and ./
s = strsplit(path, '[/\\\\]')[[1L]]
i = (s != '.') & (s != '..') & grepl('\\.', s)
if (any(i)) {
s[i] = gsub('\\.', '_', s[i])
path = paste(s, collapse = '/')
warning('dots in figure paths replaced with _ ("', path, '")')
}
paste0(path, suffix)
sanitize_fn = function(path, warn = TRUE) {
p = gsub('[^~:_./\\[:alnum:]-]', '_', path)
if (warn && (p != path)) warning(
"Replaced special characters in figure filename '", path, "' -> '", p, "'"
)
p
}

#' Obtain the figure filenames for a chunk
Expand Down
14 changes: 6 additions & 8 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,12 @@ assert('sanitize_fn() warns against spaces in filenames', {
options(op)

assert('fig_path() sanitizes paths', {
(identical(sanitize_fn('fig/foo', '.png'), 'fig/foo.png'))
(suppressWarnings(c(
identical(sanitize_fn('figure/a b'), 'figure/a_b'),
identical(sanitize_fn('fig space/a.b'), 'fig_space/a_b'),
identical(sanitize_fn('../c.d'), '../c_d'),
identical(sanitize_fn('./../c..d'), './../c__d')
)))
(identical(sanitize_fn('C:/foo/bar'), 'C:/foo/bar'))
(sanitize_fn('fig/foo') %==% 'fig/foo')
(sanitize_fn('figure/a b c', FALSE) %==% 'figure/a_b__c')
(sanitize_fn('fig space/a.b') %==% 'fig_space/a.b')
(sanitize_fn('../c.d') %==% '../c.d')
(sanitize_fn('./../c..d') %==% './../c..d')
(sanitize_fn('C:/foo/bar') %==% 'C:/foo/bar')
})

assert('fig_chunk() generates figure filenames for a code chunk', {
Expand Down

0 comments on commit a007b37

Please sign in to comment.