Merge branch 'gh-pages' of github.com:carpentries/styles into gh-pages
[rnaseq-cwl-training.git] / bin / generate_md_episodes.R
1 generate_md_episodes <- function() {
2
3   ## get the Rmd file to process from the command line, and generate the path
4   ## for their respective outputs
5   args  <- commandArgs(trailingOnly = TRUE)
6   if (!identical(length(args), 2L)) {
7     stop("input and output file must be passed to the script")
8   }
9
10   src_rmd <- args[1]
11   dest_md <- args[2]
12
13   ## knit the Rmd into markdown
14   knitr::knit(src_rmd, output = dest_md)
15
16   # Read the generated md files and add comments advising not to edit them
17   add_no_edit_comment <- function(y) {
18     con <- file(y)
19     mdfile <- readLines(con)
20     if (mdfile[1] != "---")
21       stop("Input file does not have a valid header")
22     mdfile <- append(
23       mdfile,
24       "# Please do not edit this file directly; it is auto generated.",
25       after = 1
26     )
27     mdfile <- append(
28       mdfile,
29       paste("# Instead, please edit", basename(y), "in _episodes_rmd/"),
30       after = 2
31     )
32     writeLines(mdfile, con)
33     close(con)
34     return(paste("Warning added to YAML header of", y))
35   }
36
37   vapply(dest_md, add_no_edit_comment, character(1))
38 }
39
40 generate_md_episodes()