| 1 |
# SET OF HOOKS FOR RESTRUCTURED TEXT --- |
| 2 |
|
| 3 |
#' @rdname hook_plot |
| 4 |
#' @export |
| 5 |
hook_plot_rst = function(x, options) { |
| 6 |
if (options$fig.show == 'animate') return(hook_plot_html(x, options)) |
| 7 |
|
| 8 |
cap = .img.cap(options) |
| 9 |
alt = .img.cap(options, alt = TRUE) |
| 10 |
# TODO: add all options for figure |
| 11 |
# See http://docutils.sourceforge.net/docs/ref/rst/directives.html#image |
| 12 |
# http://docutils.sourceforge.net/docs/ref/rst/directives.html#figure |
| 13 |
make_directive( |
| 14 |
'figure', |
| 15 |
paste0(opts_knit$get('base.url'), .upload.url(x)), |
| 16 |
c(align = if (options$fig.align == 'default') NULL else options$fig.align, |
| 17 |
alt = alt, width = options$out.width, height = options$out.height), |
| 18 |
cap |
| 19 |
) |
| 20 |
} |
| 21 |
|
| 22 |
#' @rdname output_hooks |
| 23 |
#' @export |
| 24 |
render_rst = function(strict = FALSE) { |
| 25 |
set_html_dev() |
| 26 |
knit_hooks$set(hooks_rst(strict)) |
| 27 |
} |
| 28 |
|
| 29 |
#' @rdname output_hooks |
| 30 |
#' @export |
| 31 |
hooks_rst = function(strict = FALSE) { |
| 32 |
hook.s = function(x, options) { |
| 33 |
one_string(c('\n\n::\n', indent_block(x), '')) |
| 34 |
} |
| 35 |
hook.t = function(x, options) { |
| 36 |
make_directive('sourcecode', tolower(options$engine), '', content = x) |
| 37 |
} |
| 38 |
hook.i = function(x) .inline.hook(format_sci(x, 'rst')) |
| 39 |
list( |
| 40 |
source = function(x, options) { |
| 41 |
x = one_string(c(hilight_source(x, 'rst', options), '')) |
| 42 |
(if (strict) hook.s else hook.t)(x, options) |
| 43 |
}, |
| 44 |
warning = hook.s, error = hook.s, message = hook.s, |
| 45 |
inline = hook.i, plot = hook_plot_rst, output = function(x, options) { |
| 46 |
if (output_asis(x, options)) x else hook.s(x, options) |
| 47 |
} |
| 48 |
) |
| 49 |
} |
| 50 |
|
| 51 |
# Insert a reStructuredText directive for sphinx |
| 52 |
# |
| 53 |
# A reSt directive consists of a name, arguments, option and some content. |
| 54 |
# A typical reSt directive looks like this: |
| 55 |
# .. <name>:: <arguments> |
| 56 |
# :<option>: <option values> |
| 57 |
# |
| 58 |
# content |
| 59 |
# |
| 60 |
# This function accepts these arguments and returns the correctly formatted |
| 61 |
# reStructuredText directive |
| 62 |
# Input |
| 63 |
# make_directive('figure', 'fig.png', c(align = 'center', alt = 'cap')) |
| 64 |
# Output |
| 65 |
# .. figure:: fig.png |
| 66 |
# :align: center |
| 67 |
# :alt: cap |
| 68 |
make_directive = function(name, arg, opt, content = '') { |
| 69 |
l1 = sprintf('\n.. %s:: %s\n', name, arg) |
| 70 |
l2 = one_string(sprintf(':%s: %s', names(opt), opt)) |
| 71 |
paste0(l1, indent_block(l2), '\n\n', indent_block(content)) |
| 72 |
} |