Skip to content

Commit

Permalink
simplify filter_chunk_md
Browse files Browse the repository at this point in the history
  • Loading branch information
atusy committed Sep 25, 2021
1 parent d6b6e6c commit c302685
Showing 1 changed file with 4 additions and 25 deletions.
29 changes: 4 additions & 25 deletions R/parser.R
Original file line number Diff line number Diff line change
Expand Up @@ -400,38 +400,17 @@ parse_chunk = function(x, rc = knit_patterns$get('ref.chunk')) {

# filter chunk.end lines that don't actually end a chunk
filter_chunk_end = function(chunk.begin, chunk.end, lines = NULL, patterns = NULL) {
keys = c('chunk.begin', 'chunk.end')
if (identical(patterns[keys], all_patterns[['md']][keys])) {
filter_chunk_end_md(chunk.begin, chunk.end, lines)
} else {
filter_chunk_end_general(chunk.begin, chunk.end)
}
}

filter_chunk_end_general = function(chunk.begin, chunk.end) {
in.chunk = FALSE
fun = function(is.begin, is.end) {
if (in.chunk && is.end) {
in.chunk <<- FALSE
return(TRUE)
}
if (!in.chunk && is.begin) in.chunk <<- TRUE
FALSE
}
mapply(fun, chunk.begin, chunk.end)
}

filter_chunk_end_md = function(chunk.begin, chunk.end, lines) {
in.chunk = FALSE
expected_end = NA_character_
is.md = identical(patterns, all_patterns[['md']])
pattern.end = NA_character_
fun = function(is.begin, is.end, line) {
if (in.chunk && is.end && grepl(expected_end, line)) {
if (in.chunk && is.end && (is.na(pattern.end) || grepl(pattern.end, line))) {
in.chunk <<- FALSE
return(TRUE)
}
if (!in.chunk && is.begin) {
in.chunk <<- TRUE
expected_end <<- paste0(sub('(^[\t >]*```+).*', '\\1', line), "\\s*$")
if (is.md) pattern.end <<- paste0(sub('(^[\t >]*```+).*', '\\1', line), "\\s*$")
}
FALSE
}
Expand Down

0 comments on commit c302685

Please sign in to comment.