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

Detect malformed rmd chunks #794

Merged
merged 5 commits into from May 6, 2021
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
2 changes: 1 addition & 1 deletion NEWS.md
Expand Up @@ -55,7 +55,7 @@
within a learnr tutorial (#790).
* `#>` is recognized as an output marker and no space is added after `#` (#771).
* R code chunks in nested non-R chunks in R markdown don't yield an error
anymore when document is styled, chunks are still not styled (#788).
anymore when document is styled, chunks are still not styled (#788, #794).
* `multi_line` attribute in parse table is now integer, not boolean (#782).
* style guide used in Addin is verified when set via R option (#789).
* improve pkgdown author URLs (#775).
Expand Down
16 changes: 8 additions & 8 deletions R/transform-code.R
Expand Up @@ -72,6 +72,10 @@ separate_chunks <- function(lines, filetype) {
#' Raw in the sense that these chunks don't contain pure R code, but they
#' contain a header and footer of markdown. Only code chunks that have an engine
#' whose name matches `engine-pattern` are considered as R code.
#' For every opening, we match the next closing. If there are not the same
#' amount of closing and openings after this matching, we throw an error.
#' Similarly, if there are two openings before a closing, the closing gets
#' matched twice, on which we throw an error.
#' @inheritParams separate_chunks
#' @param engine_pattern A regular expression that must match the engine name.
#' @importFrom rlang abort
Expand All @@ -85,14 +89,10 @@ identify_raw_chunks <- function(lines, filetype, engine_pattern = get_engine_pat
if (filetype == "Rmd") {
starts <- grep("^[\t >]*```+\\s*\\{([Rr]( *[ ,].*)?)\\}\\s*$", lines, perl = TRUE)
ends <- grep("^[\t >]*```+\\s*$", lines, perl = TRUE)

if (length(starts) != length(ends)) {
# for each start, match next end, required for nested chunks
ends <- purrr::imap_int(starts, ~ ends[which(ends > .x)[1]]) %>%
na.omit()
if (length(starts) != length(ends)) {
abort("Malformed file!")
}
ends <- purrr::imap_int(starts, ~ ends[which(ends > .x)[1]]) %>%
na.omit()
if (length(starts) != length(ends) || anyDuplicated(ends) != 0) {
abort("Malformed file!")
}
} else if (filetype == "Rnw") {
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
Expand Down
4 changes: 4 additions & 0 deletions man/identify_raw_chunks.Rd

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

270 changes: 0 additions & 270 deletions tests/testthat/public-api/xyzfile_rmd/random5.Rmd

This file was deleted.

23 changes: 23 additions & 0 deletions tests/testthat/public-api/xyzfile_rmd/random7.Rmd
@@ -0,0 +1,23 @@
Some text
```{r}
# Some R code
f <- function(x) {
x
}
```
More text before malformed chunk
# More R code
g <- function(y) {
y
}
```
Final text
```{r}

```

Final text
```{r}
```{r}
1 + 2
```
17 changes: 17 additions & 0 deletions tests/testthat/rmd/random3-in.Rmd
@@ -0,0 +1,17 @@
Some text
```{r}
# Some R code
f <- function(x) {
x
}
```
More text before malformed chunk
# More R code
g <- function(y) {
y
}
```
Final text
```{r}
1 + 2
```
17 changes: 17 additions & 0 deletions tests/testthat/rmd/random3-out.Rmd
@@ -0,0 +1,17 @@
Some text
```{r}
# Some R code
f <- function(x) {
x
}
```
More text before malformed chunk
# More R code
g <- function(y) {
y
}
```
Final text
```{r}
1 + 2
```