Skip to content

Commit 27ead33

Browse files
authored
Merge pull request carpentries#200 from mawds/commentgenerated
Advise users not to edit .md files generated from .Rmd files
2 parents 0e1523e + 682e775 commit 27ead33

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

bin/generate_md_episodes.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@ generate_md_episodes <- function() {
2727
## find all the Rmd files, and generate the paths for their respective outputs
2828
src_rmd <- list.files(pattern = "??-*.Rmd$", path = "_episodes_rmd", full.names = TRUE)
2929
dest_md <- file.path("_episodes", gsub("Rmd$", "md", basename(src_rmd)))
30-
30+
3131
## knit the Rmd into markdown
3232
mapply(function(x, y) {
3333
knitr::knit(x, output = y)
3434
}, src_rmd, dest_md)
35-
35+
36+
# Read the generated md files and add comments advising not to edit them
37+
vapply(dest_md, function(y) {
38+
con <- file(y)
39+
mdfile <- readLines(con)
40+
if (mdfile[1] != "---")
41+
stop("Input file does not have a valid header")
42+
mdfile <- append(mdfile, "# Please do not edit this file directly; it is auto generated.", after = 1)
43+
mdfile <- append(mdfile, paste("# Instead, please edit",
44+
basename(y), "in _episodes_rmd/"), after = 2)
45+
writeLines(mdfile, con)
46+
close(con)
47+
return(paste("Warning added to YAML header of", y))
48+
},
49+
character(1))
3650
}
3751

3852
generate_md_episodes()

0 commit comments

Comments
 (0)