Back to home page

EIC code displayed by LXR

 
 

    


Warning, /tutorial-jana2/bin/generate_md_episodes.R is written in an unsupported language. File is not indexed.

0001 generate_md_episodes <- function() {
0002 
0003   # avoid ansi color characters from being printed in the output
0004   op <- options()
0005   on.exit(options(op), add = TRUE)
0006   options(crayon.enabled = FALSE)
0007   ## get the Rmd file to process from the command line, and generate the path
0008   ## for their respective outputs
0009   args  <- commandArgs(trailingOnly = TRUE)
0010   if (!identical(length(args), 2L)) {
0011     stop("input and output file must be passed to the script")
0012   }
0013 
0014   src_rmd <- args[1]
0015   dest_md <- args[2]
0016 
0017   ## knit the Rmd into markdown
0018   knitr::knit(src_rmd, output = dest_md)
0019 
0020   # Read the generated md files and add comments advising not to edit them
0021   add_no_edit_comment <- function(y) {
0022     con <- file(y)
0023     mdfile <- readLines(con)
0024     if (mdfile[1] != "---")
0025       stop("Input file does not have a valid header")
0026     mdfile <- append(
0027       mdfile,
0028       "# Please do not edit this file directly; it is auto generated.",
0029       after = 1
0030     )
0031     mdfile <- append(
0032       mdfile,
0033       paste("# Instead, please edit", basename(y), "in _episodes_rmd/"),
0034       after = 2
0035     )
0036     writeLines(mdfile, con)
0037     close(con)
0038     return(paste("Warning added to YAML header of", y))
0039   }
0040 
0041   vapply(dest_md, add_no_edit_comment, character(1))
0042 }
0043 
0044 generate_md_episodes()