Skip to content

Commit a749d87

Browse files
Merge pull request #931 from r-lib/issue-931
support qmd file format, and treat it internally as R markdown
2 parents eec45e4 + 5df9c8a commit a749d87

26 files changed

+384
-14
lines changed

NEWS.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
# styler 1.7.0.9000 (Development version)
2+
3+
**Features**
4+
5+
* `filetype` `.qmd` is now supported, but not turned on by default (#931).
16
* new R option `styler.ignore_alignment` controls if alignment should be
27
detected (and preserved) or not (#932).
8+
9+
**Bug Fixes**
10+
311
* the cache is also invalidated on changing the stylerignore markers (#932).
412

13+
514
# styler 1.7.0
615

716
* if `else` follows directly after `if`, line breaks are removed (#935).

R/addins.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ style_active_file <- function() {
6060
is_rprofile_file(context$path)
6161
)
6262

63-
if (is_rmd_file(context$path)) {
63+
if (is_rmd_file(context$path) || is_qmd_file(context$path)) {
6464
out <- transform_mixed(context$contents, transformer, filetype = "Rmd")
6565
} else if (is_rnw_file(context$path)) {
6666
out <- transform_mixed(context$contents, transformer, filetype = "Rnw")

R/set-assert-args.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ set_and_assert_arg_filetype <- function(filetype) {
7171
#' @importFrom rlang abort
7272
#' @keywords internal
7373
assert_filetype <- function(lowercase_filetype) {
74-
if (!all(lowercase_filetype %in% c("r", "rmd", "rmarkdown", "rnw", "rprofile"))) {
74+
allowed_types <- c("r", "rmd", "rmarkdown", "rnw", "rprofile", "qmd")
75+
if (!all(lowercase_filetype %in% allowed_types)) {
7576
abort(paste(
7677
"filetype must not contain other values than 'R', 'Rprofile',",
77-
"'Rmd', 'Rmarkdown' or 'Rnw' (case is ignored)."
78+
"'Rmd', 'Rmarkdown', 'qmd' or 'Rnw' (case is ignored)."
7879
))
7980
}
8081
}

R/transform-code.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
transform_code <- function(path, fun, ..., dry) {
1212
if (is_plain_r_file(path) || is_rprofile_file(path)) {
1313
transform_utf8(path, fun = fun, ..., dry = dry)
14-
} else if (is_rmd_file(path)) {
14+
} else if (is_rmd_file(path) || is_qmd_file(path)) {
1515
transform_utf8(path,
1616
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"),
1717
..., dry = dry

R/ui-styling.R

+18-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ NULL
77
#'
88
#' Performs various substitutions in all `.R` files in a package
99
#' (code and tests). One can also (optionally) style `.Rmd`, `.Rmarkdown` and/or
10-
#' `.Rnw` files (vignettes and readme) by changing the `filetype` argument.
10+
#' `.qmd`, `.Rnw` files (vignettes and readme) by changing the `filetype`
11+
#' argument.
1112
#' Carefully examine the results after running this function!
1213
#'
1314
#' @param pkg Path to a (subdirectory of an) R package.
@@ -161,6 +162,17 @@ prettify_pkg <- function(transformers,
161162
)
162163
)
163164
}
165+
166+
if ("\\.qmd" %in% filetype_) {
167+
vignette_files <- append(
168+
vignette_files,
169+
dir_without_.(
170+
path = ".",
171+
pattern = "\\.qmd$"
172+
)
173+
)
174+
}
175+
164176
files <- setdiff(
165177
c(r_files, rprofile_files, vignette_files, readme),
166178
exclude_files
@@ -214,7 +226,8 @@ style_text <- function(text,
214226

215227
#' Prettify arbitrary R code
216228
#'
217-
#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown` and/or `.Rnw` files
229+
#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown`, `qmd`
230+
#' and/or `.Rnw` files
218231
#' in a directory (by default only `.R` files are styled - see `filetype` argument).
219232
#' Carefully examine the results after running this function!
220233
#' @param path Path to a directory with files to transform.
@@ -263,8 +276,8 @@ style_dir <- function(path = ".",
263276
#'
264277
#' This is a helper function for style_dir.
265278
#' @inheritParams style_pkg
266-
#' @param recursive A logical value indicating whether or not files in subdirectories
267-
#' should be styled as well.
279+
#' @param recursive A logical value indicating whether or not files in
280+
#' subdirectories should be styled as well.
268281
#' @keywords internal
269282
prettify_any <- function(transformers,
270283
filetype,
@@ -298,7 +311,7 @@ prettify_any <- function(transformers,
298311
)
299312
}
300313

301-
#' Style `.R`, `.Rmd`, `.Rmarkdown` or `.Rnw` files
314+
#' Style `.R`, `.Rmd`, `.Rmarkdown`, `.qmd` or `.Rnw` files
302315
#'
303316
#' Performs various substitutions in the files specified.
304317
#' Carefully examine the results after running this function!

R/utils-files.R

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ is_rnw_file <- function(path) {
1313
grepl("\\.Rnw$", path, ignore.case = TRUE)
1414
}
1515

16+
is_qmd_file <- function(path) {
17+
grepl("\\.qmd$", path, ignore.case = TRUE)
18+
}
19+
20+
1621
is_unsaved_file <- function(path) {
1722
path == ""
1823
}

man/prettify_any.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_dir.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_file.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/style_pkg.Rd

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
output:
3+
github_document:
4+
html_preview: true
5+
---
6+
7+
<!-- README.md is generated from README.Rmd. Please edit that file -->
8+
9+
Some text
10+
```{r}
11+
# Some R code
12+
f <- function(x) {
13+
x
14+
}
15+
```
16+
Final text
17+
```{r}
18+
1 + 2
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Package: xyzpackage
2+
Title: What the Package Does (one line, title case)
3+
Version: 0.0.0.9000
4+
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
5+
Description: What the package does (one paragraph).
6+
Depends: R (>= 3.3.2)
7+
License: What license is it under?
8+
Encoding: UTF-8
9+
LazyData: true
10+
Suggests: testthat
11+
LinkingTo:
12+
Rcpp
13+
Imports:
14+
Rcpp
15+
RoxygenNote: 6.0.1.9000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Generated by roxygen2: do not edit by hand
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
timesTwo <- function(x) {
5+
.Call("_xyzpackage_timesTwo", PACKAGE = "xyzpackage", x)
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello_world <- function() {
2+
print("hello, world")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
output: github_document
3+
---
4+
5+
<!-- README.md is generated from README.Rmd. Please edit that file -->
6+
7+
```{r setup, include = FALSE}
8+
knitr::opts_chunk$set(
9+
collapse = TRUE,
10+
comment = "#>",
11+
fig.path = "man/figures/README-"
12+
)
13+
```
14+
# styler
15+
16+
The goal of styler is to ...
17+
18+
## Installation
19+
20+
You can install styler from github with:
21+
22+
```{r gh-installation, eval = FALSE}
23+
# install.packages("devtools")
24+
devtools::install_github("jonmcalder/styler")
25+
```
26+
27+
## Example
28+
29+
This is a basic example which shows you how to solve a common problem:
30+
31+
```{r example}
32+
## basic example code
33+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
output:
3+
github_document:
4+
html_preview: true
5+
---
6+
7+
<!-- README.md is generated from README.Rmd. Please edit that file -->
8+
9+
Some text
10+
```{r}
11+
# Some R code
12+
f <- function(x) {
13+
x
14+
}
15+
```
16+
Final text
17+
```{r}
18+
1 + 2
19+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.dll
2+
*.o
3+
*.so
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
#include <Rcpp.h>
5+
6+
using namespace Rcpp;
7+
8+
// timesTwo
9+
NumericVector timesTwo(NumericVector x);
10+
RcppExport SEXP _xyzpackage_timesTwo(SEXP xSEXP) {
11+
BEGIN_RCPP
12+
Rcpp::RObject rcpp_result_gen;
13+
Rcpp::RNGScope rcpp_rngScope_gen;
14+
Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP);
15+
rcpp_result_gen = Rcpp::wrap(timesTwo(x));
16+
return rcpp_result_gen;
17+
END_RCPP
18+
}
19+
20+
static const R_CallMethodDef CallEntries[] = {
21+
{"_xyzpackage_timesTwo", (DL_FUNC) &_xyzpackage_timesTwo, 1},
22+
{NULL, NULL, 0}
23+
};
24+
25+
RcppExport void R_init_xyzpackage(DllInfo *dll) {
26+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
27+
R_useDynamicSymbols(dll, FALSE);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
// This is a simple example of exporting a C++ function to R. You can
5+
// source this function into an R session using the Rcpp::sourceCpp
6+
// function (or via the Source button on the editor toolbar). Learn
7+
// more about Rcpp at:
8+
//
9+
// http://www.rcpp.org/
10+
// http://adv-r.had.co.nz/Rcpp.html
11+
// http://gallery.rcpp.org/
12+
//
13+
14+
// [[Rcpp::export]]
15+
NumericVector timesTwo(NumericVector x) {
16+
return x * 2;
17+
}
18+
19+
20+
// You can include R code blocks in C++ files processed with sourceCpp
21+
// (useful for testing and development). The R code will be automatically
22+
// run after the compilation.
23+
//
24+
25+
/*** R
26+
timesTwo(42)
27+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(testthat)
2+
library(xyzpackage)
3+
4+
test_check("xyzpackage")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
context("testing styler on package")
2+
3+
test_that("hi there", {
4+
I(am(a(package(x))))
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Vignette Title"
3+
author: "Vignette Author"
4+
date: "`r Sys.Date()`"
5+
output: rmarkdown::html_vignette
6+
vignette: >
7+
%\VignetteIndexEntry{Vignette Title}
8+
%\VignetteEngine{knitr::rmarkdown}
9+
%\VignetteEncoding{UTF-8}
10+
---
11+
12+
Vignettes are long form documentation commonly included in packages. Because they are part of the distribution of the package, they need to be as compact as possible. The `html_vignette` output type provides a custom style sheet (and tweaks some options) to ensure that the resulting html is as small as possible. The `html_vignette` format:
13+
14+
- Never uses retina figures
15+
- Has a smaller default figure size
16+
- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style
17+
18+
## Vignette Info
19+
20+
Note the various macros within the `vignette` section of the metadata block above. These are required in order to instruct R how to build the vignette. Note that you should change the `title` field and the `\VignetteIndexEntry` to match the title of your vignette.
21+
22+
## Styles
23+
24+
The `html_vignette` template includes a basic CSS theme. To override this theme you can specify your own CSS in the document metadata as follows:
25+
26+
output:
27+
rmarkdown::html_vignette:
28+
css: mystyles.css
29+
30+
## Figures
31+
32+
The figure sizes have been customised so that you can easily put two images side-by-side.
33+
34+
```{r, fig.show='hold'}
35+
plot(1:10)
36+
plot(10:1)
37+
```
38+
39+
You can enable figure captions by `fig_caption: yes` in YAML:
40+
41+
output:
42+
rmarkdown::html_vignette:
43+
fig_caption: yes
44+
45+
Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**.
46+
47+
## More Examples
48+
49+
You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`.
50+
51+
```{r, echo=FALSE, results='asis'}
52+
knitr::kable(head(mtcars, 10))
53+
```
54+
55+
Also a quote using `>`:
56+
57+
> "He who gives up [code] safety for [code] speed deserves neither."
58+
([via](https://twitter.com/hadleywickham/status/504368538874703872))

0 commit comments

Comments
 (0)