Skip to content

Commit b5c7ddd

Browse files
support xaringan
1 parent de54d5b commit b5c7ddd

File tree

8 files changed

+117
-13
lines changed

8 files changed

+117
-13
lines changed

R/expr-is.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,18 @@ is_subset_expr <- function(pd) {
110110
#' style_text("#!/usr/bin/env Rscript")
111111
#' @keywords internal
112112
is_shebang <- function(pd) {
113-
is_first_comment <- is_comment(pd) & (pd$pos_id == 1L)
113+
is_first_comment <- pd$pos_id == 1L
114114
is_first_comment[is_first_comment] <- grepl(
115115
"^#!", pd$text[is_first_comment],
116116
perl = TRUE
117117
)
118118
is_first_comment
119119
}
120120

121-
#' Identify spinning code chunk header
121+
#' Identify spinning code chunk header or xaringan
122122
#'
123+
#' Wrongly identifies a comment without a preceding line break as a code chunk
124+
#' header.
123125
#' See https://yihui.name/knitr/demo/stitch/#spin-comment-out-texts for details.
124126
#' @examples
125127
#' style_text(c(
@@ -130,10 +132,10 @@ is_shebang <- function(pd) {
130132
#' ))
131133
#' @param pd A parse table.
132134
#' @keywords internal
133-
is_code_chunk_header <- function(pd) {
135+
is_code_chunk_header_or_xaringan <- function(pd) {
134136
is_comment <- is_comment(pd)
135137
is_comment[is_comment] <- grepl(
136-
"^#[\\+|\\-]", pd$text[is_comment],
138+
"^#[\\+|\\-|<<]", pd$text[is_comment],
137139
perl = TRUE
138140
)
139141
is_comment

R/rules-spacing.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,19 @@ set_space_between_levels <- function(pd_flat) {
294294
#' @importFrom purrr map_chr
295295
#' @keywords internal
296296
start_comments_with_space <- function(pd, force_one = FALSE) {
297-
comment_pos <- is_comment(pd) & !is_shebang(pd) & !is_code_chunk_header(pd)
298-
if (!any(comment_pos)) {
297+
is_comment <- is_comment(pd)
298+
299+
if (any(is_comment)) {
300+
is_comment <- is_comment & !is_shebang(pd) & !is_code_chunk_header_or_xaringan(pd)
301+
if (!any(is_comment)) {
302+
return(pd)
303+
}
304+
} else {
299305
return(pd)
300306
}
301307

302308
comments <- rematch2::re_match(
303-
pd$text[comment_pos],
309+
pd$text[is_comment],
304310
"^(?<prefix>#+['\\*]*)(?<space_after_prefix> *)(?<text>.*)$"
305311
)
306312
comments$space_after_prefix <- nchar(
@@ -312,14 +318,14 @@ start_comments_with_space <- function(pd, force_one = FALSE) {
312318
force_one
313319
)
314320

315-
pd$text[comment_pos] <-
321+
pd$text[is_comment] <-
316322
paste0(
317323
comments$prefix,
318324
map_chr(comments$space_after_prefix, rep_char, char = " "),
319325
comments$text
320326
) %>%
321327
trimws("right")
322-
pd$short[comment_pos] <- substr(pd$text[comment_pos], 1, 5)
328+
pd$short[is_comment] <- substr(pd$text[is_comment], 1, 5)
323329
pd
324330
}
325331

inst/WORDLIST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ Walthert
203203
withr
204204
writeLines
205205
www
206+
xaringan
206207
xenial
207208
xfun
208209
Xie

man/is_code_chunk_header.Rd renamed to man/is_code_chunk_header_or_xaringan.Rd

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo(
2+
data = mtcars,
3+
x = cyl,
4+
y = wt #<<
5+
)
6+
7+
8+
library(ggplot2)
9+
10+
ggplot(aes(x, y), data) +
11+
geom_point() + #<<
12+
scale_x_continuous() #<<

tests/testthat/parse_comments/xaringan-in_tree

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
foo(
2+
data = mtcars,
3+
x = cyl,
4+
y = wt #<<
5+
)
6+
7+
8+
library(ggplot2)
9+
10+
ggplot(aes(x, y), data) +
11+
geom_point() + #<<
12+
scale_x_continuous() #<<

tests/testthat/test-parse_comments.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ test_that("hashbangs are respected", {
5555
), NA)
5656
})
5757

58+
test_that("xaringan markers are respected", {
59+
expect_warning(test_collection("parse_comments",
60+
"xaringan",
61+
transformer = style_text
62+
), NA)
63+
})
64+
5865
test_that("code chunk headers for spinning are respected", {
5966
expect_warning(test_collection("parse_comments",
6067
"spinning_code_chunk_headers",

0 commit comments

Comments
 (0)