Skip to content

Commit 77757f1

Browse files
only consider R code chunks as code
1 parent e9d65f9 commit 77757f1

File tree

4 files changed

+64
-29
lines changed

4 files changed

+64
-29
lines changed

R/transform-code.R

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,65 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
2929
#' @param transformer_fun A styler transformer function
3030
#' @importFrom purrr flatten_chr
3131
transform_rmd <- function(lines, transformer_fun) {
32-
chunks <- identify_chunks(lines)
32+
chunks <- separate_chunks(lines)
3333
chunks$r_chunks <- map(chunks$r_chunks, transformer_fun)
3434

3535
map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>%
3636
flatten_chr()
3737
}
3838

3939

40-
#' Identify chunks within Rmd contents
41-
#'
42-
#' Identifies the code and text chunks within an Rmd file, and returns these
43-
#' as a nested list.
40+
#' Separate chunks within Rmd contents
4441
#'
42+
#' Identifies and separates the code and text chunks (the latter includes non-R
43+
#' code) within an Rmd file, and returns these separately.
4544
#' @param lines a character vector of lines from an Rmd file
46-
#'
4745
#' @importFrom purrr map2
4846
#' @importFrom rlang seq2
49-
identify_chunks <- function(lines) {
47+
separate_chunks <- function(lines) {
48+
r_raw_chunks <- identify_r_raw_chunks(lines)
49+
r_chunks <- map2(
50+
r_raw_chunks$starts, r_raw_chunks$ends, ~lines[seq2(.x + 1, .y - 1)]
51+
)
52+
53+
text_chunks <- map2(
54+
c(1, r_raw_chunks$ends), c(r_raw_chunks$starts, length(lines)),
55+
~lines[seq2(.x, .y)]
56+
)
57+
lst(r_chunks, text_chunks)
58+
}
59+
60+
#' Identifies raw R code chunks
61+
#'
62+
#' Raw in the sense that these chunks don't contain pure R code, but they
63+
#' contain a header and footer of markdown. Only code chunks that have an engine
64+
#' whose name matches `engine-pattern` are considered as R code.
65+
#' @inheritParams separate_chunks
66+
#' @param engine_pattern A regular expression that must match the engine name.
67+
identify_r_raw_chunks <- function(lines, engine_pattern = "[rR]") {
5068
pattern <- get_knitr_pattern(lines)
5169
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
5270
stop("Unrecognized chunk pattern!", call. = FALSE)
5371
}
54-
5572
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
5673
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
5774

5875
if (length(starts) != length(ends)) {
5976
stop("Malformed file!", call. = FALSE)
6077
}
6178

62-
r_chunks <- map2(starts, ends, ~lines[seq2(.x + 1, .y - 1)])
63-
64-
text_chunks <- map2(c(1, ends), c(starts, length(lines)), ~lines[seq2(.x, .y)])
65-
66-
lst(r_chunks, text_chunks)
79+
is_r_code <- grepl(
80+
paste0("^[\t >]*```+\\s*\\{(", engine_pattern, ".*)\\}\\s*$"),
81+
lines[starts], perl = TRUE
82+
)
83+
list(starts = starts[is_r_code], ends = ends[is_r_code])
6784
}
6885

6986
#' Get chunk pattern
7087
#'
7188
#' Determine a regex pattern for identifying R code chunks.
7289
#'
73-
#' @inheritParams identify_chunks
90+
#' @inheritParams separate_chunks
7491
get_knitr_pattern <- function(lines) {
7592
knitr::all_patterns[["md"]]
7693
}

man/identify_chunks.Rd

Lines changed: 0 additions & 15 deletions
This file was deleted.

man/identify_r_raw_chunks.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/separate_chunks.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)