new_env = function(...) new.env(..., parent = emptyenv()) #' Parse R Markdown or R scripts #' #' Parse input into code chunks, inline code expressions, and text fragments: #' [crack()] is for parsing R Markdown, and [sieve()] is for R scripts. #' #' For R Markdown, a code chunk must start with a fence of the form ```` #' ```{lang} ````, where `lang` is the language name, e.g., `r` or `python`. The #' body of a code chunk can start with chunk options written in "pipe comments", #' e.g., `#| eval = TRUE, echo = FALSE` (the CSV syntax) or `#| eval: true` (the #' YAML syntax). An inline code fragment is of the form `` `{lang} source` `` #' embedded in Markdown text. #' @inheritParams mark #' @export #' @return A list of code chunks and text blocks: #' #' - Code chunks are of the form `list(source, type = "code_chunk", options, #' comments, ...)`: `source` is a character vector of the source code of a #' code chunk, `options` is a list of chunk options, and `comments` is a #' vector of pipe comments. #' #' - Text blocks are of the form `list(source, type = "text_block", ...)`. If #' the text block does not contain any inline code, `source` will be a #' character string (lines of text concatenated by line breaks), otherwise it #' will be a list with members that are either character strings (normal text #' fragments) or lists of the form `list(source, options, ...)` (`source` is #' the inline code, and `options` contains its options specified inside `` #' `{lang, ...}` ``). #' #' Both code chunks and text blocks have a list member named `lines` that #' stores their starting and ending line numbers in the input. #' @examples #' library(litedown) #' # parse R Markdown #' res = crack(c('```{r}\n1+1\n```', 'Hello, `pi` = `{r} pi` and `e` = `{r} exp(1)`!')) #' str(res) #' # evaluate inline code and combine results with text fragments #' txt = lapply(res[[2]]$source, function(x) { #' if (is.character(x)) x else eval(parse(text = x$source)) #' }) #' paste(unlist(txt), collapse = '') crack = function(input, text = NULL) { text = read_input(input, text); input = attr(text, 'input') xml = commonmark::markdown_xml(text, sourcepos = TRUE) rx_engine = '([a-zA-Z0-9_]+)' # only allow these characters for engine names r = paste0( '<(code|code_block) sourcepos="(\\d+):(\\d+)-(\\d+):(\\d+)"( info="[{]+', rx_engine, '[^"]*?[}]")? xml:space="[^>]*>([^<]*)<' ) m = match_all(xml, r, perl = TRUE)[[1]] %|% matrix(character(), 9) # code blocks must have non-empty info strings m = m[, m[2, ] != 'code_block' | m[8, ] != '', drop = FALSE] res = list() # add a block of text and the line range info add_block = function(l1, l2, ...) { res[[length(res) + 1]] <<- list(source = text[l1:l2], ..., lines = c(l1, l2)) res } n = length(text) i = 1L # the possible start line number of text blocks for (j in which(m[2, ] == 'code_block')) { # start (3) and end (5) line numbers for code chunks pos = as.integer(m[c(3, 5), j]); i1 = pos[1]; i2 = pos[2] # add the possible text block before the current code chunk if (i1 > i) add_block(i, i1 - 1L, type = 'text_block') # add the code chunk add_block(i1, i2, info = m[8, j], type = 'code_chunk') i = i2 + 1L # the earliest line for the next text block is next line } # if there are lines remaining, they must be a text block if (i <= n) add_block(i, n, type = 'text_block') if (!length(m)) return(res) set_error_handler(input) m = m[, m[2, ] == 'code', drop = FALSE] # find out inline code `{lang} expr` rx_inline = '^\\s*[{](.+?)[}]\\s+(.+?)\\s*$' # look for `r expr` if `{lang}` not found (for compatibility with knitr) if (!any(j <- grepl(rx_inline, m[9, ])) && getOption('litedown.enable.knitr_inline', FALSE)) { rx_inline = '^(r) +(.+?)\\s*$' j = grepl(rx_inline, m[9, ]) } m = m[, j, drop = FALSE] n_start = uapply(res, function(x) x$lines[1]) # starting line numbers j = findInterval(m[3, ], n_start) # find which block each inline code belongs to for (i in seq_len(ncol(m))) { b = res[[j[i]]]; l = b$lines # column position is based on bytes instead of chars; needs to be adjusted to the latter pos = char_pos(text, as.integer(m[3:6, i])) i1 = pos[1]; i2 = pos[3] # commonmark::markdown_xml(sourcepos = TRUE) gives wrong column info when # the line has leading spaces (which are ignored), so doublecheck here (in # theory we also need to consider the case i1 != i2 but that's a little too # complicated and may be rare, too) if (i1 == i2 && grepl('^\\s+', ti <- text[i1])) { mi = restore_html(m[9, i]) if (substring(ti, pos[2], pos[4]) != mi) { p = base::gregexpr(mi, ti, fixed = TRUE)[[1]] if (length(p) > 1 || p < 1) { save_pos(c(i1, i2)); stop( 'Unable to locate the inline code expression ', mi, '. Please file an issue to https://github.com/yihui/litedown/issues ', 'with a minimal reproducible example.' ) } pos[2] = p; pos[4] = p + attr(p, 'match.length') - 1 } } s = nchar(b$source) # calculate new position of code after we concatenate all lines of this block by \n b$col = c(b$col, c( sum(s[seq_len(i1 - l[1])] + 1) + pos[2], sum(s[seq_len(i2 - l[1])] + 1) + pos[4] )) b$pos = c(b$pos, pos) res[[j[i]]] = b } i1 = 0 # code chunk index # remove code fences, and extract code in text blocks for (j in seq_along(res)) { b = res[[j]] if (b$type == 'code_chunk') { code = b$source N = length(code) # a code block may be indented or inside a blockquote p = grep_sub('^([\t >]*)(`{3,}|~{3,}).*', '\\1\\2', code[1]) if (length(p) == 0) stop('Possibly malformed code block fence: ', code[1]) if (!grepl(sub('^[\t >]*', '', p), code[N])) stop( 'The fences of the code block do not match:\n\n', code[1], '\n', code[N] ) p = gsub('[`~]+$', '', p) if (p != '') { i = startsWith(code, p) # remove indentation or > code[i] = substr(code[i], nchar(p) + 1, nchar(code[i])) # trailing spaces in the prefix may have been trimmed: yihui/knitr#1446 code[!i] = gsub(gsub('(.+?)\\s+$', '^\\1', p), '', code[!i]) b$prefix = p } # possible comma-separated chunk options in header rx_opts = paste0('^(`{3,}|~{3,})\\s*([{]+)', rx_engine, '(.*?)\\s*[}]+\\s*$') o = match_one(code[1], rx_opts)[[1]] if (length(o)) { # if two or more `{` is used, we will write chunk fences to output if (nchar(o[3]) > 1) b$fences = c( sub('{{', '{', sub('}}\\s*$', '}', code[1]), fixed = TRUE), code[N] ) o = if (o[5] != '') csv_options(o[5]) } code = code[-c(1, N)] # remove fences save_pos(b$lines) code = split_chunk(b$info, code) b[c('source', 'options', 'comments')] = code[c('code', 'options', 'src')] # starting line number of code b$code_start = b$lines[1] + 1L + length(b$comments) # default label is chunk-i (or parent-label-i for child documents) i1 = i1 + 1 # merge chunk options from header with pipe comment options b$options = merge_list(list( label = sprintf('%s-%d', (if (isTRUE(.env$child)) reactor('label')) %||% 'chunk', i1) ), o, b$options) b$options$engine = b$info b$info = NULL # the info is stored in chunk options as `engine` } else if (length(p <- b$col) > 0) { p = matrix(p, nrow = 2) x = one_string(b$source) x1 = substring(x, p[1, ], p[2, ]) # code # then extract normal text p = rbind(p[1, ] - 1, p[2, ] + 1) p = matrix(c(1, p, nchar(x)), nrow = 2) x2 = substring(x, p[1, ], p[2, ]) # text # get rid of left-over backticks N = length(x2) x2[1] = gsub('`+$', '', x2[1]) # trailing ` of first x2[N] = gsub('^`+', '', x2[N]) # leading ` of last # ` at both ends for text in the middle if (N > 2) x2[2:(N - 1)] = gsub('^`+|`+$', '', x2[2:(N - 1)]) # see if the code is wrapped in $ $ d1 = substring(x2, nchar(x2), nchar(x2)) == '$' d2 = substring(x2, 1, 1) == '$' dollar = d1[-N] & d2[-1] # position of code c(row1, col1, row2, col2) pos = matrix(b$pos, nrow = 4) x = as.list(head(c(rbind(x2, c(x1, ''))), -1)) for (i in seq_len(N - 1)) { z = match_one(x1[i], rx_inline)[[1]][-1] p2 = pos[, i]; save_pos(p2) xi = list( source = z[2], pos = p2, options = csv_options(gsub('^([^,]+)', 'engine="\\1"', z[1])) ) if (dollar[i]) xi$math = TRUE x[[2 * i]] = xi } b$source = x } else { b$source = paste(b$source, collapse = '\n') } b$pos = b$col = NULL # positions not useful anymore res[[j]] = b } res } set_error_handler = function(input) { opts = options(xfun.handle_error.loc_fun = get_loc) oenv = as.list(.env) exit_call(function() { options(opts); reset_env(oenv, .env) }) .env$input = input # store the input name for get_loc() } # convert byte position to character position char_pos = function(x, p) { x2 = x[p[c(1, 3)]] # no need to convert if no multibyte chars if (all(nchar(x2) == nchar(x2, 'bytes'))) return(p) p2 = p[c(2, 4)] Encoding(x2) = 'bytes' x2 = substr(x2, 1, p2 - 1) # go back one char in case current column is multibyte Encoding(x2) = 'UTF-8' p[c(2, 4)] = nchar(x2) + 1L # go forward by one char if (p2[2] == 0) p[4] = 0L # boundary case: \n before the closing backtick p } #' @details For R scripts, text blocks are extracted by removing the leading #' `#'` tokens. All other lines are treated as R code, which can optionally be #' separated into chunks by consecutive lines of `#|` comments (chunk options #' are written in these comments). If no `#'` or `#|` tokens are found in the #' script, the script will be divided into chunks that contain smallest #' possible complete R expressions. #' @note For simplicity, [sieve()] does not support inline code expressions. #' Text after `#'` is treated as pure Markdown. #' #' It is a pure coincidence that the function names `crack()` and `sieve()` #' weakly resemble Carson Sievert's name, but I will consider adding a class #' name `sievert` to the returned value of `sieve()` if Carson becomes the #' president of the United States someday, which may make the value #' radioactive and introduce a new programming paradigm named _Radioactive #' Programming_ (in case _Reactive Programming_ is no longer fun or cool). #' @rdname crack #' @export #' @examples #' #' # parse R code #' res = sieve(c("#' This is _doc_.", '', '#| eval=TRUE', '# this is code', '1 + 1')) #' str(res) sieve = function(input, text = NULL) { text = read_input(input, text); input = attr(text, 'input') n = length(text) r = run_range(grepl("^#'( .+| *)$", text), is_blank(text)) nc = ncol(r) # no #' or #|: split code into smallest expressions if (nc == 0 && !any(startsWith(text, '#| '))) { res = xfun::split_source(text, TRUE, TRUE) res = .mapply(function(code, label) { l = attr(code, 'lines') list( source = c(code), type = 'code_chunk', lines = l, code_start = l[1], options = list(engine = 'r', label = label) ) }, res, sprintf('chunk-%d', seq_along(res))) return(res) } # split doc and code by #', and divide code by #| res = list() add_block = function(l1, l2, type = 'code_chunk', pipe = FALSE) { x = text[l1:l2] if (type == 'text_block') { el = list(source = one_string(sub("^#' ?", '', x))) } else { if (all(i <- is_blank(x))) return() # trim blank lines at both ends i2 = range(which(!i)) # first and last non-empty lines l1 = l1 + (i2[1] - 1) l2 = l2 - (length(i) - i2[2]) save_pos(c(l1, l2)) x = text[l1:l2] el = if (pipe) partition(x) else list(source = x) el$code_start = as.integer(l1) + length(el$comments) el$options$engine = 'r' } el$type = type el$lines = as.integer(c(l1, l2)) res[[length(res) + 1]] <<- el } partition = function(code) { code = split_chunk('r', code) set_names(code[c('code', 'options', 'src')], c('source', 'options', 'comments')) } # detect #| and split a block of code into chunks add_chunk = function(l1, l2) { x = text[l1:l2]; N = length(x) k = run_range(startsWith(x, '#| '))[1, ] if ((n <- length(k)) == 0) return(add_block(l1, l2)) if (k[1] > 1) { k = c(1, k); n = n + 1 } # make sure to scan from start for (i in seq_len(n)) { add_block(l1 - 1 + k[i], if (i == n) l2 else l1 - 1 + k[i + 1] - 1, pipe = TRUE) } } set_error_handler(input) i = 1 for (j in seq_len(nc)) { i1 = r[1, j]; i2 = r[2, j] if (i1 > i) add_chunk(i, i1 - 1) add_block(i1, i2, type = 'text_block') i = i2 + 1 } if (i <= n) add_chunk(i, n) # add possibly missing chunk labels i = vapply(res, function(x) x$type == 'code_chunk', FALSE) res[i] = .mapply(function(x, label) { if (is.null(x$options$label)) x$options$label = label x }, res[i], sprintf('chunk-%d', seq_len(sum(i)))) res } # ranges of TRUE runs in a logical vector run_range = function(x, extend = NULL) { r = rle(x); l = r$lengths; i = r$values i2 = cumsum(l); i1 = i2 - l + 1 i1 = i1[i]; i2 = i2[i] # include adjacent blank lines, e.g., a blank line before or after #' should be doc if (!is.null(extend)) { k = extend[pmax(i1 - 1, 1)] # check if previous line is empty i1[k] = i1[k] - 1 k = extend[pmin(i2 + 1, length(extend))] # check if next line is empty i2[k] = i2[k] + 1 # merge adjacent runs, e.g., 1:2 and 2:4 -> 1:4 k = NULL if ((n <- length(i1)) > 1) { k = i2[1:(n - 1)] < i1[2:n] i1 = i1[c(TRUE, k)]; i2 = i2[c(k, TRUE)] } } rbind(i1, i2) } # convert knitr's inline `r code` to litedown's `{r} code` convert_knitr = function(input) { x = read_utf8(input) r = '(? td)) { cat(x, sep = '', file = p_out) p_yes <<- TRUE } } # if error occurs, print error location with a clickable file link k = 0 # when exiting, k should be n + 1 on_error = function() { if (k > n) return() # blocks have been successfully fused p_bar(p_clr) ansi_link(input) message('Quitting from ', get_loc(nms[k])) } # suppress tidyverse progress bars and use cairo for bitmap devices (for # smaller plot files and possible parallel execution) opt = options(rstudio.notebook.executing = TRUE, bitmapType = bitmap_type()) on.exit({ options(opt); on_error() }, add = TRUE) # the chunk option `order` determines the execution order of chunks o = block_order(blocks, n) res = character(n) for (i in seq_len(n)) { k = o[i]; b = blocks[[k]]; save_pos(b$lines) p_bar(c(as.character(round((i - 1)/n * 100)), '%', ' | ', get_loc(nms[k]))) # record timing if requested if (!isFALSE(time <- timing_path())) t1 = Sys.time() res[k] = if (b$type == 'code_chunk') { one_string(fuse_code(b, blocks)) } else { one_string(fuse_text(b), '') } if (!isFALSE(time)) record_time(Sys.time() - t1, b$lines, nms[k]) p_bar(p_clr) } k = n + 1 res } # add ANSI link on input path if supported ansi_link = function(x) { if (length(x) && isTRUE(as.logical(Sys.getenv('RSTUDIO_CLI_HYPERLINKS')))) .env$input2 = sprintf( '\033]8;%s;file://%s\a%s\033]8;;\a', link_pos(), normalize_path(x), x ) } # use options(bitmapType = 'cairo') for bitmap devices on macOS if possible bitmap_type = function() { # xquartz has to be installed for cairo to work (I don't know why) if (xfun::is_macos() && capabilities('cairo') && Sys.which('xquartz') != '') 'cairo' else .Options$bitmapType } time_frame = function(s = NA_character_, l = integer(), lab = NA_character_, t = NA_real_) { data.frame(source = s, line1 = l[1], line2 = l[2], label = lab, time = t) } record_time = function(x, lines, label) { x = as.numeric(x) gc(FALSE) d = time_frame(.env$input %||% '#text', lines, label, x) .env$time = append(.env$time, list(d)) } #' Get the timing data of code chunks and text blocks in a document #' #' Timing can be enabled via the chunk option `time = TRUE` (e.g., set #' [litedown::reactor]`(time = TRUE)` in the first code chunk). After it is #' enabled, the execution time for code chunks and text blocks will be recorded. #' This function can be called to retrieve the timing data later in the document #' (e.g., in the last code chunk). #' @param threshold A number (time in seconds) to subset data with. Only rows #' with time above this threshold are returned. #' @param sort Whether to sort the data by time in the decreasing order. #' @param total Whether to append the total time to the data. #' @note By default, the data will be cleared after each call of [fuse()] and #' will not be available outside [fuse()]. To store the data persistently, you #' can set the `time` option to a file path. This is necessary if you want to #' get the timing data for multiple input documents (such as all chapters of a #' book). Each document needs to point the `time` option to the same path. #' When you do not need timing any more, you will need to delete this file by #' yourself. #' @return A data frame containing input file paths, line numbers, chunk labels, #' and time. If no timing data is available, `NULL` is returned. #' @export timing_data = function(threshold = 0, sort = TRUE, total = TRUE) { d = .env$time if (!is.null(d)) d = do.call(rbind, d) if (is.character(path <- timing_path())) { if (file_exists(path)) { d2 = readRDS(path) if (length(input <- .env$input)) d2 = subset(d2, source != input) d = rbind(d2, d) } saveRDS(d, path) } if (is.null(d)) return(invisible(d)) total = if (total) sum(d$time) # add edit links in the roam() mode if (is_roaming() && !all(i <- d$source == '#text')) { d$source = ifelse(i, '', sprintf( '%s [✎](?path=%s&line=%d)', d$source, URLencode(d$source, TRUE), d$line1 )) } d = d[d$time > threshold, ] if (sort) d = d[order(d$time, decreasing = TRUE), ] if (!is.null(total)) d = rbind(d, time_frame('Total', t = total)) d } timing_path = function() { p = xfun::env_option('litedown.time') if (is.character(p) && tolower(p) %in% c('true', 'false')) p = as.logical(p) if (is.logical(p) || (is.character(p) && p != '')) p else reactor('time') } # an internal function for RStudio IDE to recognize the custom knit function # when users hit the Knit button knit = function(input, ...) fuse(input, envir = parent.frame()) fuse_code = function(x, blocks) { # merge local chunk options into global options old = reactor(x$options); on.exit(reactor(old), add = TRUE) opts = reactor() # delayed assignment to evaluate a chunk option only when it is actually used lapply(setdiff(names(opts), 'order'), function(i) { # skip the `order` option since it needs to be eval()ed in block_order() if (i != 'order' && is_lang(o <- opts[[i]])) delayedAssign(i, eval(o, fuse_env()), environment(), opts) }) # set the working directory before evaluating anything else change_wd(opts$wd) # fuse child documents (empty the `child` option to avoid infinite recursion) if (length(opts$child)) return(uapply(reactor(child = NULL)$child, function(.) { child = .env$child; .env$child = TRUE; on.exit(.env$child <- child) fuse(., output = 'markdown', envir = fuse_env(), quiet = TRUE) })) lab = opts$label; lang = opts$engine # the source code could be from chunk options 'file' or 'code' test_source = function(name) { if (length(opts[[name]]) == 0) return(FALSE) if (cond <- length(x$source) > 0) warning( "The chunk option '", name, "' is ignored for the non-empty chunk:\n\n", one_string(x$source) ) !cond } if (test_source('file')) { x$source = read_all(opts$file) } else if (test_source('code')) { x$source = opts$code } else { # use code from other chunks of the same label labs = if (length(x$source) == 0) which(names(blocks) == lab) if (length(labs)) x$source = uapply(blocks[labs], `[[`, 'source') } # resolve inline chunk references and do code interpolation x$source = fill_source(x$source, opts$fill, blocks) res = if (isFALSE(opts$eval)) list(new_source(x$source)) else { if (is.function(eng <- engines(lang))) eng(x) else list( new_source(x$source), new_warning(sprintf("The engine '%s' is not supported.", lang)) ) } if (!opts$include) return('') # if the engine result contains new chunk options, apply the new options, and # make sure they will be properly restored via `old` on exit if (length(opts_new <- attr(res, 'options'))) { old2 = reactor(opts_new) for (i in setdiff(names(old2), names(old))) old[i] = old2[i] } # decide the number of backticks to wrap up output fence = xfun::make_fence(c(unlist(res), x$fences)) # deal with figure alt text, captions, and environment env = opts$fig.env; alt = opts$fig.alt; cap = opts$fig.cap att = if (is.null(att <- opts$attr.plot)) '' else paste0('{', att, '}') if (is.null(alt)) alt = cap p1 = Filter(function(x) !is_plot(x), res) p2 = Filter(is_plot, res) p3 = unlist(p2) # vector of plot paths # get the relative path of the plot directory fig.dir = if (length(p3)) tryCatch( sub('^[.]/', '', paste0(dirname(relative_path(p3[1], .env$wd_out)), '/')), error = function(e) NULL ) # record plot paths so they can be cleaned up if option embed_cleanup = true; # however, when cache = true, we shouldn't clean up plots since they won't be # regenerated next time (then they won't be found) if (!isTRUE(opts$cache)) .env$plot_files = c(.env$plot_files, p3) # recycle alt and attributes for all plots pn = length(p3) if (pn && is.null(alt)) { # reminder about missing alt text if this option is set to TRUE if (getOption('litedown.fig.alt', FALSE)) message( "\nPlease provide a 'fig.alt' option to the code chunk at ", get_loc(lab) ) alt = '' } alt = rep(alt, length.out = pn) att = rep(att, length.out = pn) # if figure caption is provided, merge all plots in one env if (pn && length(cap)) res = c(xfun:::merge_record(p1), list(new_plot(p3))) i = 0 # plot counter res_show = opts$results # normalize the 'results' option if (is.logical(res_show)) res_show = if (res_show) 'markup' else 'hide' l1 = x$code_start # starting line number of the whole code chunk # filter the results if desired if (!is.null(opts$filter)) res = match.fun(opts$filter)(res) # generate markdown output out = lapply(res, function(x) { type = grep_sub('^record_', '', class(x))[1] if (is.na(type)) type = 'output' if (type == 'source') { if (!opts$echo) return() l2 = attr(x, 'lines')[1] # starting line number of a code block x = one_string(x) if (opts$strip.white) x = trim_blank(x) } asis = if (type %in% c('output', 'asis')) { if (res_show == 'hide') return() any(c(res_show, type) == 'asis') } else FALSE if (type == 'warning' && !isTRUE(opts$warning)) return() if (type == 'message' && !isTRUE(opts$message)) return() if (type == 'plot') { n = length(x); i2 = i + seq_len(n); i <<- i + n img = sprintf( '![%s](<%s>)%s', alt[i2], if (is.null(fig.dir)) x else gsub('^.*/', fig.dir, x), att[i2] ) add_cap(img, cap, lab, opts$cap.pos, env) } else { a = opts[[paste0('attr.', type)]] if (type == 'source') { # use engine name as class name when `a` is not provided if (is.null(a)) a = paste0('.', lang) # add line numbers if (is_roaming()) a = c(a, lineno_attr(NA, l1 + l2 - 1)) } else { if (type == 'message') x = sub('\n$', '', x) if (!asis) { opt2 = attr(x, 'opts') cmt = opts$comment %||% opt2$comment %||% '#> ' if (cmt != '') { x = split_lines(x) x = paste0(cmt, x) # comment out text output } if (is.null(a)) if (!is.null(a <- opt2$attr)) a = c(a, '.plain') } } if (asis) { if (is.null(a)) x else fenced_div(x, a) } else fenced_block(x, a, fence) } }) out = dropNULL(out) # collapse a code block without attributes into previous adjacent code block # (also try to collapse for results = 'hide') collapse = isTRUE(opts$collapse) || res_show == 'hide' if (collapse && (n <- length(out)) > 1) { i1 = 1; k = NULL # indices of elements to be removed from `out` for (i in 2:n) { if (i - i1 > 1) i1 = i - 1 # make sure blocks are adjacent o1 = out[[i1]]; n1 = length(o1); e1 = o1[n1] # previous block should have a closing fence ```+ if (n1 < 3 || !grepl('^```+$', e1)) next o2 = out[[i]] if (continue_block(o1[2], e1, head(o2, 2))) { out[[i]] = c(o1[-n1], o2[-(1:2)]) k = c(k, i1) # merge previous block into current and remove previous } } if (length(k)) out = out[-k] } a = opts$attr.chunk if (length(x$fences) == 2) { # add a class name to the chunk output so we can style it differently a = c(a, '.fenced-chunk') out = add_fences(out, x, fence) } out = unlist(out) if (!is.null(a)) out = fenced_div(out, a) # if first line of chunk output is empty, remove it (the chunk should have had # an empty line before it in the source) if (length(out) && out[1] == '') out = out[-1] # add prefix (possibly indentation and > quote chars) if (!is.null(x$prefix)) out = gsub('^|(?<=\n)', x$prefix, out, perl = TRUE) out } # resolve `