Skip to content

Commit

Permalink
Merge pull request #931 from r-lib/issue-931
Browse files Browse the repository at this point in the history
support qmd file format, and treat it internally as R markdown
  • Loading branch information
lorenzwalthert authored Mar 25, 2022
2 parents eec45e4 + 5df9c8a commit a749d87
Show file tree
Hide file tree
Showing 26 changed files with 384 additions and 14 deletions.
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
# styler 1.7.0.9000 (Development version)

**Features**

* `filetype` `.qmd` is now supported, but not turned on by default (#931).
* new R option `styler.ignore_alignment` controls if alignment should be
detected (and preserved) or not (#932).

**Bug Fixes**

* the cache is also invalidated on changing the stylerignore markers (#932).


# styler 1.7.0

* if `else` follows directly after `if`, line breaks are removed (#935).
Expand Down
2 changes: 1 addition & 1 deletion R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ style_active_file <- function() {
is_rprofile_file(context$path)
)

if (is_rmd_file(context$path)) {
if (is_rmd_file(context$path) || is_qmd_file(context$path)) {
out <- transform_mixed(context$contents, transformer, filetype = "Rmd")
} else if (is_rnw_file(context$path)) {
out <- transform_mixed(context$contents, transformer, filetype = "Rnw")
Expand Down
5 changes: 3 additions & 2 deletions R/set-assert-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ set_and_assert_arg_filetype <- function(filetype) {
#' @importFrom rlang abort
#' @keywords internal
assert_filetype <- function(lowercase_filetype) {
if (!all(lowercase_filetype %in% c("r", "rmd", "rmarkdown", "rnw", "rprofile"))) {
allowed_types <- c("r", "rmd", "rmarkdown", "rnw", "rprofile", "qmd")
if (!all(lowercase_filetype %in% allowed_types)) {
abort(paste(
"filetype must not contain other values than 'R', 'Rprofile',",
"'Rmd', 'Rmarkdown' or 'Rnw' (case is ignored)."
"'Rmd', 'Rmarkdown', 'qmd' or 'Rnw' (case is ignored)."
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion R/transform-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
transform_code <- function(path, fun, ..., dry) {
if (is_plain_r_file(path) || is_rprofile_file(path)) {
transform_utf8(path, fun = fun, ..., dry = dry)
} else if (is_rmd_file(path)) {
} else if (is_rmd_file(path) || is_qmd_file(path)) {
transform_utf8(path,
fun = partial(transform_mixed, transformer_fun = fun, filetype = "Rmd"),
..., dry = dry
Expand Down
23 changes: 18 additions & 5 deletions R/ui-styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ NULL
#'
#' Performs various substitutions in all `.R` files in a package
#' (code and tests). One can also (optionally) style `.Rmd`, `.Rmarkdown` and/or
#' `.Rnw` files (vignettes and readme) by changing the `filetype` argument.
#' `.qmd`, `.Rnw` files (vignettes and readme) by changing the `filetype`
#' argument.
#' Carefully examine the results after running this function!
#'
#' @param pkg Path to a (subdirectory of an) R package.
Expand Down Expand Up @@ -161,6 +162,17 @@ prettify_pkg <- function(transformers,
)
)
}

if ("\\.qmd" %in% filetype_) {
vignette_files <- append(
vignette_files,
dir_without_.(
path = ".",
pattern = "\\.qmd$"
)
)
}

files <- setdiff(
c(r_files, rprofile_files, vignette_files, readme),
exclude_files
Expand Down Expand Up @@ -214,7 +226,8 @@ style_text <- function(text,

#' Prettify arbitrary R code
#'
#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown` and/or `.Rnw` files
#' Performs various substitutions in all `.R`, `.Rmd`, `.Rmarkdown`, `qmd`
#' and/or `.Rnw` files
#' in a directory (by default only `.R` files are styled - see `filetype` argument).
#' Carefully examine the results after running this function!
#' @param path Path to a directory with files to transform.
Expand Down Expand Up @@ -263,8 +276,8 @@ style_dir <- function(path = ".",
#'
#' This is a helper function for style_dir.
#' @inheritParams style_pkg
#' @param recursive A logical value indicating whether or not files in subdirectories
#' should be styled as well.
#' @param recursive A logical value indicating whether or not files in
#' subdirectories should be styled as well.
#' @keywords internal
prettify_any <- function(transformers,
filetype,
Expand Down Expand Up @@ -298,7 +311,7 @@ prettify_any <- function(transformers,
)
}

#' Style `.R`, `.Rmd`, `.Rmarkdown` or `.Rnw` files
#' Style `.R`, `.Rmd`, `.Rmarkdown`, `.qmd` or `.Rnw` files
#'
#' Performs various substitutions in the files specified.
#' Carefully examine the results after running this function!
Expand Down
5 changes: 5 additions & 0 deletions R/utils-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ is_rnw_file <- function(path) {
grepl("\\.Rnw$", path, ignore.case = TRUE)
}

is_qmd_file <- function(path) {
grepl("\\.qmd$", path, ignore.case = TRUE)
}


is_unsaved_file <- function(path) {
path == ""
}
Expand Down
4 changes: 2 additions & 2 deletions man/prettify_any.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/style_dir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/style_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/style_pkg.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/testthat/public-api/xyzfile_qmd/new.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
output:
github_document:
html_preview: true
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

Some text
```{r}
# Some R code
f <- function(x) {
x
}
```
Final text
```{r}
1 + 2
```
15 changes: 15 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Package: xyzpackage
Title: What the Package Does (one line, title case)
Version: 0.0.0.9000
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
Description: What the package does (one paragraph).
Depends: R (>= 3.3.2)
License: What license is it under?
Encoding: UTF-8
LazyData: true
Suggests: testthat
LinkingTo:
Rcpp
Imports:
Rcpp
RoxygenNote: 6.0.1.9000
1 change: 1 addition & 0 deletions tests/testthat/public-api/xyzpackage-qmd/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Generated by roxygen2: do not edit by hand
6 changes: 6 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

timesTwo <- function(x) {
.Call("_xyzpackage_timesTwo", PACKAGE = "xyzpackage", x)
}
3 changes: 3 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/R/hello-world.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello_world <- function() {
print("hello, world")
}
33 changes: 33 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# styler

The goal of styler is to ...

## Installation

You can install styler from github with:

```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("jonmcalder/styler")
```

## Example

This is a basic example which shows you how to solve a common problem:

```{r example}
## basic example code
```
19 changes: 19 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/new.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
output:
github_document:
html_preview: true
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

Some text
```{r}
# Some R code
f <- function(x) {
x
}
```
Final text
```{r}
1 + 2
```
3 changes: 3 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.dll
*.o
*.so
28 changes: 28 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

// timesTwo
NumericVector timesTwo(NumericVector x);
RcppExport SEXP _xyzpackage_timesTwo(SEXP xSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< NumericVector >::type x(xSEXP);
rcpp_result_gen = Rcpp::wrap(timesTwo(x));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_xyzpackage_timesTwo", (DL_FUNC) &_xyzpackage_timesTwo, 1},
{NULL, NULL, 0}
};

RcppExport void R_init_xyzpackage(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
27 changes: 27 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/src/timesTwo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <Rcpp.h>
using namespace Rcpp;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//

// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}


// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
//

/*** R
timesTwo(42)
*/
4 changes: 4 additions & 0 deletions tests/testthat/public-api/xyzpackage-qmd/tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(xyzpackage)

test_check("xyzpackage")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
context("testing styler on package")

test_that("hi there", {
I(am(a(package(x))))
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: "Vignette Title"
author: "Vignette Author"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

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:

- Never uses retina figures
- Has a smaller default figure size
- Uses a custom CSS stylesheet instead of the default Twitter Bootstrap style

## Vignette Info

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.

## Styles

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:

output:
rmarkdown::html_vignette:
css: mystyles.css

## Figures

The figure sizes have been customised so that you can easily put two images side-by-side.

```{r, fig.show='hold'}
plot(1:10)
plot(10:1)
```

You can enable figure captions by `fig_caption: yes` in YAML:

output:
rmarkdown::html_vignette:
fig_caption: yes

Then you can use the chunk option `fig.cap = "Your figure caption."` in **knitr**.

## More Examples

You can write math expressions, e.g. $Y = X\beta + \epsilon$, footnotes^[A footnote here.], and tables, e.g. using `knitr::kable()`.

```{r, echo=FALSE, results='asis'}
knitr::kable(head(mtcars, 10))
```

Also a quote using `>`:

> "He who gives up [code] safety for [code] speed deserves neither."
([via](https://twitter.com/hadleywickham/status/504368538874703872))
Loading

0 comments on commit a749d87

Please sign in to comment.