Implement using @naupaka 's suggestions
[rnaseq-cwl-training.git] / bin / generate_md_episodes.R
index f2a40ba4a7285db9ee2b001ed1a56aabd973a9d8..6c27d9c9245fc5b6d1c5aaa82af2ea4073e59b80 100644 (file)
@@ -1,5 +1,7 @@
 generate_md_episodes <- function() {
 
+    library("methods")
+    
     if (require("knitr") && packageVersion("knitr") < '1.9.19')
         stop("knitr must be version 1.9.20 or higher")
 
@@ -25,12 +27,26 @@ generate_md_episodes <- function() {
     ## find all the Rmd files, and generate the paths for their respective outputs
     src_rmd <- list.files(pattern = "??-*.Rmd$", path = "_episodes_rmd", full.names = TRUE)
     dest_md <- file.path("_episodes", gsub("Rmd$", "md", basename(src_rmd)))
-
+    
     ## knit the Rmd into markdown
     mapply(function(x, y) {
         knitr::knit(x, output = y)
     }, src_rmd, dest_md)
-
+    
+    # Read the generated md files and add comments advising not to edit them
+    vapply(dest_md, function(y) {
+      con <- file(y)
+      mdfile <- readLines(con)
+      if (mdfile[1] != "---")
+        stop("Input file does not have a valid header")
+      mdfile <- append(mdfile, "# Please do not edit this file directly; it is auto generated.", after = 1)
+      mdfile <- append(mdfile, paste("# Instead, please edit", 
+                                     basename(y), "in _episodes_rmd/"), after = 2)
+      writeLines(mdfile, con)
+      close(con)
+      return(paste("Warning added to YAML header of", y))
+    },
+    character(1))
 }
 
 generate_md_episodes()