Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move split_code_headers() to gradethis #313

Merged
merged 3 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion R/gradethis-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ NULL
# @staticimports pkg:learnr
# is_AsIs
# is_html_tag is_html_chr is_html_any
# split_code_headers
# str_trim
# knitr_engine_caption

Expand Down
47 changes: 47 additions & 0 deletions R/solutions.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,50 @@ code_standardize_string <- function(code, scalar = TRUE) {
code
}
}

split_code_headers <- function(code, prefix = "section") {
if (is.null(code)) {
return(NULL)
}

code <- paste(code, collapse = "\n")
code <- str_trim(code, character = "[\r\n]")
code <- strsplit(code, "\n")[[1]]

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

if (length(lines_headers) > 0 && max(lines_headers) == length(code)) {
# nothing after last heading
lines_headers <- lines_headers[-length(lines_headers)]
}

if (!length(lines_headers)) {
return(list(paste(code, collapse = "\n")))
}

# header names are 3rd group, so 4th place in match since 1st is the whole match
header_names <- vapply(headers[lines_headers], `[[`, character(1), 4)
header_names <- str_trim(header_names)
if (any(!nzchar(header_names))) {
header_names[!nzchar(header_names)] <- sprintf(
paste0(prefix, "%02d"),
which(!nzchar(header_names))
)
}

rgx_header_line <- gsub("[$^]", "(^|\n|$)", rgx_header)
sections <- strsplit(paste(code, collapse = "\n"), rgx_header_line, perl = TRUE)[[1]]
if (length(sections) > length(header_names)) {
header_names <- c(paste0(prefix, "00"), header_names)
}
names(sections) <- header_names

# trim leading/trailing new lines from code section
sections <- str_trim(sections, character = "[\r\n]")
# drop any sections that don't have anything in them
sections <- sections[nzchar(str_trim(sections))]

as.list(sections)
}
47 changes: 0 additions & 47 deletions R/staticimports.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,53 +55,6 @@ knitr_engine_caption <- function(engine = NULL) {
)
}

split_code_headers <- function(code, prefix = "section") {
if (is.null(code)) {
return(NULL)
}

code <- paste(code, collapse = "\n")
code <- str_trim(code, character = "[\r\n]")
code <- strsplit(code, "\n")[[1]]

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

if (length(lines_headers) > 0 && max(lines_headers) == length(code)) {
# nothing after last heading
lines_headers <- lines_headers[-length(lines_headers)]
}

if (!length(lines_headers)) {
return(list(paste(code, collapse = "\n")))
}

# header names are 3rd group, so 4th place in match since 1st is the whole match
header_names <- vapply(headers[lines_headers], `[[`, character(1), 4)
header_names <- str_trim(header_names)
if (any(!nzchar(header_names))) {
header_names[!nzchar(header_names)] <- sprintf(
paste0(prefix, "%02d"),
which(!nzchar(header_names))
)
}

rgx_header_line <- gsub("[$^]", "(^|\n|$)", rgx_header)
sections <- strsplit(paste(code, collapse = "\n"), rgx_header_line, perl = TRUE)[[1]]
if (length(sections) > length(header_names)) {
header_names <- c(paste0(prefix, "00"), header_names)
}
names(sections) <- header_names

# trim leading/trailing new lines from code section
sections <- str_trim(sections, character = "[\r\n]")
# drop any sections that don't have anything in them
sections <- sections[nzchar(str_trim(sections))]

as.list(sections)
}

str_trim <- function(x, side = "both", character = "\\s") {
if (side %in% c("both", "left", "start")) {
rgx <- sprintf("^%s+", character)
Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/test-solutions.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,41 @@ runif(2 - 1)'
"# but maybe this one would work, too\nrunif(2 - 1)"
)
})

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
)
})