Skip to content
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

fixes #252: Allow plain-code-blocks in RMarkdown #349

Merged
merged 3 commits into from
Sep 18, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
avoid reading and pre-processing of ignored files (@mwaldstein)
* Allow for any number of `#` to start a comment. Useful in ESS (#299, @prosoitos)
* New equals_na_linter() (#143, #326, @jabranham)
* Fixed plain-code-block bug in Rmarkdown (#252, @russHyde)

# lintr 1.0.2 #
* Fix tests to work with upcoming testthat release.
Expand Down
31 changes: 30 additions & 1 deletion R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ extract_r_source <- function(filename, lines) {
}

starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
ends <- filter_chunk_end_positions(
starts = starts,
ends = grep(pattern$chunk.end, lines, perl = TRUE)
)

# no chunks found, so just return the lines
if (length(starts) == 0 || length(ends) == 0) {
Expand Down Expand Up @@ -46,6 +49,32 @@ get_knitr_pattern <- function(filename, lines) {
}
}

filter_chunk_end_positions <- function(starts, ends) {
# In a valid file, possibly with plain-code-blocks,
# - there should be at least as many ends as starts,
# and there should be an even-number of extra ends (possibly zero)
# since each plain-code-block should open & close, and the open/close
# tags of a plain-code-block both match the chunk.end pattern

# starts (1, 3, 5, 7, 11) --> (1, 3, 5, 7, 11)
# ends (2, 4, 6, 8, 9, 10, 12) --> (2, 4, 6, 8, 12) # return this
length_difference <- length(ends) - length(starts)

if(length_difference < 0 | length_difference %% 2 != 0) {
stop("Malformed file!", call. = FALSE)
}
if (length_difference == 0 && all(ends > starts)) {
return(ends)
}

positions <- sort(c(starts = starts, ends = ends))
code_start_indexes <- which(grepl("starts", names(positions)))
code_ends <- positions[1 + code_start_indexes]

stopifnot(all(grepl("ends", names(code_ends))))
code_ends
}

replace_prefix <- function(lines, prefix_pattern) {
if (is.null(prefix_pattern)) {
return(lines)
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/knitr_formats/test.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ a=[]

a[0]=1
```

```
Plain code blocks can be written after three or more backticks
- R Markdown: The Definitive Guide. Xie, Allaire and Grolemund (2.5.2)
```