Skip to content

Commit

Permalink
Merge pull request #386 from lorenzwalthert/rmd
Browse files Browse the repository at this point in the history
Accept engine-agnostic code in .Rmd
  • Loading branch information
lorenzwalthert committed Apr 3, 2018
2 parents a1fe62f + 5b376f4 commit ea6dcd6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
5 changes: 3 additions & 2 deletions R/transform-code.R
Expand Up @@ -73,8 +73,9 @@ identify_r_raw_chunks <- function(lines, engine_pattern = get_engine_pattern())
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
stop("Unrecognized chunk pattern!", call. = FALSE)
}
starts <- grep("^[\t >]*```+\\s*\\{\\s*([a-zA-Z0-9]+.*)\\}\\s*$", lines, perl = TRUE)
ends <- grep("^[\t >]*```+\\s*$", lines, perl = TRUE)
chunks <- grep("^[\t >]*```+\\s*", lines, perl = TRUE)
starts <- odd(chunks)
ends <- even(chunks)

if (length(starts) != length(ends)) {
stop("Malformed file!", call. = FALSE)
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
@@ -1,5 +1,25 @@
parse_text <- function(x) parse(text = x)[[1L]]

line_col_names <- function() {
c("line1", "line2", "col1", "col2")
}

#' Check whether two columns match
#'
#' @param col1,col2 Column names as string.
#' @param data The data frames that contains `col1` and `col2`.
two_cols_match <- function(col1, col2, data) {
all(unlist(data[col1]) == unlist(data[col2]))
}

odd <- function(x) {
x[seq(1L, length(x), by = 2)]
}

even <- function(x) {
x[seq(2L, length(x), by = 2)]
}

#' Repeat elements of a character vector `times` times and collapse it
#'
#' @param char A character vector.
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/rmd/r_and_non_r_code_chunks-in.Rmd
Expand Up @@ -28,3 +28,8 @@ I like the jungle, I like the .2'
I like the jungle,
```


Code without engine
```
1+1
```
5 changes: 5 additions & 0 deletions tests/testthat/rmd/r_and_non_r_code_chunks-out.Rmd
Expand Up @@ -28,3 +28,8 @@ I like the jungle, I like the .2'
I like the jungle,
```


Code without engine
```
1+1
```
4 changes: 2 additions & 2 deletions tests/testthat/test-rmd.R
@@ -1,13 +1,13 @@
context("rmd")

test_that("can style .Rmd files", {
expect_error(test_collection(
expect_warning(test_collection(
"rmd", "simple",
transformer = transform_rmd,
transformer_fun = style_text,
write_tree = FALSE
), NA)
expect_error(test_collection(
expect_warning(test_collection(
"rmd", "r_and_non_r_code_chunks",
transformer = transform_rmd,
transformer_fun = style_text,
Expand Down

0 comments on commit ea6dcd6

Please sign in to comment.