Skip to content

Commit dc974fc

Browse files
committed
Add functions to allow for styling of Rmd files
1 parent 019c0f9 commit dc974fc

File tree

10 files changed

+142
-6
lines changed

10 files changed

+142
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Collate:
5454
'serialize.R'
5555
'serialized_tests.R'
5656
'style_guides.R'
57+
'style_rmd.R'
5758
'styler.R'
5859
'token-create.R'
5960
'transform.R'

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ importFrom(purrr,pwalk)
2727
importFrom(purrr,reduce)
2828
importFrom(purrr,when)
2929
importFrom(rlang,seq2)
30+
importFrom(tibble,lst)
3031
importFrom(utils,tail)
3132
importFrom(utils,write.table)

R/style_rmd.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#' Transform code from R or Rmd files
2+
#'
3+
#' @inheritParams utf8::transform_lines_enc
4+
#' @param ... Further arguments passed to `utf8::transform_lines_enc()`.
5+
transform_code <- function(path, fun, verbose, ...) {
6+
if (grepl("\\.R$", path, ignore.case = TRUE)) {
7+
utf8::transform_lines_enc(path, fun = fun, ...)
8+
} else if (grepl("\\.Rmd$", path, ignore.case = TRUE)) {
9+
utf8::transform_lines_enc(path, fun = partial(transform_rmd, transformer_fun = fun), ...)
10+
} else {
11+
stop("")
12+
}
13+
}
14+
15+
#' Transform Rmd contents with a transformer function
16+
#'
17+
#' @param lines A character vector of lines from an Rmd file
18+
#' @param transformer_fun A styler transformer function
19+
#' @importFrom purrr flatten_chr
20+
transform_rmd <- function(lines, transformer_fun) {
21+
chunks <- identify_chunks(lines)
22+
chunks$r_chunks <- map(chunks$r_chunks, transformer_fun)
23+
24+
map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>%
25+
flatten_chr()
26+
}
27+
28+
29+
#' Identify R and text chunks within an Rmd
30+
#'
31+
#' @param lines a character vector of lines from an Rmd file
32+
#'
33+
#' @importFrom purrr map2
34+
#' @importFrom rlang seq2
35+
#' @importFrom tibble lst
36+
identify_chunks <- function(lines) {
37+
pattern <- get_knitr_pattern(lines)
38+
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) {
39+
stop("Unrecognized chunk pattern!", call. = FALSE)
40+
}
41+
42+
starts <- grep(pattern$chunk.begin, lines, perl = TRUE)
43+
ends <- grep(pattern$chunk.end, lines, perl = TRUE)
44+
45+
if (length(starts) != length(ends)) {
46+
stop("Malformed file!", call. = FALSE)
47+
}
48+
49+
r_chunks <- map2(starts, ends, ~lines[seq2(.x + 1, .y - 1)])
50+
51+
text_chunks <- map2(c(1, ends), c(starts, length(lines)), ~lines[seq2(.x, .y)])
52+
53+
lst(r_chunks, text_chunks)
54+
}
55+
56+
#' Get chunk pattern
57+
#'
58+
#' Determine a regex pattern for identifying R code chunks
59+
#'
60+
#' @inheritParams identify_chunks
61+
get_knitr_pattern <- function(lines) {
62+
pattern <- knitr:::detect_pattern(lines, "rmd")
63+
if (!is.null(pattern)) {
64+
knitr::all_patterns[[pattern]]
65+
} else {
66+
NULL
67+
}
68+
}

R/transform.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ transform_files <- function(files, transformers) {
2727
invisible(changed)
2828
}
2929

30-
#' Transform a file an give customized message
30+
#' Transform a file and output a customized message
3131
#'
32-
#' Wraps `utf8::transform_lines_enc()` and gives customized messages.
32+
#' Wraps `utf8::transform_lines_enc()` and outputs customized messages.
3333
#' @param max_char_path The number of characters of the longest path. Determines
3434
#' the indention level of `message_after`.
3535
#' @param message_before The message to print before the path.
@@ -51,7 +51,7 @@ transform_file <- function(path,
5151
n_spaces_before_message_after <-
5252
max_char_after_message_path - char_after_path
5353
message(message_before, path, ".", appendLF = FALSE)
54-
changed <- utf8::transform_lines_enc(path, fun = fun, verbose = verbose, ...)
54+
changed <- transform_code(path, fun = fun, verbose = verbose, ...)
5555

5656
message(
5757
rep(" ", max(0, n_spaces_before_message_after)),

R/ws.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ style_file <- function(path,
155155
#' @inheritParams style_dir
156156
#' @param path The path to a file that should be styled.
157157
prettify_one <- function(transformers, path) {
158-
if (!grepl("\\.[Rr]$", path)) stop(path, " is not a .R file")
158+
if (!grepl("\\.[r](md)?$", path, ignore.case = TRUE))
159+
stop(path, " is not an R or Rmd file")
159160
transform_files(path, transformers)
160161
}

man/get_knitr_pattern.Rd

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

man/identify_chunks.Rd

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

man/transform_code.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.

man/transform_file.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_rmd.Rd

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