| 1 |
library(testit) |
| 2 |
|
| 3 |
render_jekyll('prettify') |
| 4 |
options(prompt = '> ', continue = '+ ') |
| 5 |
|
| 6 |
# see http://stackoverflow.com/q/18992260/559676 for the bug |
| 7 |
assert('the source hook pastes source code into one character string', { |
| 8 |
(knit_hooks$get('source')(c('1+1', '2-2'), opts_chunk$get()) %==% |
| 9 |
"\n\n<pre><code class=\"prettyprint \">1+1\n2-2</code></pre>\n\n") |
| 10 |
(knit_hooks$get('source')(c('1+1+\n1', '2-2'), opts_chunk$merge(list(prompt = TRUE))) %==% |
| 11 |
"\n\n<pre><code class=\"prettyprint \">> 1+1+\n+ 1\n> 2-2</code></pre>\n\n") |
| 12 |
}) |
| 13 |
|
| 14 |
render_markdown() |
| 15 |
|
| 16 |
img_output = function(path, opts = list()) { |
| 17 |
opts = opts_chunk$merge(opts) |
| 18 |
sew(knit_print(include_graphics(path, error = FALSE)), opts) |
| 19 |
} |
| 20 |
|
| 21 |
assert('include_graphics() includes custom images correctly', { |
| 22 |
(img_output('a.png') %==% '') |
| 23 |
(img_output(c('a.png', 'b.png'), list(fig.show = 'hold')) %==% '') |
| 24 |
(img_output('a.png', list(fig.cap = 'foo bar')) %==% '') |
| 25 |
(img_output('a.png', list(out.width = '50%')) %==% '<img src="a.png" alt="" width="50%" />') |
| 26 |
(img_output('a.pdf', list(out.width = '300px')) %==% '<embed src="a.pdf" width="300px" type="application/pdf" />') |
| 27 |
}) |
| 28 |
|
| 29 |
hook_src = knit_hooks$get("source") |
| 30 |
options_ = list(engine = "r", prompt = FALSE, highlight = TRUE) |
| 31 |
|
| 32 |
assert('Length of fences are satisfied', { |
| 33 |
(hook_src("", options_) %==% "\n\n``` r\n\n```\n\n") |
| 34 |
(hook_src("```", options_) %==% "\n\n```` r\n```\n````\n\n") |
| 35 |
}) |
| 36 |
|
| 37 |
assert('Attributes for source can be specified class.source and attr.source', { |
| 38 |
(hook_src("1", c(options_, class.source = "a b")) %==% "\n\n```{.r .a .b}\n1\n```\n\n") |
| 39 |
(hook_src("1", c(options_, attr.source = ".a .b")) %==% "\n\n```{.r .a .b}\n1\n```\n\n") |
| 40 |
(hook_src("1", c(options_, class.source = "a", attr.source = "b='1'")) %==% |
| 41 |
"\n\n```{.r .a b='1'}\n1\n```\n\n") |
| 42 |
(hook_src("1", c(options_, attr.source = ".a b='1'")) %==% |
| 43 |
"\n\n```{.r .a b='1'}\n1\n```\n\n") |
| 44 |
}) |
| 45 |
|
| 46 |
assert('class.source and attr.source works also with collapse = TRUE', { |
| 47 |
hook_chunk = hooks_markdown()$chunk |
| 48 |
(hook_chunk("```{.r .a b=1}\n1\n```", c(options_, collapse = TRUE)) %==% |
| 49 |
"```{.r .a b=1}\n1\n```") |
| 50 |
(hook_chunk("```{.r .a b=1}\n1\n```\n```{.r .a b=1}\n1\n```", |
| 51 |
c(options_, collapse = TRUE)) %==% |
| 52 |
"```{.r .a b=1}\n1\n1\n```") |
| 53 |
}) |
| 54 |
|
| 55 |
hook_out = knit_hooks$get("output") |
| 56 |
|
| 57 |
assert('Attributes for source can be specified class.source and attr.source', { |
| 58 |
(hook_out("1\n", c(options_, class.output = "a b")) %==% |
| 59 |
"\n\n```{.a .b}\n1\n```\n\n") |
| 60 |
(hook_out("1\n", c(options_, attr.output = ".a .b")) %==% |
| 61 |
"\n\n```{.a .b}\n1\n```\n\n") |
| 62 |
(hook_out("1\n", c(options_, class.output = "a", attr.output = "b='1'")) %==% |
| 63 |
"\n\n```{.a b='1'}\n1\n```\n\n") |
| 64 |
(hook_out("1\n", c(options_, attr.output = ".a b='1'")) %==% |
| 65 |
"\n\n```{.a b='1'}\n1\n```\n\n") |
| 66 |
}) |
| 67 |
|
| 68 |
hook_chunk = knit_hooks$get("chunk") |
| 69 |
|
| 70 |
assert('Chunks are enclosed by fenced divs when needed.', { |
| 71 |
(hook_chunk('', list(class.chunk=NULL)) %==% '') |
| 72 |
(hook_chunk('', list(class.chunk="")) %==% '::: \n\n:::') |
| 73 |
(hook_chunk('', list(class.chunk="foo")) %==% '::: foo\n\n:::') |
| 74 |
(hook_chunk(':::', list(class.chunk="foo")) %==% ':::: foo\n:::\n::::') |
| 75 |
}) |
| 76 |
|
| 77 |
knit_hooks$restore() |
| 78 |
|
| 79 |
|
| 80 |
x = "1.png" |
| 81 |
w = h = 1 |
| 82 |
ex = "style='margin: 0;'" |
| 83 |
cap = "foo" |
| 84 |
opt = function( |
| 85 |
w = NULL, h = NULL, ex = NULL, cap = NULL, show = 'asis', |
| 86 |
fig.align = 'default', ... |
| 87 |
) { |
| 88 |
list( |
| 89 |
out.width = w, out.height = h, out.extra = ex, |
| 90 |
fig.cap = cap, fig.show = show, fig.align = fig.align, ... |
| 91 |
) |
| 92 |
} |
| 93 |
|
| 94 |
assert("Include a plot by pandoc md", { |
| 95 |
(hook_plot_md_pandoc(x, opt()) %==% "") |
| 96 |
(hook_plot_md_pandoc(x, opt(w = w)) %==% sprintf("{width=%s}", w)) |
| 97 |
(hook_plot_md_pandoc(x, opt(h = h)) %==% sprintf("{height=%s}", h)) |
| 98 |
(hook_plot_md_pandoc(x, opt(cap = cap)) %==% sprintf("", cap)) |
| 99 |
(hook_plot_md_pandoc(x, opt(ex = ex)) %==% sprintf("{%s}", ex)) |
| 100 |
(hook_plot_md_pandoc(x, opt(w = w, cap = cap, ex = ex)) %==% |
| 101 |
sprintf("{width=%s %s}", cap, w, ex)) |
| 102 |
}) |
| 103 |
|
| 104 |
assert('empty alt text is preserved and NA alt is discarded', { |
| 105 |
(hook_plot_md(x, opts_chunk$merge(list(fig.alt = ''))) %==% '<img src="1.png" alt="" />') |
| 106 |
(hook_plot_md(x, opts_chunk$merge(list(fig.alt = NA, out.width = '100'))) %==% '<img src="1.png" width="100" />') |
| 107 |
}) |
| 108 |
|
| 109 |
assert("fig.alt does not break office document", { |
| 110 |
old = opts_knit$get('rmarkdown.pandoc.to') |
| 111 |
opts_knit$set(rmarkdown.pandoc.to = "docx") |
| 112 |
(suppressWarnings(hook_plot_md(x, opt())) %==% "") |
| 113 |
(suppressWarnings(hook_plot_md(x, opt(fig.alt = "bar"))) %==% "") |
| 114 |
opts_knit$set('rmarkdown.pandoc.to' = old) |
| 115 |
}) |