forked from datacarpentry/python-ecology-lesson
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request datacarpentry#56 from fmichonneau/rmarkdown-tools
add tools to deal with episodes written in Rmd
- Loading branch information
Showing
4 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# These settings control the behavior of all chunks in the novice R materials. | ||
# For example, to generate the lessons with all the output hidden, simply change | ||
# `results` from "markup" to "hide". | ||
# For more information on available chunk options, see | ||
# http://yihui.name/knitr/options#chunk_options | ||
|
||
library("knitr") | ||
|
||
fix_fig_path <- function(pth) file.path("..", pth) | ||
|
||
## We use the swc-rmd- prefix for the figures generated by the lssons | ||
## so they can be easily identified and deleted by `make clean`. The | ||
## working directory when the lessons are generated is the root so the | ||
## figures need to be saved in fig/, but when the site is generated, | ||
## the episodes will be one level down. We fix the path using the | ||
## `fig.process` option. | ||
opts_chunk$set(tidy = FALSE, results = "markup", comment = NA, | ||
fig.align = "center", fig.path = "fig/swc-rmd-", | ||
fig.process = fix_fig_path) | ||
|
||
# The hooks below add html tags to the code chunks and their output so that they | ||
# are properly formatted when the site is built. | ||
hook_in <- function(x, options) { | ||
stringr::str_c("\n\n~~~{.r}\n", | ||
paste0(x, collapse="\n"), | ||
"\n~~~\n{: .source}\n\n") | ||
} | ||
|
||
hook_out <- function(x, options) { | ||
stringr::str_c("\n\n~~~\n", | ||
paste0(x, collapse="\n"), | ||
"\n~~~\n{: .output}\n\n") | ||
} | ||
|
||
hook_error <- function(x, options) { | ||
stringr::str_c("\n\n~~~\n", | ||
paste0(x, collapse="\n"), | ||
"\n~~~\n{: .error}\n\n") | ||
} | ||
|
||
knit_hooks$set(source = hook_in, output = hook_out, warning = hook_error, | ||
error = hook_error, message = hook_out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
if (require("knitr")) { | ||
if (packageVersion("knitr") < '1.9.19') { | ||
stop("knitr must be version 1.9.20 or higher") | ||
} | ||
} else stop("knitr 1.9.20 or above is needed to build the lessons.") | ||
|
||
if (!require("stringr")) | ||
stop("The package stringr is required for generating the lessons.") | ||
|
||
src_rmd <- list.files(pattern = "??-*.Rmd$", path = "_episodes_rmd", full.names = TRUE) | ||
dest_md <- file.path("_episodes", gsub("Rmd$", "md", basename(src_rmd))) | ||
|
||
for (i in seq_along(src_rmd)) { | ||
knitr::knit(src_rmd[i], output = dest_md[i]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [ -d "_episodes_rmd" ] ; then | ||
Rscript -e "source('bin/generate_md_episodes.R')" | ||
fi |