Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

paste source lines together before wrapping them in <pre>

  • Loading branch information...
commit 8c9881ece306659c61d562295d47d71bbdb5945e 1 parent 24898c0
@yihui authored
Showing with 26 additions and 4 deletions.
  1. +2 −0  NEWS.md
  2. +5 −4 R/hooks-md.R
  3. +19 −0 tests/testit/test-hooks.R
View
2  NEWS.md
@@ -24,6 +24,8 @@
- fixed #604: `kable()` did not work on data frames/matrices of one row (thanks, Kevin Ushey)
+- fixes the bug reported at http://stackoverflow.com/q/18992260/559676; `render_jekyll('prettify')` should have pasted the source code lines into one character string
+
## MAJOR CHANGES
- when the chunk option `cache=2`, the recorded plots (i.e. display lists) will no longer be cached, and the figure files will be assumed to exist the next time the document is compiled, otherwise the cache will be purged and the chunk needs to be recomputed; this will save some disk space and avoid bugs like #588 (thanks, @knokknok)
View
9 R/hooks-md.R
@@ -69,9 +69,8 @@ render_jekyll = function(highlight = c('pygments', 'prettify', 'none'), extra =
if (hi == 'none') return()
switch(hi, pygments = {
hook.r = function(x, options) {
- x = paste(c(hilight_source(x, 'markdown', options), ''), collapse = '\n')
str_c('\n\n{% highlight ', tolower(options$engine), if (extra != '') ' ', extra, ' %}\n',
- x, '{% endhighlight %}\n\n')
+ x, '\n{% endhighlight %}\n\n')
}
hook.t = function(x, options) str_c('\n\n{% highlight text %}\n', x, '{% endhighlight %}\n\n')
}, prettify = {
@@ -82,6 +81,8 @@ render_jekyll = function(highlight = c('pygments', 'prettify', 'none'), extra =
hook.t = function(x, options) str_c('\n\n<pre><code>', escape_html(x), '</code></pre>\n\n')
})
hook.o = function(x, options) if (output_asis(x, options)) x else hook.t(x, options)
- knit_hooks$set(source = hook.r, output = hook.o, warning = hook.t,
- error = hook.t, message = hook.t)
+ knit_hooks$set(source = function(x, options) {
+ x = paste(hilight_source(x, 'markdown', options), collapse = '\n')
+ hook.r(x, options)
+ }, output = hook.o, warning = hook.t, error = hook.t, message = hook.t)
}
View
19 tests/testit/test-hooks.R
@@ -0,0 +1,19 @@
+library(testit)
+
+render_jekyll('prettify')
+options(prompt = '> ', continue = '+ ')
+
+# see http://stackoverflow.com/q/18992260/559676 for the bug
+assert(
+ 'the source hook pastes source code into one character string',
+ identical(
+ knit_hooks$get('source')(c('1+1', '2-2'), opts_chunk$get()),
+ "\n\n<pre><code class=\"prettyprint \">1+1\n2-2</code></pre>\n\n"
+ ),
+ identical(
+ knit_hooks$get('source')(c('1+1+\n1', '2-2'), opts_chunk$merge(list(prompt=TRUE))),
+ "\n\n<pre><code class=\"prettyprint \">&gt; 1+1+\n+ 1\n&gt; 2-2</code></pre>\n\n"
+ )
+)
+
+knit_hooks$restore()
Please sign in to comment.
Something went wrong with that request. Please try again.