Skip to content

Commit 1e381d9

Browse files
fix regex
1 parent 146482a commit 1e381d9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

R/transform-code.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ identify_r_raw_chunks <- function(lines, engine_pattern = "[rR]") {
6969
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
7070
stop("Unrecognized chunk pattern!", call. = FALSE)
7171
}
72-
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
73-
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
72+
starts <- grep("^[\t >]*```+\\s*\\{\\s*([a-zA-Z0-9]+.*)\\}\\s*$", lines, perl = TRUE)
73+
ends <- grep("^[\t >]*```+\\s*$", lines, perl = TRUE)
7474

7575
if (length(starts) != length(ends)) {
7676
stop("Malformed file!", call. = FALSE)
7777
}
7878

7979
is_r_code <- grepl(
80-
paste0("^[\t >]*```+\\s*\\{(", engine_pattern, ".*)\\}\\s*$"),
80+
paste0("^[\t >]*```+\\s*\\{\\s*", engine_pattern, "[\\s\\},]"),
8181
lines[starts], perl = TRUE
8282
)
8383
list(starts = starts[is_r_code], ends = ends[is_r_code])

tests/testthat/rmd/r_and_non_r_code_chunks-in.Rmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ Some text.
66
1+1
77
```
88

9+
```{r}
10+
1+1
11+
```
12+
13+
```{r, a = 3}
14+
1+1
15+
```
16+
17+
```{ r }
18+
1+1
19+
```
20+
921
More text.
1022

1123
```{nonR}

tests/testthat/rmd/r_and_non_r_code_chunks-out.Rmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ Some text.
66
1 + 1
77
```
88

9+
```{r}
10+
1 + 1
11+
```
12+
13+
```{r, a = 3}
14+
1 + 1
15+
```
16+
17+
```{ r }
18+
1 + 1
19+
```
20+
921
More text.
1022

1123
```{nonR}

0 commit comments

Comments
 (0)