| 1 |
# hooks that return character values will be inserted into final output |
| 2 |
run_hooks = function(before, options, envir = knit_global()) { |
| 3 |
# default and user-defined new hooks |
| 4 |
hooks.n = knit_hooks$get() |
| 5 |
hooks.a = hooks.n[setdiff(names(hooks.n), names(.default.hooks))] # a list of hooks to run |
| 6 |
out = NULL |
| 7 |
nms = intersect(names(options), names(hooks.a)) |
| 8 |
if (!before) nms = rev(nms) |
| 9 |
for (i in nms) { |
| 10 |
# run only when option is not NULL, and hook is not NULL |
| 11 |
if (is.null(options[[i]]) || is.null(hook <- hooks.a[[i]])) next |
| 12 |
if (is.character(hook)) hook = get(hook, envir = envir, mode = 'function') |
| 13 |
args = list(before = before, options = options, envir = envir, name = i) |
| 14 |
anms = names(formals(hook)) |
| 15 |
if (!('...' %in% anms)) args = args[anms] |
| 16 |
res = do.call(hook, args, envir = envir) |
| 17 |
if (is.character(res)) out = c(out, res) |
| 18 |
} |
| 19 |
out |
| 20 |
} |