Debian Codegraph @

R/hooks-asciidoc.R

raw
1 #' @rdname hook_plot
2 #' @export
3 hook_plot_asciidoc = function(x, options) {
4 base = opts_knit$get('base.url') %n% ''
5 cap = .img.cap(options)
6
7 width = sprintf('width=%s', options$out.width)
8 height = sprintf('height=%s', options$out.height)
9 align = sprintf('align=%s', options$fig.align)
10 tags = paste(c(cap, width, height, align), collapse = ',')
11
12 sprintf('.%s\nimage::%s%s[%s]\n', cap, base, .upload.url(x), tags)
13 }
14
15 #' @rdname output_hooks
16 #' @export
17 render_asciidoc = function() {
18 set_html_dev()
19 opts_knit$set(out.format = 'asciidoc')
20 knit_hooks$set(hooks_asciidoc())
21 }
22
23 #' @rdname output_hooks
24 #' @export
25 hooks_asciidoc = function() {
26 hook.source = function(x, options) {
27 x = one_string(c(hilight_source(x, 'asciidoc', options), ''))
28 sprintf('\n[source,%s]\n----\n%s----\n', tolower(options$engine), x)
29 }
30 hook.message = function(x, options) {
31 sprintf('\n[NOTE]\n====\n.Message\n%s\n====\n', substring(x, comment_length(options$comment)))
32 }
33 hook.warning = function(x, options) {
34 sprintf('\n[WARNING]\n====\n.Warning\n%s\n====\n', gsub('^.*Warning: ', '', x))
35 }
36 hook.error = function(x, options) {
37 sprintf('\n[CAUTION]\n====\n.Error\n%s\n====\n', gsub('^.*Error: ', '', x))
38 }
39 hook.output = function(x, options) {
40 if (output_asis(x, options)) x else sprintf('\n----\n%s----\n', x)
41 }
42 list(
43 source = hook.source, output = hook.output, message = hook.message,
44 warning = hook.warning, error = hook.error, plot = hook_plot_asciidoc
45 )
46 }
47
48 comment_length = function(x) {
49 (if (is.null(x) || !nzchar(x) || is.na(x)) 0L else nchar(x)) + 1L
50 }