Skip to content

Commit

Permalink
add check_comments to R/spin.R re #yihui#1801
Browse files Browse the repository at this point in the history
  • Loading branch information
dewittpe committed Jan 24, 2020
1 parent ab191b0 commit 1c9004d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Authors@R: c(
person("Niels Richard", "Hansen", role = "ctb"),
person("Noam", "Ross", role = "ctb"),
person("Obada", "Mahdi", role = "ctb"),
person("Peter", "DeWitt", role = "ctb"),
person("Qiang", "Li", role = "ctb"),
person("Ramnath", "Vaidyanathan", role = "ctb"),
person("Richard", "Cotton", role = "ctb"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

- By default, `include_graphics(files)` will signal an error if any `files` do not exist and are not web resources. To avoid the error (e.g., if you think it is a false positive), use `include_graphics(..., error = FALSE)` (thanks, @hadley, #1717).

## BUG FIXES

- Check for the correct ordering of start/end deliminators of comments for spin files (#1801)

# CHANGES IN knitr VERSION 1.27

## NEW FEATURES
Expand Down
48 changes: 46 additions & 2 deletions R/spin.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ spin = function(
x = if (nosrc <- is.null(text)) read_utf8(hair) else split_lines(text)
stopifnot(length(comment) == 2L)
c1 = grep(comment[1], x); c2 = grep(comment[2], x)
if (length(c1) != length(c2))
stop('comments must be put in pairs of start and end delimiters')
check_comments(c1, c2)
# remove comments
if (length(c1)) x = x[-unique(unlist(mapply(seq, c1, c2, SIMPLIFY = FALSE)))]

Expand Down Expand Up @@ -183,3 +182,48 @@ spin_child = function(input, format) {
quiet = TRUE
))
}

#' Check Comments
#'
#' A more robust check for open/close matching sets of comments in a spin file.
#'
#' @param c1 index (line numbers) for the start delimiter of comments
#' @param c1 index (line numbers) for the end delimiter of comments
#'
check_comments <- function(c1, c2) {

cs <- sort(c(openers = c1, closers = c2))
err <- FALSE
notes <- character()

while(length(cs)) {
if (grepl("closer", names(cs)[1])) {
notes <- append(notes, paste0(" unopened comment; closed on line ", cs[1]))
cs <- cs[-1]
err <- TRUE
}

i <- 1
while(i < length(cs)) {
if (grepl("opener", names(cs)[i]) & grepl("closer", names(cs)[i + 1])) {
notes <- append(notes, paste0(" opened on line ", cs[i], "; closed on line ", cs[i + 1]))
cs <- cs[-c(i, i+1)]
} else {
i <- i + 1
}
}

if (length(cs) == 1L & grepl("opener", names(cs)[1])) {
notes <- append(notes, paste0(" opened on line ", cs[1], "; unclosed"))
cs <- cs[-1]
err <- TRUE
}
}

if (err) {
stop(paste('comments must be put in pairs of start and end delimiters.\n', paste(notes, collapse = '\n'), collapse = "\n"),
call. = FALSE)
}
invisible(notes)
}

0 comments on commit 1c9004d

Please sign in to comment.