Skip to content

Commit

Permalink
Allow whitespace after code headers in split_code_headers() (#678)
Browse files Browse the repository at this point in the history
Co-authored-by: Garrick Aden-Buie <garrick@adenbuie.com>
  • Loading branch information
rossellhayes and gadenbuie committed Mar 18, 2022
1 parent 6339fa0 commit 1c01ac2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
8 changes: 6 additions & 2 deletions R/staticimports.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ is_html_tag <- function(x) {
inherits(x, c("shiny.tag", "shiny.tag.list"))
}

knitr_engine_caption <- function(engine) {
knitr_engine_caption <- function(engine = NULL) {
if (is.null(engine)) {
engine <- "r"
}

switch(
tolower(engine),
"bash" = "Bash",
Expand Down Expand Up @@ -60,7 +64,7 @@ split_code_headers <- function(code, prefix = "section") {
code <- str_trim(code, character = "[\r\n]")
code <- strsplit(code, "\n")[[1]]

rgx_header <- "^(#+)([ -]*)(.+?)?\\s*----+$"
rgx_header <- "^\\s*(#+)([ -]*)(.+?)?\\s*----+\\s*$"
headers <- regmatches(code, regexec(rgx_header, code, perl = TRUE))
lines_headers <- which(vapply(headers, length, integer(1)) > 0)

Expand Down
2 changes: 1 addition & 1 deletion inst/staticexports/split_code_headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ split_code_headers <- function(code, prefix = "section") {
code <- str_trim(code, character = "[\r\n]")
code <- strsplit(code, "\n")[[1]]

rgx_header <- "^(#+)([ -]*)(.+?)?\\s*----+$"
rgx_header <- "^(#+)([ -]*)(.+?)?\\s*----+\\s*$"
headers <- regmatches(code, regexec(rgx_header, code, perl = TRUE))
lines_headers <- which(vapply(headers, length, integer(1)) > 0)

Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-staticimports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
test_that("split_code_headers()", {
target <- list(one = "1", two = "2")

# No whitespace after dashes
expect_equal(
split_code_headers(
"# one ----
1
# two ----
2"
),
target
)


# Whitespace after first header
expect_equal(
split_code_headers(
"# one ----
1
# two ----
2"
),
target
)

# Whitespace after subsequent headers
expect_equal(
split_code_headers(
"# one ----
1
# two ----
2"
),
target
)
})

0 comments on commit 1c01ac2

Please sign in to comment.