From ec8998275016d80d067f6980192d965d987dd1de Mon Sep 17 00:00:00 2001 From: jsta Date: Sun, 1 Jul 2018 10:23:24 -0400 Subject: [PATCH] only knit rmd files to md if they have changed --- Makefile | 5 ++--- bin/generate_md_episodes.R | 17 ++++++++++------- bin/knit_lessons.sh | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 5ee0980..ac587b8 100644 --- a/Makefile +++ b/Makefile @@ -88,9 +88,8 @@ HTML_DST = \ ## lesson-md : convert Rmarkdown files to markdown lesson-md : ${RMD_DST} -# Use of .NOTPARALLEL makes rule execute only once -${RMD_DST} : ${RMD_SRC} - @bin/knit_lessons.sh ${RMD_SRC} +_episodes/%.md: _episodes_rmd/%.Rmd + @bin/knit_lessons.sh $< $@ ## lesson-check : validate lesson Markdown. lesson-check : lesson-fixme diff --git a/bin/generate_md_episodes.R b/bin/generate_md_episodes.R index 6c27d9c..60d65e9 100644 --- a/bin/generate_md_episodes.R +++ b/bin/generate_md_episodes.R @@ -24,15 +24,18 @@ generate_md_episodes <- function() { install.packages(missing_pkgs) } - ## 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))) + ## get the Rmd file to process from the command line, and generate the path for their respective outputs + args <- commandArgs(trailingOnly = TRUE) + if (length(args) != 2){ + stop("input and output file must be passed to the script") + } + src_rmd <- args[1] + dest_md <- args[2] + ## knit the Rmd into markdown - mapply(function(x, y) { - knitr::knit(x, output = y) - }, src_rmd, dest_md) - + knitr::knit(src_rmd, output = dest_md) + # Read the generated md files and add comments advising not to edit them vapply(dest_md, function(y) { con <- file(y) diff --git a/bin/knit_lessons.sh b/bin/knit_lessons.sh index 3a2395f..6f9dae0 100755 --- a/bin/knit_lessons.sh +++ b/bin/knit_lessons.sh @@ -4,5 +4,5 @@ # The Makefile passes in the names of files. if [ $# -ne 0 ] ; then - Rscript -e "source('bin/generate_md_episodes.R')" + Rscript -e "source('bin/generate_md_episodes.R')" $* fi -- 2.30.2