Skip to content

Commit

Permalink
tweak yihui#175 and add an example in the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Oct 23, 2018
1 parent d23bd1f commit f3a0090
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xaringan
Type: Package
Title: Presentation Ninja
Version: 0.7.2
Version: 0.7.3
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Claus Thorn", "Ekstrøm", role = "ctb"),
Expand Down
8 changes: 4 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## NEW FEATURES

- Added output highlighting using the chunk option `highlight.output`. (@malcolmbarrett, #175)
- Added output highlighting using the chunk option `highlight.output` (thanks, @malcolmbarrett, #175).

- Added a CSS theme `chocolate`. See [here](https://liao961120.github.io/slides/xaringan/) for an example slide. (@liao961120, #171)
- Added a CSS theme `chocolate`. See [here](https://liao961120.github.io/slides/xaringan/) for an example slide (thanks, @liao961120, #171).

- Added a set of CSS theme `kunoichi`, `shinobi` and `ninjutsu` - see [here for example](https://emitanaka.github.io/ninja-theme) (@emitanaka, #165)
- Added a set of CSS theme `kunoichi`, `shinobi` and `ninjutsu` - see [here for example](https://emitanaka.github.io/ninja-theme) (thanks, @emitanaka, #165).

- Added a CSS theme `lucy` (@LucyMcGowan, #163)
- Added a CSS theme `lucy` (thanks, @LucyMcGowan, #163).

# CHANGES IN xaringan VERSION 0.7

Expand Down
45 changes: 21 additions & 24 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,34 +134,31 @@ moon_reader = function(

optk = list()

hooks = local({
ohooks = knitr::knit_hooks$get(); on.exit(knitr::knit_hooks$restore(ohooks))
knitr::render_markdown()
knitr::knit_hooks$get(c('source', 'output'))
})

hook_highlight = if (isTRUE(nature$highlightLines)) {
# an ugly way to access the `source` hook of markdown output in knitr
function(x, options) {
hook_source = hooks[['source']]
res = hook_source(x, options)
highlight_code(res)
}
highlight_hooks = NULL
if (isTRUE(nature$highlightLines)) {
# an ugly way to access hooks of markdown output in knitr
hooks = local({
ohooks = knitr::knit_hooks$get(); on.exit(knitr::knit_hooks$restore(ohooks))
knitr::render_markdown()
knitr::knit_hooks$get(c('source', 'output'))
})
highlight_hooks = list(
source = function(x, options) {
hook = hooks[['source']]
res = hook(x, options)
highlight_code(res)
},
output = function(x, options) {
hook = hooks[['output']]
res = highlight_output(x, options)
hook(res, options)
}
)
}

hook_highlight_output = if (isTRUE(nature$highlightLines)) {
function(x, options) {
hook_output = hooks[['output']]
res = highlight_output(x, options)
hook_output(res, options)
}
}

rmarkdown::output_format(
if (!is.null(hook_highlight)) {
rmarkdown::knitr_options(knit_hooks = list(source = hook_highlight,
output = hook_highlight_output))
},
rmarkdown::knitr_options(knit_hooks = highlight_hooks),
NULL, clean_supporting = self_contained,
pre_knit = function(...) {
optk <<- knitr::opts_knit$get()
Expand Down
17 changes: 3 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,9 @@ highlight_code = function(x) {
}

highlight_output = function(x, options) {
highlight.output = options$highlight.output
if (is.null(highlight.output)) return(x)
if (is.logical(highlight.output) & !highlight.output) return(x)
x = strsplit(x, '\n')[[1]]

if (isTRUE(highlight.output)) {
highlight.lines = seq_along(x)
} else if (is.numeric(highlight.output)) {
highlight.lines = highlight.output
} else {
stop("`highlight.output` must be numeric or logical")
}

x[highlight.lines] = paste0('*', x[highlight.lines])
if (is.null(i <- options$highlight.output) || isFALSE(i)) return(x)
x = split_lines(x)
x[i] = paste0('*', x[i])
paste(x, collapse = '\n')
}

Expand Down
18 changes: 18 additions & 0 deletions inst/rmarkdown/templates/xaringan/skeleton/skeleton.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,24 @@ ggplot(mtcars) +

# Some Tips

When you enable line-highlighting, you can also use the chunk option `highlight.output` to highlight specific lines of the text output from a code chunk. For example, `highlight.output = TRUE` means highlighting all lines, and `highlight.output = c(1, 3)` means highlighting the first and third line.

````md
`r ''````{r, highlight.output=c(1, 3)}
head(iris)
```
````

```{r, highlight.output=c(1, 3), echo=FALSE}
head(iris)
```

Question: what does `highlight.output = c(TRUE, FALSE)` mean? (Hint: think about R's recycling of vectors)

---

# Some Tips

- To make slides work offline, you need to download a copy of remark.js in advance, because **xaringan** uses the online version by default (see the help page `?xaringan::moon_reader`).

- You can use `xaringan::summon_remark()` to download the latest or a specified version of remark.js. By default, it is downloaded to `libs/remark-latest.min.js`.
Expand Down

0 comments on commit f3a0090

Please sign in to comment.