@@ -12,12 +12,12 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
12
12
enc :: transform_lines_enc(path , fun = fun , ... , verbose = verbose )
13
13
} else if (is_rmd_file(path )) {
14
14
enc :: transform_lines_enc(path ,
15
- fun = partial(transform_rmd , transformer_fun = fun ), ... ,
15
+ fun = partial(transform_mixed , transformer_fun = fun , filetype = " Rmd " ), ... ,
16
16
verbose = verbose
17
17
)
18
18
} else if (is_rnw_file(path )) {
19
19
enc :: transform_lines_enc(path ,
20
- fun = partial(transform_rnw , transformer_fun = fun ), ... ,
20
+ fun = partial(transform_mixed , transformer_fun = fun , filetype = " Rnw " ), ... ,
21
21
verbose = verbose
22
22
)
23
23
} else {
@@ -31,61 +31,30 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
31
31
# ' an Rmd or Rnw file and recombines the resulting (styled) code chunks with the text
32
32
# ' chunks.
33
33
# '
34
- # ' @param lines A character vector of lines from an Rmd or Rnw file
35
- # ' @param transformer_fun A styler transformer function
36
- # ' @param filetype A string indicating the filetype (Rmd or Rnw)
34
+ # ' @param lines A character vector of lines from an Rmd or Rnw file.
35
+ # ' @param transformer_fun A styler transformer function.
36
+ # ' @param filetype A string indicating the filetype (Rmd or Rnw).
37
37
# ' @importFrom purrr flatten_chr
38
38
# ' @keywords internal
39
- transform_mixed <- function (lines , transformer_fun , filetype = " Rmd " ) {
39
+ transform_mixed <- function (lines , transformer_fun , filetype ) {
40
40
chunks <- separate_chunks(lines , filetype )
41
41
chunks $ r_chunks <- map(chunks $ r_chunks , transformer_fun )
42
42
43
43
map2(chunks $ text_chunks , c(chunks $ r_chunks , list (character (0 ))), c ) %> %
44
44
flatten_chr()
45
45
}
46
46
47
- # ' Transform Rmd contents
48
- # '
49
- # ' Applies the supplied transformer function to code chunks identified within
50
- # ' an Rmd file and recombines the resulting (styled) code chunks with the text
51
- # ' chunks.
52
- # '
53
- # ' @param lines A character vector of lines from an Rmd file
54
- # ' @param transformer_fun A styler transformer function
55
- # ' @importFrom purrr flatten_chr
56
- # ' @keywords internal
57
- transform_rmd <- function (lines , transformer_fun ) {
58
- transform_mixed(lines , transformer_fun , filetype = " Rmd" )
59
- }
60
-
61
- # ' Transform Rnw contents
62
- # '
63
- # ' Applies the supplied transformer function to code chunks identified within
64
- # ' an Rnw file and recombines the resulting (styled) code chunks with the text
65
- # ' chunks.
66
- # '
67
- # ' @param lines A character vector of lines from an Rnw file
68
- # ' @param transformer_fun A styler transformer function
69
- # ' @importFrom purrr flatten_chr
70
- # ' @keywords internal
71
- transform_rnw <- function (lines , transformer_fun ) {
72
- transform_mixed(lines , transformer_fun , filetype = " Rnw" )
73
- }
74
-
75
47
# ' Separate chunks within Rmd and Rnw contents
76
48
# '
77
49
# ' Identifies and separates the code and text chunks (the latter includes non-R
78
50
# ' code) within an Rmd or Rnw file, and returns these separately.
79
- # ' @param lines a character vector of lines from an Rmd file
51
+ # ' @param lines A character vector of lines from an Rmd or Rnw file.
52
+ # ' @param filetype A string indicating the filetype - either 'Rmd' or 'Rnw'.
80
53
# ' @importFrom purrr map2
81
54
# ' @importFrom rlang seq2
82
55
# ' @keywords internal
83
- separate_chunks <- function (lines , filetype = " Rmd" ) {
84
- if (filetype == " Rmd" ) {
85
- r_raw_chunks <- identify_rmd_raw_chunks(lines )
86
- } else {
87
- r_raw_chunks <- identify_rnw_raw_chunks(lines )
88
- }
56
+ separate_chunks <- function (lines , filetype ) {
57
+ r_raw_chunks <- identify_raw_chunks(lines , filetype = filetype )
89
58
90
59
r_chunks <- map2(
91
60
r_raw_chunks $ starts , r_raw_chunks $ ends , ~ lines [seq2(.x + 1 , .y - 1 )]
@@ -98,55 +67,40 @@ separate_chunks <- function(lines, filetype = "Rmd") {
98
67
lst(r_chunks , text_chunks )
99
68
}
100
69
101
- # ' Identifies raw Rmd code chunks
70
+ # ' Identifies raw Rmd or Rnw code chunks
102
71
# '
103
72
# ' Raw in the sense that these chunks don't contain pure R code, but they
104
73
# ' contain a header and footer of markdown. Only code chunks that have an engine
105
74
# ' whose name matches `engine-pattern` are considered as R code.
106
75
# ' @inheritParams separate_chunks
107
76
# ' @param engine_pattern A regular expression that must match the engine name.
108
77
# ' @keywords internal
109
- identify_rmd_raw_chunks <- function (lines , engine_pattern = get_engine_pattern()) {
110
- pattern <- get_knitr_pattern(lines , filetype = " Rmd " )
78
+ identify_raw_chunks <- function (lines , filetype , engine_pattern = get_engine_pattern()) {
79
+ pattern <- get_knitr_pattern(filetype )
111
80
if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
112
81
stop(" Unrecognized chunk pattern!" , call. = FALSE )
113
82
}
114
- chunks <- grep(" ^[\t >]*```+\\ s*" , lines , perl = TRUE )
115
- starts <- odd(chunks )
116
- ends <- even(chunks )
117
83
118
- if (length(starts ) != length(ends )) {
119
- stop(" Malformed file!" , call. = FALSE )
120
- }
121
-
122
- is_r_code <- grepl(
123
- paste0(" ^[\t >]*```+\\ s*\\ {\\ s*" , engine_pattern , " [\\ s\\ },]" ),
124
- lines [starts ],
125
- perl = TRUE
126
- )
127
- list (starts = starts [is_r_code ], ends = ends [is_r_code ])
128
- }
129
-
130
- # ' Identifies raw Rnw code chunks
131
- # '
132
- # ' Raw in the sense that these chunks don't contain pure R code, but they
133
- # ' contain the Rnw chunk header and footer. All Rnw code chunks are assumed
134
- # ' to contain R code.
135
- # ' @inheritParams separate_chunks
136
- # ' @keywords internal
137
- identify_rnw_raw_chunks <- function (lines ) {
138
- pattern <- get_knitr_pattern(lines , filetype = " Rnw" )
139
- if (is.null(pattern $ chunk.begin ) || is.null(pattern $ chunk.end )) {
140
- stop(" Unrecognized chunk pattern!" , call. = FALSE )
84
+ if (filetype == " Rmd" ) {
85
+ chunks <- grep(" ^[\t >]*```+\\ s*" , lines , perl = TRUE )
86
+ starts <- odd(chunks )
87
+ ends <- even(chunks )
88
+ is_r_code <- grepl(
89
+ paste0(" ^[\t >]*```+\\ s*\\ {\\ s*" , engine_pattern , " [\\ s\\ },]" ),
90
+ lines [starts ],
91
+ perl = TRUE
92
+ )
93
+ } else if (filetype == " Rnw" ) {
94
+ starts <- grep(pattern $ chunk.begin , lines , perl = TRUE )
95
+ ends <- grep(pattern $ chunk.end , lines , perl = TRUE )
96
+ is_r_code <- rep(TRUE , length(start ))
141
97
}
142
- starts <- grep(pattern $ chunk.begin , lines , perl = TRUE )
143
- ends <- grep(pattern $ chunk.end , lines , perl = TRUE )
144
98
145
99
if (length(starts ) != length(ends )) {
146
100
stop(" Malformed file!" , call. = FALSE )
147
101
}
148
102
149
- list (starts = starts , ends = ends )
103
+ list (starts = starts [ is_r_code ] , ends = ends [ is_r_code ] )
150
104
}
151
105
152
106
# ' What's the engine pattern for rmd code chunks?
@@ -170,10 +124,10 @@ get_engine_pattern <- function() {
170
124
# '
171
125
# ' @inheritParams separate_chunks
172
126
# ' @keywords internal
173
- get_knitr_pattern <- function (lines , filetype = " Rmd " ) {
127
+ get_knitr_pattern <- function (filetype ) {
174
128
if (filetype == " Rnw" ) {
175
129
knitr :: all_patterns [[" rnw" ]]
176
- } else {
130
+ } else if ( filetype == " Rmd " ) {
177
131
knitr :: all_patterns [[" md" ]]
178
132
}
179
133
}
0 commit comments