diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 5a7377d..b4d1cdd 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -14,6 +14,8 @@ #' @param quiz_dir directory that contains the quiz .md files that should be #' checked and incorporated into the Book.txt file. If you don't have quizzes, #' set this to NULL +#' @param clean_up TRUE/FALSE the old output directory should be deleted and +#' everything created fresh. #' @param footer_text Optionally can add a bit of text that will be added to the #' end of each file before the references section. #' @@ -28,8 +30,20 @@ bookdown_to_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL) { - set_up_leanpub() + footer_text = NULL, + clean_up = FALSE) { + # Run the set up + set_up_leanpub(path = path, + embed = FALSE, + clean_up = clean_up, + render = render, + output_dir = output_dir, + make_book_txt = make_book_txt, + quiz_dir = quiz_dir, + run_quiz_checks = run_quiz_checks, + remove_resources_start = remove_resources_start, + verbose = verbose, + footer_text = footer_text) # Establish path path <- bookdown_path(path) @@ -40,6 +54,7 @@ bookdown_to_leanpub <- function(path = ".", rmd_files <- bookdown_rmd_files(path = path) bib_files <- list.files(pattern = "[.]bib$") + if (length(bib_files) > 0) { pandoc_args <- paste0("--bibliography=", path.expand(normalizePath(bib_files))) } else { @@ -78,7 +93,7 @@ bookdown_to_leanpub <- function(path = ".", message("Running bookdown_to_book_txt") } bookdown_to_book_txt( - path = path, + md_files = rmd_files, output_dir = output_dir, quiz_dir = quiz_dir, verbose = verbose @@ -134,6 +149,8 @@ bookdown_to_leanpub <- function(path = ".", #' @param quiz_dir directory that contains the quiz .md files that should be #' checked and incorporated into the Book.txt file. If you don't have quizzes, #' set this to NULL +#' @param clean_up TRUE/FALSE the old output directory should be deleted and +#' everything created fresh. #' @param footer_text Optionally can add a bit of text that will be added to the #' end of each file before the references section. #' @@ -147,10 +164,12 @@ bookdown_to_leanpub <- function(path = ".", #' ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapter_urls.tsv") #' #' } + bookdown_to_embed_leanpub <- function(path = ".", chapt_img_key = NULL, bookdown_index = file.path("docs", "index.html"), base_url = NULL, + clean_up = FALSE, default_img = NULL, render = TRUE, output_dir = "manuscript", @@ -160,13 +179,29 @@ bookdown_to_embed_leanpub <- function(path = ".", remove_resources_start = FALSE, verbose = TRUE, footer_text = NULL) { - set_up_leanpub() - + # Run the set up + set_up_leanpub(path = path, + embed = TRUE, + clean_up = clean_up, + render = render, + output_dir = output_dir, + make_book_txt = make_book_txt, + quiz_dir = quiz_dir, + run_quiz_checks = run_quiz_checks, + remove_resources_start = remove_resources_start, + verbose = verbose, + footer_text = footer_text) + + # If TSV chapter image key file is specified read it in if (!is.null(chapt_img_key)) { + message(paste("Reading in a chapt_img_key TSV file:", chapt_img_key)) chapt_df <- readr::read_tsv(chapt_img_key) + } else { + # If its not supplied, create it from the get_chapters function message("Creating a chapt_img_key TSV file") + if (is.null(base_url)) { stop("No base_url is supplied and no chapt_img_key file was supplied. Need one or the other.") } @@ -176,6 +211,8 @@ bookdown_to_embed_leanpub <- function(path = ".", # If there's no img_path supplied, then use a default image for each. if (!("img_path" %in% colnames(chapt_df))) { + + # If no default image is supplied if (is.null(default_img)) { default_img <- "https://docs.google.com/presentation/d/1jEUxUY1qXDZ3DUtvTU6NCc6ASG5nx4Gwczv5aAglYX4/edit#slide=id.p" } @@ -199,11 +236,11 @@ bookdown_to_embed_leanpub <- function(path = ".", ####################### Book.txt creation #################################### out <- NULL if (make_book_txt) { - if (verbose > 1) { - message("Running bookdown_to_book_txt") - } + if (verbose) message("Running bookdown_to_book_txt") + md_files <- basename(unlist(md_output_files)) + bookdown_to_book_txt( - path = output_dir, + md_files = , output_dir = output_dir, quiz_dir = quiz_dir, verbose = verbose @@ -239,6 +276,7 @@ bookdown_to_embed_leanpub <- function(path = ".", #' Create Book.txt file from files existing in quiz directory #' #' @param path path to the bookdown book, must have a `_bookdown.yml` file +#' @param md_files vector of file path of the md's to be included #' @param output_dir output directory to put files. It should likely be #' relative to path #' @param quiz_dir Where are the quizzes stored? Default looks for folder called "quizzes". @@ -248,32 +286,37 @@ bookdown_to_embed_leanpub <- function(path = ".", #' @export #' bookdown_to_book_txt <- function(path = ".", + md_files = NULL, output_dir = "manuscript", quiz_dir = "quizzes", verbose = TRUE) { - # Establish path - path <- bookdown_path(path) + + # If md_files are not speciied, then try to get them + if (is.null(md_files)) { + # Establish path + path <- bookdown_path(path) - rmd_regex <- "[.][R|r]md$" + rmd_regex <- "[.][R|r]md$" - # Extract the names of the Rmd files (the chapters) - rmd_files <- bookdown_rmd_files(path = path) + # Extract the names of the Rmd files (the chapters) + md_files <- bookdown_rmd_files(path = path) + } if (!is.null(quiz_dir)) { # Find the quiz files in the quiz directory quiz_files <- list.files(pattern = "\\.md$", quiz_dir) # Put files in one vector - all_files <- c(rmd_files, quiz_files) + all_files <- c(md_files, quiz_files) # Make a vector specifying the file type: quiz or not file_type <- c( - rep("non-quiz", length(rmd_files)), + rep("non-quiz", length(md_files)), rep("quiz", length(quiz_files)) ) } else { - all_files <- rmd_files - file_type <- rep("non-quiz", length(rmd_files)) + all_files <- md_files + file_type <- rep("non-quiz", length(md_files)) } # Put all files in one data.frame all_files <- data.frame( diff --git a/R/set_up.R b/R/set_up.R index d288e1a..1a1c20a 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -181,7 +181,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' set this to NULL #' @param footer_text Optionally can add a bit of text that will be added to the #' end of each file before the references section. -#' +#' @param embed is this being run by bookdown_to_embed_leanpub? TRUE/FALSE #' @return A list of output files and diagnostics #' @export #' @@ -194,12 +194,18 @@ set_up_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL) { + footer_text = NULL, + embed = NULL) { - if (clean_up && dir.exists(output_dir)) { + if (clean_up) { message(paste("Clearing out old version of output files:", output_dir)) - file.remove(output_dir, recursive = TRUE) - dir.create(output_dir, showWarnings = FALSE) + + file.remove(output_dir, recursive = TRUE, showWarnings = FALSE) + } + + # If output directory doesn't exist, make it + if (!dir.exists(output_dir)) { + dir.create(output_dir, recursive = TRUE, showWarnings = FALSE) } # Declare regex for finding rmd files @@ -244,41 +250,32 @@ set_up_leanpub <- function(path = ".", clean_envir = FALSE ) } - - # may be irrelevant since copy_docs does everything - if (verbose) { - message("Copying Resources") - } - copy_resources(path, output_dir = output_dir) - - if (verbose) { - message("Copying Docs folder") - } - - copy_docs(path, output_dir = output_dir) - if (verbose) { - message("Copying docs files") - } - - copy_bib(path, output_dir = output_dir) - if (verbose) { - message("Copying bib files") + + # We only need to copy these things if we are not doing embed + if (!embed) { + if (verbose) message("Copying Resources") + copy_resources(path, output_dir = output_dir) + + if (verbose) message("Copying docs files") + copy_docs(path, output_dir = output_dir) + + if (verbose) message("Copying bib files") + copy_bib(path, output_dir = output_dir) } if (!is.null(quiz_dir)) { #### Run quiz checks if (run_quiz_checks) { message("Checking quizzes") - quiz_checks <- check_quizzes(quiz_dir, + quiz_checks <- check_quizzes( + quiz_dir, verbose = verbose ) } + if (verbose) message("Copying quiz files") copy_quizzes( quiz_dir = quiz_dir, output_dir = output_dir ) - if (verbose) { - message("Copying quiz files") - } } } diff --git a/man/bookdown_to_book_txt.Rd b/man/bookdown_to_book_txt.Rd index 2370e6e..a0b46cd 100644 --- a/man/bookdown_to_book_txt.Rd +++ b/man/bookdown_to_book_txt.Rd @@ -6,6 +6,7 @@ \usage{ bookdown_to_book_txt( path = ".", + md_files = NULL, output_dir = "manuscript", quiz_dir = "quizzes", verbose = TRUE @@ -14,6 +15,8 @@ bookdown_to_book_txt( \arguments{ \item{path}{path to the bookdown book, must have a `_bookdown.yml` file} +\item{md_files}{vector of file path of the md's to be included} + \item{output_dir}{output directory to put files. It should likely be relative to path} diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/bookdown_to_embed_leanpub.Rd index d355c26..19dd041 100644 --- a/man/bookdown_to_embed_leanpub.Rd +++ b/man/bookdown_to_embed_leanpub.Rd @@ -9,6 +9,7 @@ bookdown_to_embed_leanpub( chapt_img_key = NULL, bookdown_index = file.path("docs", "index.html"), base_url = NULL, + clean_up = FALSE, default_img = NULL, render = TRUE, output_dir = "manuscript", @@ -34,6 +35,9 @@ If no image column supplied, default image used.} \item{base_url}{The base url of where the chapters are published -- the url to provide to the iframe in Leanpub e.g. https://jhudatascience.org/OTTR_Template/coursera} +\item{clean_up}{TRUE/FALSE the old output directory should be deleted and +everything created fresh.} + \item{default_img}{A google slide link to the default image to be used for all chapters} \item{render}{if `TRUE`, then [bookdown::render_book()] will be run on each Rmd.} diff --git a/man/bookdown_to_leanpub.Rd b/man/bookdown_to_leanpub.Rd index 552cf8c..8087447 100644 --- a/man/bookdown_to_leanpub.Rd +++ b/man/bookdown_to_leanpub.Rd @@ -13,7 +13,8 @@ bookdown_to_leanpub( run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL + footer_text = NULL, + clean_up = FALSE ) } \arguments{ @@ -40,6 +41,9 @@ of any image path.} \item{footer_text}{Optionally can add a bit of text that will be added to the end of each file before the references section.} + +\item{clean_up}{TRUE/FALSE the old output directory should be deleted and +everything created fresh.} } \value{ A list of output files and diagnostics diff --git a/man/get_chapters.Rd b/man/get_chapters.Rd index 9e57d5c..378d40f 100644 --- a/man/get_chapters.Rd +++ b/man/get_chapters.Rd @@ -13,7 +13,7 @@ get_chapters(bookdown_index = file.path("docs", "index.html"), base_url = NULL) e.g. https://jhudatascience.org/OTTR_Template/coursera} } \value{ -A data.frame of the chapter urls and their titles that are to be ported to Leanpub. +A data.frame of the chapter urls and their titles that are to be ported to Leanpub. This can be passed to } \description{ diff --git a/man/set_up_leanpub.Rd b/man/set_up_leanpub.Rd index d64d819..0d5a842 100644 --- a/man/set_up_leanpub.Rd +++ b/man/set_up_leanpub.Rd @@ -14,7 +14,8 @@ set_up_leanpub( run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL + footer_text = NULL, + embed = NULL ) } \arguments{ @@ -44,6 +45,8 @@ of any image path.} \item{footer_text}{Optionally can add a bit of text that will be added to the end of each file before the references section.} + +\item{embed}{is this being run by bookdown_to_embed_leanpub? TRUE/FALSE} } \value{ A list of output files and diagnostics