#' Find the indices of lines in Markdown that are prose (not code blocks) #' #' Filter out the indices of lines between code block fences such as \verb{```} #' (could be three or four or more backticks). #' @param x A character vector of text in Markdown. #' @param warn Whether to emit a warning when code fences are not balanced. #' @note If the code fences are not balanced (e.g., a starting fence without an #' ending fence), this function will treat all lines as prose. #' @return An integer vector of indices of lines that are prose in Markdown. #' @export #' @examples library(xfun) #' prose_index(c('a', '```', 'b', '```', 'c')) #' prose_index(c('a', '````', '```r', '1+1', '```', '````', 'c')) prose_index = function(x, warn = TRUE) { xi = seq_along(x); n = length(idx <- code_lines_regex(x)) if (n == 0) return(xi) if (n %% 2 != 0) { if (warn) warning('Code fences are not balanced') # treat all lines as prose return(xi) } idx2 = matrix(idx, nrow = 2) idx2 = unlist(mapply(seq, idx2[1, ], idx2[2, ], SIMPLIFY = FALSE)) xi[-idx2] } # find starting and ending lines of code blocks via regex (may not be accurate) code_lines_regex = function(x) { idx = NULL # if raw HTML
 exists, it should be treated as code block
  inside_pre = if (length(p1 <- grep('
', x))) {
    p2 = grep('
', x) if (length(p1) == length(p2)) { idx = rbind(p1, p2) function(i) any(i > p1 & i < p2) } } r = '^(\\s*```+).*'; s = '' # shouldn't match ``` ``text``` ```, which is inline code, not code block i1 = grepl(r, x); i2 = !grepl('^\\s*```+\\s+`', x); i3 = !grepl('-->\\s*$', x) for (i in which(i1 & i2 & i3)) { if (is.function(inside_pre) && inside_pre(i)) next if (s == '') { s = gsub(r, '\\1', x[i]); idx = c(idx, i); next } # look for the next line with the same amount of backticks (end of block) if (grepl(paste0('^', s), x[i])) { idx = c(idx, i); s = '' } } idx } # find lines via commonmark (accurate but gregexpr()/substring() are slow) code_lines_cmark = function(x) { xml = commonmark::markdown_xml(x, sourcepos = TRUE) r = '(?<=) m = gregexpr('(?<=^|[\\s(])[$](?![ `])[^$]+?(?Download filename}. The file can be downloaded when #' the link is clicked in modern web browsers. For a directory, it will be #' compressed as a zip archive first, and the zip file is passed to #' `embed_file()`. For multiple files, they are also compressed to a zip file #' first. #' #' These functions can be called in R code chunks in R Markdown documents with #' HTML output formats. You may embed an arbitrary file or directory in the HTML #' output file, so that readers of the HTML page can download it from the #' browser. A common use case is to embed data files for readers to download. #' @param path Path to the file(s) or directory. #' @param name The default filename to use when downloading the file. Note that #' for `embed_dir()`, only the base name (of the zip filename) will be used. #' @param text The text for the hyperlink. #' @param ... For `embed_file()`, additional arguments to be passed to #' [html_tag()] (e.g., `class = 'foo'`). For `embed_dir()` and #' `embed_files()`, arguments passed to `embed_file()`. #' @note Windows users may need to install Rtools to obtain the \command{zip} #' command to use `embed_dir()` and `embed_files()`. #' @return An HTML tag \samp{} with the appropriate attributes. #' @export #' @examples #' logo = xfun:::R_logo() #' link = xfun::embed_file(logo, text = 'Download R logo') #' link #' if (interactive()) xfun::html_view(link) embed_file = function(path, name = basename(path), text = paste('Download', name), ...) { h = base64_uri(path) html_tag('a', text, href = h, download = name, ...) } #' @rdname embed_file #' @export embed_dir = function(path, name = paste0(normalize_path(path), '.zip'), ...) { name = gsub('/', '', basename(name)) in_dir(path, { name = file.path(tempdir(), name); on.exit(file.remove(name), add = TRUE) zip(name, '.'); embed_file(name, ...) }) } #' @rdname embed_file #' @export embed_files = function(path, name = with_ext(basename(path[1]), '.zip'), ...) { name = file.path(tempdir(), basename(name)) on.exit(file.remove(name), add = TRUE) zip(name, path) embed_file(name, ...) } zip = function(name, ...) { if (utils::zip(name, ...) != 0) stop('Failed to create the zip archive ', name) invisible(0) } #' Generate a simple Markdown pipe table #' #' A minimal Markdown table generator using the pipe `|` as column separators. #' #' The default argument values can be set via global options with the prefix #' `xfun.md_table.`, e.g., `options(xfun.md_table.digits 2, xfun.md_table.na = #' 'n/a')`. #' @param x A 2-dimensional object (e.g., a matrix or data frame). #' @param digits The number of decimal places to be passed to [round()]. It can #' be a integer vector of the same length as the number of columns in `x` to #' round columns separately. The default is `3`. #' @param na A character string to represent `NA` values. The default is an #' empty string. #' @param newline A character string to substitute `\n` in `x` (because pipe #' tables do not support line breaks in cells). The default is a space. #' @param limit The maximum number of rows to show in the table. If it is #' smaller than the number of rows, the data in the middle will be omitted. If #' it is of length 2, the second number will be used to limit the number of #' columns. Zero and negative values are ignored. #' @param escape Whether to backslash-escape special Markdown characters #' (\verb{\\`, *, _, [, ], <, >, ~, \\}) in table cells. This prevents cell #' content from being interpreted as Markdown (e.g., `` being treated #' as a link). It can take a logical value (`TRUE` means all columns and #' `FALSE` means none), or a vector of column indices or names to escape #' selectively. The default is `FALSE`. #' @return A character vector. #' @seealso [knitr::kable()] (which supports more features) #' @export #' @examples #' xfun::md_table(head(iris)) #' xfun::md_table(mtcars, limit = c(10, 6)) #' xfun::md_table(data.frame(x = 'Hello ', y = 1), escape = TRUE) #' xfun::md_table(data.frame(x = 'Hello ', y = 1), escape = 'x') md_table = function(x, digits = NULL, na = NULL, newline = NULL, limit = NULL, escape = NULL) { if (length(d <- dim(x)) != 2) stop('xfun::md_table() only supports 2-dimensional objects.') if (d[2] == 0) return(character()) if (is.null(digits)) digits = getOption('xfun.md_table.digits', min(getOption('digits'), 3)) digits = rep(digits, d[2]) # recycle for all columns num = logical(d[2]) # numeric columns for (j in seq_len(d[2])) if (is.numeric(x[, j])) { num[j] = TRUE x[, j] = round(x[, j], digits[j]) } is_na = is.na(x) x = as.matrix(format(x)) if (any(is_na)) x[is_na] = na %||% getOption('xfun.md_table.na', '') rn = rownames(x) # ignore empty and automatic row names (integers) if (!all(grepl('^[0-9]*$', rn))) { x = cbind(' ' = rn, x) num = c(FALSE, num) } # get first and last limit/2 rows/cols in N rows/cols if (length(limit <- limit %||% getOption('xfun.md_table.limit'))) { # subset rows l1 = limit[1] if (l1 > 0 && l1 < d[1]) { n1 = round(l1/2); n2 = l1 - n1 x = rbind(head(x, n1), '⋮', tail(x, n2)) } # subset columns if (length(limit) >= 2 && (l2 <- limit[2]) > 0 && l2 < d[2]) { n1 = round(l2/2); n2 = l2 - n1 x = cbind( x[, seq_len(n1), drop = FALSE], `...` = '...', x[, d[2] - seq_len(n2) + 1, drop = FALSE] ) num = c(head(num, n1), FALSE, tail(num, n2)) } d = dim(x) if (d[2] == 0) return(character()) } escape = escape %||% getOption('xfun.md_table.escape', FALSE) if (!isFALSE(escape)) { cols = if (isTRUE(escape)) seq_len(ncol(x)) else { if (is.character(escape)) match(escape, colnames(x)) else escape } cols = cols[!is.na(cols)] for (j in cols) x[, j] = gsub('([][\\\\`*_<>~])', '\\\\\\1', x[, j]) } cn = colnames(x) %||% rep(' ', d[2]) # table header a = ifelse(num, '--:', '---') # alignment a[cn == '...'] = ':-:' x = rbind(cn, a, x) d = dim(x) x = gsub('|', '\\|', x, fixed = TRUE) dim(x) = d res = do.call(function(...) paste(..., sep = '|'), as.data.frame(x)) res = gsub('\n', newline %||% getOption('xfun.md_table.newline', ' '), res, fixed = TRUE) paste0('|', res, '|') } #' Represent a (recursive) list with (nested) tabsets #' #' The tab titles are names of list members, and the tab content contains the #' values of list members. If a list member is also a list, it will be #' represented recursively with a child tabset. #' @param x A list for `tabset()`, and a character vector for `tab_content()`. #' @param value A function to print the value of a list member. By default, #' [str()] is used to print the structure of the value. You may also use #' [dput()] to output the full value, but it may be slow when the size of the #' value is too big. If the function prints the value to the console, the #' output will be captured and shown in a code block. If you want construct #' the tab content by yourself instead of capturing console output as a code #' block, you may pass the Markdown content to [tab_content()] and return it #' in this `value()` function. #' @return A character vector of Markdown that can be rendered to HTML with #' [litedown::mark()]. #' @export #' @examples #' xfun::tabset(iris) #' xfun::tabset(iris, dput) #' xfun::tabset(iris, print) #' #' # a deeply nested list #' plot(1:10) #' p = recordPlot() #' xfun::tabset(p) #' #' # custom tab content #' xfun::tabset(iris, function(x) { #' if (is.factor(x)) { #' res = c('A factor with levels: ', xfun::join_words(levels(x), before = '`')) #' xfun::tab_content(res) #' } else print(summary(x)) #' }) tabset = function(x, value = str) { obj = paste(deparse(substitute(x)), collapse = ' ') md_viewable(.tabset(x, value), meta = list( css = '@tabsets', js = '@tabsets', title = paste0('`xfun::tabset(', obj, ')`') )) } .tabset = function(x, value) { if (is.list(x)) { idx = seq_along(x) nms = names(x) %||% sprintf('[[%d]]', idx) res = NULL for (i in idx) { con = paste0(' ', .tabset(unclass(x)[[i]], value)) # indent content res = c(res, paste0('- `', nms[i], '`'), '', con) } # attach attributes to the last tab if necessary if (is.list(att <- attributes(x))) { att$names = NULL # names have been displayed if (length(att)) res = c( res, '', '- `attr(*)`', '', paste0(' ', .tabset(att, value)) ) } c('::: tabset', res, ':::') } else { out = capture.output(val <- value(x)) # if the value() function prints something, use a code block to show the # output; otherwise, show the value itself if it is a record_asis object if (length(out)) c('```r', out, '```') else as.character(if (inherits(val, 'record_asis')) val) } } #' @rdname tabset #' @export tab_content = function(x) invisible(new_record(x, 'asis')) md_viewable = function(x, ...) { structure(x, class = c('xfun_md_viewable', 'record_asis'), ...) } #' @export print.xfun_md_viewable = function(x, ...) { if (loadable('litedown')) { html = litedown::mark(text = c('---', '---', '', x), meta = attr2(x, 'meta'), ...) html_view(html) } else { warning("Cannot preview the result since the 'litedown' package is not available.") raw_string(x) } }