# reset an environment using "objects" in a list reset_env = function(x = list(), envir) { rm(list = ls(envir, all.names = TRUE), envir = envir) list2env(x, envir) } # counters for elements to be cross-referenced (e.g., fig, tab) counters = local({ db = NULL list( get = function() db, inc = function(name) db[[name]] <<- (db[[name]] %||% 0L) + 1L, del = function(name) db <<- NULL ) }) # use PCRE by default (which seems to handle multibyte chars better) gregexpr = function(..., perl = TRUE) base::gregexpr(..., perl = perl) attr = function(...) base::attr(..., exact = TRUE) # exact attr() please `%|%` = function(x, y) if (length(x)) x else y if (getRversion() < '4.4.0') `%||%` = function(x, y) if (is.null(x)) y else x set_names = function(x, nm) { names(x) = nm; x } dropNULL = function(x) x[!vapply(x, is.null, logical(1))] # remove the
tag from HTML sans_p = function(x) gsub('^
]*>|(
)?\n$', '', x) # remove ugly single quotes, e.g., 'LaTeX' -> LaTeX sans_sq = function(x) gsub("(^|\\W)'([^']+)'(\\W|$)", '\\1\\2\\3', x) # remove YAML header (if title exists, convert it to h1) sans_yaml = function(x) { if (length(x) && grepl('^---\\s*?($|\n)', x[1])) { res = xfun::yaml_body(split_lines(x)) x = c(if (is.character(t <- res$yaml$title)) paste('#', t), res$body) } x } split_chunk = function(...) xfun::divide_chunk(..., use_yaml = FALSE) is_lang = function(x) is.symbol(x) || is.language(x) uapply = function(..., recursive = TRUE) unlist(lapply(...), recursive = recursive) .mapply = function(fun, ...) base::.mapply(fun, list(...), NULL) #' Convert some ASCII strings to HTML entities #' #' Transform ASCII strings `(c)` (copyright), `(r)` (registered trademark), #' `(tm)` (trademark), and fractions `n/m` into *smart* typographic HTML #' entities. #' @param text A character vector of the Markdown text. #' @return A character vector of the transformed text. #' @keywords internal smartypants = function(text) { text = split_lines(text) i = prose_index(text) r = '(?', x, fixed = TRUE) x } #' Add CSS/JS assets to HTML output #' #' While CSS/JS assets can be set via the `css`/`js` keys under the `meta` field #' of the `html` output format in YAML, this function provides another way to #' add them, which can be called in a code chunk to dynamically add assets. #' @param feature A character vector of features supported by CSS/JS, e.g., #' `c('article', 'callout')`. See the row names of `litedown:::assets` for all #' available features. Each feature will be mapped to CSS/JS. #' @param css,js Character vectors of CSS/JS assets. #' @return A vector of `` (CSS) or `' x = match_replace(x, r, function(z) { # de-dup assets (e.g., when vest() is called multiple times on the same asset) z[duplicated(z)] = '' z }) embed = c('https', 'local') %in% options[['embed_resources']] offline = options[['offline']] if (!any(embed, is.character(offline))) return(x) clean = options[['embed_cleanup']] # find images in