Skip to content

Commit

Permalink
Merge pull request #788 from lorenzwalthert/issue-787
Browse files Browse the repository at this point in the history
- Ignore non-R code chunks for styling (#787).
  • Loading branch information
lorenzwalthert committed May 2, 2021
2 parents 8089254 + f4390eb commit 85435e3
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 20 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -50,6 +50,8 @@
## Minor changes and fixes

* `#>` 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).
* `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
29 changes: 16 additions & 13 deletions R/transform-code.R
Expand Up @@ -83,25 +83,28 @@ identify_raw_chunks <- function(lines, filetype, engine_pattern = get_engine_pat
}

if (filetype == "Rmd") {
chunks <- grep("^[\t >]*```+\\s*", lines, perl = TRUE)
starts <- odd(chunks)
ends <- even(chunks)
is_r_code <- grepl(
paste0("^[\t >]*```+\\s*\\{\\s*", engine_pattern, "[\\s\\},]"),
lines[starts],
perl = TRUE
)
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!")
}
}
} else if (filetype == "Rnw") {
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
is_r_code <- rep(TRUE, length(starts))
if (length(starts) != length(ends)) {
abort("Malformed file!")
}
}

if (length(starts) != length(ends)) {
abort("Malformed file!")
}

list(starts = starts[is_r_code], ends = ends[is_r_code])

list(starts = starts, ends = ends)
}

#' What's the engine pattern for rmd code chunks?
Expand Down
270 changes: 270 additions & 0 deletions tests/testthat/public-api/xyzfile_rmd/random5.Rmd
@@ -0,0 +1,270 @@
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}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```
Final text
```{r}
```{r}
1 + 2
```
20 changes: 20 additions & 0 deletions tests/testthat/public-api/xyzfile_rmd/random6.Rmd
@@ -0,0 +1,20 @@
Some text
```{r}
# Some R code
f <- function(x) {
x
}
```


Final text
```{r}
33
```


Final text
```{SQL}
```{python}
1 + 2
```
18 changes: 18 additions & 0 deletions tests/testthat/rmd/nested-2-in.Rmd
@@ -0,0 +1,18 @@
---
title: x

---
this

```{r}
1 +1 #co
```


````md
`r ""` ```{r, highlight.output=c(1, 3)}
head(iris)
```
````

More text
18 changes: 18 additions & 0 deletions tests/testthat/rmd/nested-2-out.Rmd
@@ -0,0 +1,18 @@
---
title: x

---
this

```{r}
1 + 1 # co
```


````md
`r ""` ```{r, highlight.output=c(1, 3)}
head(iris)
```
````

More text
7 changes: 7 additions & 0 deletions tests/testthat/rmd/nested-in.Rmd
@@ -0,0 +1,7 @@
this

````md
`r Sys.Date() ````{r, highlight.output=c(1, 3)}
head(iris)
```
````
7 changes: 7 additions & 0 deletions tests/testthat/rmd/nested-out.Rmd
@@ -0,0 +1,7 @@
this

````md
`r Sys.Date() ````{r, highlight.output=c(1, 3)}
head(iris)
```
````

0 comments on commit 85435e3

Please sign in to comment.