Skip to content

Commit ca8f294

Browse files
committed
Initial update to handle Rnw styling
1 parent 9192714 commit ca8f294

21 files changed

+389
-32
lines changed

R/testing.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test_collection <- function(test, sub_test = NULL,
2727

2828
pattern <- paste0(
2929
if (!is.null(sub_test)) paste0("^", sub_test, ".*"),
30-
"in\\.R(?:|md)$"
30+
"in\\.R(?:|md|nw)$"
3131
)
3232

3333
in_names <- list.files(
@@ -66,7 +66,7 @@ test_collection <- function(test, sub_test = NULL,
6666
#' ))
6767
#' @keywords internal
6868
construct_out <- function(in_paths) {
69-
gsub("\\-in([.]R(?:|md))$", "\\-out\\1", in_paths)
69+
gsub("\\-in([.]R(?:|md|nw))$", "\\-out\\1", in_paths)
7070
}
7171

7272
#' Construct paths of a tree object given the paths of *-in.R files

R/transform-code.R

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#' Transform code from R or Rmd files
1+
#' Transform code from R, Rmd or Rnw files
22
#'
33
#' A wrapper for [enc::transform_lines_enc()] which initiates the styling of
4-
#' either R or Rmd files by passing the relevant transformer function for each
4+
#' either R, Rmd or Rnw files by passing the relevant transformer function for each
55
#' case.
66
#'
77
#' @inheritParams enc::transform_lines_enc
@@ -15,40 +15,78 @@ transform_code <- function(path, fun, verbose = FALSE, ...) {
1515
fun = partial(transform_rmd, transformer_fun = fun), ...,
1616
verbose = verbose
1717
)
18+
} else if (is_rnw_file(path)) {
19+
enc::transform_lines_enc(path,
20+
fun = partial(transform_rnw, transformer_fun = fun), ...,
21+
verbose = verbose
22+
)
1823
} else {
19-
stop(path, " is not an R or Rmd file")
24+
stop(path, " is not an R, Rmd or Rnw file")
2025
}
2126
}
2227

23-
#' Transform Rmd contents
28+
#' Transform mixed contents
2429
#'
2530
#' Applies the supplied transformer function to code chunks identified within
26-
#' an Rmd file and recombines the resulting (styled) code chunks with the text
31+
#' an Rmd or Rnw file and recombines the resulting (styled) code chunks with the text
2732
#' chunks.
2833
#'
29-
#' @param lines A character vector of lines from an Rmd file
34+
#' @param lines A character vector of lines from an Rmd or Rnw file
3035
#' @param transformer_fun A styler transformer function
36+
#' @param filetype A string indicating the filetype (Rmd or Rnw)
3137
#' @importFrom purrr flatten_chr
3238
#' @keywords internal
33-
transform_rmd <- function(lines, transformer_fun) {
34-
chunks <- separate_chunks(lines)
39+
transform_mixed <- function(lines, transformer_fun, filetype = "Rmd") {
40+
chunks <- separate_chunks(lines, filetype)
3541
chunks$r_chunks <- map(chunks$r_chunks, transformer_fun)
3642

3743
map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>%
3844
flatten_chr()
3945
}
4046

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+
}
4160

42-
#' Separate chunks within Rmd contents
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+
#' Separate chunks within Rmd and Rnw contents
4376
#'
4477
#' Identifies and separates the code and text chunks (the latter includes non-R
45-
#' code) within an Rmd file, and returns these separately.
78+
#' code) within an Rmd or Rnw file, and returns these separately.
4679
#' @param lines a character vector of lines from an Rmd file
4780
#' @importFrom purrr map2
4881
#' @importFrom rlang seq2
4982
#' @keywords internal
50-
separate_chunks <- function(lines) {
51-
r_raw_chunks <- identify_r_raw_chunks(lines)
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+
}
89+
5290
r_chunks <- map2(
5391
r_raw_chunks$starts, r_raw_chunks$ends, ~lines[seq2(.x + 1, .y - 1)]
5492
)
@@ -60,16 +98,16 @@ separate_chunks <- function(lines) {
6098
lst(r_chunks, text_chunks)
6199
}
62100

63-
#' Identifies raw R code chunks
101+
#' Identifies raw Rmd code chunks
64102
#'
65103
#' Raw in the sense that these chunks don't contain pure R code, but they
66104
#' contain a header and footer of markdown. Only code chunks that have an engine
67105
#' whose name matches `engine-pattern` are considered as R code.
68106
#' @inheritParams separate_chunks
69107
#' @param engine_pattern A regular expression that must match the engine name.
70108
#' @keywords internal
71-
identify_r_raw_chunks <- function(lines, engine_pattern = get_engine_pattern()) {
72-
pattern <- get_knitr_pattern(lines)
109+
identify_rmd_raw_chunks <- function(lines, engine_pattern = get_engine_pattern()) {
110+
pattern <- get_knitr_pattern(lines, filetype = "Rmd")
73111
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
74112
stop("Unrecognized chunk pattern!", call. = FALSE)
75113
}
@@ -89,6 +127,28 @@ identify_r_raw_chunks <- function(lines, engine_pattern = get_engine_pattern())
89127
list(starts = starts[is_r_code], ends = ends[is_r_code])
90128
}
91129

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)
141+
}
142+
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
143+
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
144+
145+
if (length(starts) != length(ends)) {
146+
stop("Malformed file!", call. = FALSE)
147+
}
148+
149+
list(starts = starts, ends = ends)
150+
}
151+
92152
#' What's the engine pattern for rmd code chunks?
93153
#'
94154
#' The function returns the regular expression pattern that identifies
@@ -110,6 +170,10 @@ get_engine_pattern <- function() {
110170
#'
111171
#' @inheritParams separate_chunks
112172
#' @keywords internal
113-
get_knitr_pattern <- function(lines) {
114-
knitr::all_patterns[["md"]]
173+
get_knitr_pattern <- function(lines, filetype = "Rmd") {
174+
if(filetype == "Rnw") {
175+
knitr::all_patterns[["rnw"]]
176+
} else {
177+
knitr::all_patterns[["md"]]
178+
}
115179
}

R/ui.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ NULL
5858
#' @family stylers
5959
#' @examples
6060
#' \dontrun{
61-
#'
61+
#'
6262
#' style_pkg(style = tidyverse_style, strict = TRUE)
6363
#' style_pkg(
6464
#' scope = "line_breaks",
@@ -188,7 +188,7 @@ prettify_any <- function(transformers,
188188
)
189189
}
190190

191-
#' Style `.R` and/or `.Rmd` files
191+
#' Style `.R` and/or `.Rmd` and.or `.Rnw` files
192192
#'
193193
#' Performs various substitutions in the files specified.
194194
#' Carefully examine the results after running this function!

man/get_knitr_pattern.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/identify_r_raw_chunks.Rd renamed to man/identify_rmd_raw_chunks.Rd

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

man/identify_rnw_raw_chunks.Rd

Lines changed: 17 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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_file.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/transform_code.Rd

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

man/transform_mixed.Rd

Lines changed: 21 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)