From d16beaf59caf21af5ccfa4b723cd28b350a48576 Mon Sep 17 00:00:00 2001 From: Joe Cheng Date: Wed, 11 Apr 2012 11:23:14 -0700 Subject: [PATCH] Introduce chunk.end.is.terminator option Also changes gfm mode to have ``` chunk.end instead of ```` --- R/defaults.R | 3 ++- R/parser.R | 20 ++++++++++++++++++++ R/pattern.R | 3 ++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/R/defaults.R b/R/defaults.R index f29d983581..b79484d22e 100644 --- a/R/defaults.R +++ b/R/defaults.R @@ -109,7 +109,8 @@ all_patterns = ref.label = '^## @knitr (.*)$'), `md` = list(chunk.begin = '^``` \\{r(.*)\\}\\s*$', - chunk.end = '^````\\s*$', + chunk.end = '^```\\s*$', + chunk.end.is.terminator = TRUE, ref.chunk = '^\\s*<<(.*)>>\\s*$', inline.code = '`r\\s+([^`]*)\\s*`', global.options = '`ro\\s+([^`]*)\\s+or`', diff --git a/R/parser.R b/R/parser.R index 568eab01f4..227a40d5e7 100644 --- a/R/parser.R +++ b/R/parser.R @@ -16,6 +16,8 @@ split_file = function(path, lines = readLines(path, warn = FALSE), set.preamble blks = str_detect(lines, chunk.begin) txts = str_detect(lines, chunk.end) + if (isTRUE(as.logical(knit_patterns$get('chunk.end.is.terminator')))) + txts = filter_chunk_end(blks, txts) tmp = logical(n); tmp[blks | txts] = TRUE; lines[txts] = '' @@ -271,3 +273,21 @@ parse_chunk = function(x) { }), use.names = FALSE) x } + +## filter chunk.end lines that don't actually end a chunk +filter_chunk_end = function(chunk.begin, chunk.end) { + in.chunk = FALSE + fun = function(is.begin, is.end) { + if (in.chunk && is.end) { + in.chunk <<- FALSE + return(TRUE) + } + else if (!in.chunk && is.begin) { + in.chunk <<- TRUE + return(FALSE) + } + else + return(FALSE) + } + mapply(fun, chunk.begin, chunk.end) +} diff --git a/R/pattern.R b/R/pattern.R index 6652df6ea8..56f491996f 100644 --- a/R/pattern.R +++ b/R/pattern.R @@ -1,5 +1,6 @@ ## initial pattern list -.pat.init = list(chunk.begin = NULL, chunk.end = NULL, chunk.code = NULL, +.pat.init = list(chunk.begin = NULL, chunk.end = NULL, + chunk.end.is.terminator = NULL, chunk.code = NULL, inline.code = NULL, global.options = NULL, input.doc = NULL, ref.chunk = NULL, header.begin = NULL, document.begin = NULL, ref.label = NULL)