@@ -29,48 +29,65 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
29
29
# ' @param transformer_fun A styler transformer function
30
30
# ' @importFrom purrr flatten_chr
31
31
transform_rmd <- function (lines , transformer_fun ) {
32
- chunks <- identify_chunks (lines )
32
+ chunks <- separate_chunks (lines )
33
33
chunks $ r_chunks <- map(chunks $ r_chunks , transformer_fun )
34
34
35
35
map2(chunks $ text_chunks , c(chunks $ r_chunks , list (character (0 ))), c ) %> %
36
36
flatten_chr()
37
37
}
38
38
39
39
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
44
41
# '
42
+ # ' Identifies and separates the code and text chunks (the latter includes non-R
43
+ # ' code) within an Rmd file, and returns these separately.
45
44
# ' @param lines a character vector of lines from an Rmd file
46
- # '
47
45
# ' @importFrom purrr map2
48
46
# ' @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]" ) {
50
68
pattern <- get_knitr_pattern(lines )
51
69
if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
52
70
stop(" Unrecognized chunk pattern!" , call. = FALSE )
53
71
}
54
-
55
72
starts <- grep(pattern $ chunk.begin , lines , perl = TRUE )
56
73
ends <- grep(pattern $ chunk.end , lines , perl = TRUE )
57
74
58
75
if (length(starts ) != length(ends )) {
59
76
stop(" Malformed file!" , call. = FALSE )
60
77
}
61
78
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 ] )
67
84
}
68
85
69
86
# ' Get chunk pattern
70
87
# '
71
88
# ' Determine a regex pattern for identifying R code chunks.
72
89
# '
73
- # ' @inheritParams identify_chunks
90
+ # ' @inheritParams separate_chunks
74
91
get_knitr_pattern <- function (lines ) {
75
92
knitr :: all_patterns [[" md" ]]
76
93
}
0 commit comments