R/hooks-chunk.R

Defines functions run_hooks

# hooks that return character values will be inserted into final output
run_hooks = function(before, options, envir = knit_global()) {
  # default and user-defined new hooks
  hooks.n = knit_hooks$get()
  hooks.a = hooks.n[setdiff(names(hooks.n), names(.default.hooks))] # a list of hooks to run
  out = NULL
  nms = intersect(names(options), names(hooks.a))
  if (!before) nms = rev(nms)
  for (i in nms) {
    # run only when option is not NULL, and hook is not NULL
    if (is.null(options[[i]]) || is.null(hook <- hooks.a[[i]])) next
    if (is.character(hook)) hook = get(hook, envir = envir, mode = 'function')
    args = list(before = before, options = options, envir = envir, name = i)
    args = args[names(formals(hook))]
    res  = do.call(hook, args, envir = envir)
    if (is.character(res)) out = c(out, res)
  }
  out
}

Try the knitr package in your browser

Any scripts or data that you put into this service are public.

knitr documentation built on Nov. 2, 2023, 5:49 p.m.