Skip to content

Commit

Permalink
get rid of the extra space in ```{r }
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Aug 30, 2023
1 parent 9f3ffca commit e9c949d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
12 changes: 7 additions & 5 deletions R/spin.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ spin = function(
block = strip_white(block) # rm white lines in beginning and end
if (!length(block)) next
if (length(opt <- grep(rc <- '^(#|--)+(\\+|-| ----+| @knitr)', block))) {
block[opt] = paste0(p[1L], gsub(paste0(rc, '\\s*|-*\\s*$'), '', block[opt]), p[2L])
opts = gsub(paste0(rc, '\\s*|-*\\s*$'), '', block[opt])
opts = paste0(ifelse(opts == '', '', ' '), opts)
block[opt] = paste0(p[1L], opts, p[2L])
# close each chunk if there are multiple chunks in this block
if (any(opt > 1)) {
j = opt[opt > 1]
Expand Down Expand Up @@ -132,9 +134,9 @@ spin = function(

.fmt.pat = list(
rnw = c('<<', '>>=', '@', '\\\\Sexpr{\\1}'),
rhtml = c('<!--begin.rcode ', '', 'end.rcode-->', '<!--rinline \\1 -->'),
rtex = c('% begin.rcode ', '', '% end.rcode', '\\\\rinline{\\1}'),
rrst = c('.. {r ', '}', '.. ..', ':r:`\\1`')
rhtml = c('<!--begin.rcode', '', 'end.rcode-->', '<!--rinline \\1 -->'),
rtex = c('% begin.rcode', '', '% end.rcode', '\\\\rinline{\\1}'),
rrst = c('.. {r', '}', '.. ..', ':r:`\\1`')
)

# determine how many backticks we need to wrap code blocks and inline code
Expand All @@ -149,7 +151,7 @@ spin = function(
i = '`'
b = '```'
}
c(paste0(b, '{r '), '}', b, paste0(i, 'r \\1 ', i))
c(paste0(b, '{r'), '}', b, paste0(i, 'r \\1 ', i))
}

#' Spin a child R script
Expand Down
40 changes: 18 additions & 22 deletions tests/testit/test-spin.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,24 @@ spin_w_tempfile = function(..., format = "Rmd") {
result
}

assert(
"spin() detects lines for documentation",
identical(spin_w_tempfile("#' test", "1 * 1", "#' test"),
c("test", "", "```{r }", "1 * 1", "```", "", "test")),
assert("spin() detects lines for documentation", {
(spin_w_tempfile("#' test", "1 * 1", "#' test") %==%
c("test", "", "```{r}", "1 * 1", "```", "", "test"))
# a multiline string literal contains the pattern of doc or inline
identical(spin_w_tempfile("code <- \"", "#' test\""),
c("", "```{r }", "code <- \"", "#' test\"", "```", "")),
identical(spin_w_tempfile("code <- \"", "{{ 1 + 1 }}", "\""),
c("", "```{r }", "code <- \"", "{{ 1 + 1 }}", "\"", "```", "")),
(spin_w_tempfile("code <- \"", "#' test\"") %==%
c("", "```{r}", "code <- \"", "#' test\"", "```", ""))
(spin_w_tempfile("code <- \"", "{{ 1 + 1 }}", "\"") %==%
c("", "```{r}", "code <- \"", "{{ 1 + 1 }}", "\"", "```", ""))
# a multiline symbol contains the pattern of doc or inline
identical(spin_w_tempfile("`", "#' test", "`"),
c("", "```{r }", "`", "#' test", "`", "```", "")),
identical(spin_w_tempfile("`", "{{ 1 + 1 }}", "`"),
c("", "```{r }", "`", "{{ 1 + 1 }}", "`", "```", ""))
)
(spin_w_tempfile("`", "#' test", "`") %==%
c("", "```{r}", "`", "#' test", "`", "```", ""))
(spin_w_tempfile("`", "{{ 1 + 1 }}", "`") %==%
c("", "```{r}", "`", "{{ 1 + 1 }}", "`", "```", ""))
})

assert(
"spin() uses proper number of backticks",
identical(spin_w_tempfile("{{ '`' }}"),
c("``r '`' ``")),
identical(spin_w_tempfile("{{`x`}}"),
c("``r `x` ``")),
identical(spin_w_tempfile("x <- '", "```", "'"),
c("", "````{r }", "x <- '", "```", "'", "````", ""))
)
assert("spin() uses proper number of backticks", {
(spin_w_tempfile("{{ '`' }}") %==% c("``r '`' ``"))
(spin_w_tempfile("{{`x`}}") %==% c("``r `x` ``"))
(spin_w_tempfile("x <- '", "```", "'") %==%
c("", "````{r}", "x <- '", "```", "'", "````", ""))
})

0 comments on commit e9c949d

Please sign in to comment.