Insert comment in YAML header advising not to edit
authorDavid Mawdsley <david.mawdsley@manchester.ac.uk>
Thu, 25 Jan 2018 09:38:50 +0000 (09:38 +0000)
committerDavid Mawdsley <david.mawdsley@manchester.ac.uk>
Thu, 25 Jan 2018 10:49:56 +0000 (10:49 +0000)
bin/generate_md_episodes.R

index 4ea9835037a81c7e9c657681b12c46cfa30b235f..bcc199dfdc9afb431903e7e891ea95675a550a5c 100644 (file)
@@ -27,11 +27,23 @@ 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
+    sapply(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)
+    })    
 
 }