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