-
Notifications
You must be signed in to change notification settings - Fork 73
Update to allow styling of Rmd files #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
dc974fc
cf06cc1
e1e942c
584a294
27c0c53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Transform code from R or Rmd files | ||
#' | ||
#' @inheritParams utf8::transform_lines_enc | ||
#' @param ... Further arguments passed to `utf8::transform_lines_enc()`. | ||
transform_code <- function(path, fun, verbose, ...) { | ||
if (grepl("\\.R$", path, ignore.case = TRUE)) { | ||
utf8::transform_lines_enc(path, fun = fun, ...) | ||
} else if (grepl("\\.Rmd$", path, ignore.case = TRUE)) { | ||
utf8::transform_lines_enc(path, fun = partial(transform_rmd, transformer_fun = fun), ...) | ||
} else { | ||
stop("") | ||
} | ||
} | ||
|
||
#' Transform Rmd contents with a transformer function | ||
#' | ||
#' @param lines A character vector of lines from an Rmd file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use tidyverse style in your roxygen comments? In particular, argument descriptions should end with a full stop here and in other places. |
||
#' @param transformer_fun A styler transformer function | ||
#' @importFrom purrr flatten_chr | ||
transform_rmd <- function(lines, transformer_fun) { | ||
chunks <- identify_chunks(lines) | ||
chunks$r_chunks <- map(chunks$r_chunks, transformer_fun) | ||
|
||
map2(chunks$text_chunks, c(chunks$r_chunks, list(character(0))), c) %>% | ||
flatten_chr() | ||
} | ||
|
||
|
||
#' Identify R and text chunks within an Rmd | ||
#' | ||
#' @param lines a character vector of lines from an Rmd file | ||
#' | ||
#' @importFrom purrr map2 | ||
#' @importFrom rlang seq2 | ||
identify_chunks <- function(lines) { | ||
pattern <- get_knitr_pattern(lines) | ||
if (is.null(pattern$chunk.begin) || is.null(pattern$chunk.end)) { | ||
stop("Unrecognized chunk pattern!", call. = FALSE) | ||
} | ||
|
||
starts <- grep(pattern$chunk.begin, lines, perl = TRUE) | ||
ends <- grep(pattern$chunk.end, lines, perl = TRUE) | ||
|
||
if (length(starts) != length(ends)) { | ||
stop("Malformed file!", call. = FALSE) | ||
} | ||
|
||
r_chunks <- map2(starts, ends, ~lines[seq2(.x + 1, .y - 1)]) | ||
|
||
text_chunks <- map2(c(1, ends), c(starts, length(lines)), ~lines[seq2(.x, .y)]) | ||
|
||
lst(r_chunks, text_chunks) | ||
} | ||
|
||
#' Get chunk pattern | ||
#' | ||
#' Determine a regex pattern for identifying R code chunks | ||
#' | ||
#' @inheritParams identify_chunks | ||
get_knitr_pattern <- function(lines) { | ||
pattern <- knitr:::detect_pattern(lines, "rmd") | ||
if (!is.null(pattern)) { | ||
knitr::all_patterns[[pattern]] | ||
} else { | ||
NULL | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,7 @@ style_file <- function(path, | |
#' @inheritParams style_dir | ||
#' @param path The path to a file that should be styled. | ||
prettify_one <- function(transformers, path) { | ||
if (!grepl("\\.[Rr]$", path)) stop(path, " is not a .R file") | ||
if (!grepl("\\.[r](md)?$", path, ignore.case = TRUE)) | ||
stop(path, " is not an R or Rmd file") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this check if we check further downstream? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right that the downstream check makes this redundant. The downstream check has to be there since it provides the logic for R/Rmd handling. I suppose the only advantage to having this earlier check is that the process is stopped before the call to I'll check if it can be safely removed though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. In that case, you can remove |
||
transform_files(path, transformers) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Some text | ||
```{r} | ||
# Some R code | ||
f <- function(x) { | ||
x | ||
} | ||
``` | ||
More text | ||
```{r} | ||
# More R code | ||
g <- function(y) { | ||
y | ||
} | ||
``` | ||
Final text | ||
```{r} | ||
1 + 2 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
```{r} | ||
# Start with chunk | ||
``` | ||
Some text before empty chunk | ||
```{r} | ||
|
||
``` | ||
Final text before longer code chunk | ||
This text chunk has multiple lines | ||
```{r} | ||
# random | ||
this(is_a_call(x)) | ||
if (x) { | ||
r() | ||
a <- 3 | ||
bcds <- 5 | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Some text | ||
```{r} | ||
# Some R code | ||
f <- function(x) { | ||
x | ||
} | ||
``` | ||
More text before malformed chunk | ||
# More R code | ||
g <- function(y) { | ||
y | ||
} | ||
``` | ||
Final text | ||
```{r} | ||
1 + 2 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Some text | ||
```{r} | ||
# Some R code | ||
f <- function(x) { | ||
x | ||
} | ||
``` | ||
More text | ||
```{r} | ||
# More R code which is invalid | ||
g <- function(y) { | ||
y | ||
``` | ||
Final text | ||
```{r} | ||
1 + 2 | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can give a more informative error message here?