From 3ac4471fe4c0474e04dd6adf74170cb534a4917f Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 10:58:33 -0500 Subject: [PATCH 01/11] Add embed option --- R/bookdown_to_leanpub.R | 460 ++++++++++++++++++---------------------- R/set_up.R | 282 ++++++++++++++++++++++++ 2 files changed, 492 insertions(+), 250 deletions(-) create mode 100644 R/set_up.R diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 7a657a3..9a6dee1 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -1,170 +1,4 @@ -#' Load in Bookdown specifications from _bookdown.yml -#' -#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory -#' -#' @return The yaml contents using yaml::yaml.load_file() -#' @export - -get_bookdown_spec <- function(path = ".") { - - # Get the file path to _bookdown.yaml - file_path <- bookdown_file(path = path) - - # Read in yaml - suppressWarnings({ - yaml_contents <- yaml::yaml.load_file(file_path) - }) - - return(yaml_contents) -} - -#' Find main Bookdown directory -#' -#' @param path Where to look for the file. By default looks in current directory. -#' -#' @return Returns the directory where the _bookdown.yml is contained. -#' @export - -bookdown_path <- function(path = ".") { - - # See what unzip is being used - operating_system <- Sys.info()[1] - - path <- rprojroot::find_root(rprojroot::has_file("_bookdown.yml"), path = file.path(path)) - - return(path) -} - -#' Find file path to _bookdown.yml -#' -#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory -#' -#' @return The file path to _bookdown.yml -#' @export -#' -bookdown_file <- function(path = ".") { - root_dir <- bookdown_path(path = file.path(path)) - file_path <- file.path(root_dir, "_bookdown.yml") - - return(file_path) -} - -#' Get file paths all Rmds in the bookdown directory -#' -#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory -#' -#' @return The file paths to Rmds listed in the _bookdown.yml file. -#' @export -#' -bookdown_rmd_files <- function(path = ".") { - spec <- get_bookdown_spec(file.path(path)) - - files <- spec$rmd_files - if (is.null(files) || all(is.na(files)) || length(files) == 0) { - warning( - "No bookdown specification of the files, using ", - "list.files(pattern ='.Rmd')" - ) - root_dir <- bookdown_path(path = file.path(path)) - files <- list.files( - pattern = "[.]Rmd", ignore.case = TRUE, - path = root_dir, full.names = FALSE - ) - } - return(files) -} - -#' Declare file path to docs/ folder -#' -#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory -#' -#' @return The file paths to Rmds listed in the _bookdown.yml file. -#' @export -#' -bookdown_destination <- function(path = ".") { - - # Find _bookdown.yml - root_dir <- bookdown_path(path = file.path(path)) - - # Get specs from _bookdown.yml - spec <- get_bookdown_spec(path = file.path(path)) - - # Find output directory declared in the bookdown.yml - output_dir <- spec$output_dir - - # If none specified, assume its called docs/ - if (is.null(output_dir)) { - output_dir <- "docs" - } - # Get the full file path - full_output_dir <- file.path(root_dir, output_dir) - - # If the output dir doesn't exist, make it - dir.create(full_output_dir, showWarnings = FALSE, recursive = TRUE) - - # Declare full paths - full_output_dir <- normalizePath(full_output_dir, winslash = "/") - - return(full_output_dir) -} - -copy_resources <- function(path = ".", - images_dir = "resources", - output_dir = "manuscript") { - - # Get file path to bookdown.yml - path <- bookdown_path(path) - - # Assume image directory is `resources/images` - res_image_dir <- file.path(path, images_dir) - - # Create the directory if it doesn't exist - dir.create(res_image_dir, showWarnings = FALSE, recursive = TRUE) - - manuscript_image_dir <- file.path(output_dir, images_dir) - - dir.create(manuscript_image_dir, showWarnings = FALSE, recursive = TRUE) - - manuscript_image_dir <- normalizePath(manuscript_image_dir) - - if (dir.exists(res_image_dir)) { - R.utils::copyDirectory(res_image_dir, manuscript_image_dir, recursive = TRUE, overwrite = TRUE) - } -} - -copy_docs <- function(path = ".", output_dir = "manuscript") { - path <- bookdown_destination(path) - R.utils::copyDirectory(path, file.path(output_dir), recursive = TRUE, overwrite = TRUE) -} - -copy_bib <- function(path = ".", output_dir = "manuscript") { - path <- bookdown_path(path) - files <- list.files(path = path, full.names = TRUE, pattern = ".bib$") - if (length(files) > 0) { - file.copy(files, file.path(output_dir), overwrite = TRUE) - } -} - -copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { - quiz_dir <- file.path(quiz_dir) - - if (!dir.exists(quiz_dir)) { - stop(paste( - "The quiz directory specified by quiz_dir:", quiz_dir, "does not exist.", - "If you don't have quizzes, set quiz_dir = NULL" - )) - } - quizzes <- list.files(path = file.path(quiz_dir), full.names = TRUE, pattern = "\\.md$") - if (length(quizzes) > 0) { - fs::file_copy(quizzes, file.path(output_dir, basename(quizzes)), - overwrite = TRUE - ) - } -} - - - #' Convert Bookdown to Leanpub #' #' @param path path to the bookdown book, must have a `_bookdown.yml` file @@ -195,69 +29,7 @@ bookdown_to_leanpub <- function(path = ".", remove_resources_start = FALSE, verbose = TRUE, footer_text = NULL) { - - # Declare regex for finding rmd files - rmd_regex <- "[.][R|r]md$" - - # Get the path to the _bookdown.yml - path <- bookdown_path(path) - - if (verbose) { - message(paste0("Looking for bookdown file in ", path)) - } - rmd_files <- bookdown_rmd_files(path = path) - - if (verbose) { - message(paste0(c("Processing these files: ", rmd_files), collapse = "\n")) - } - - if (render) { - if (verbose) { - message("Rendering the Book") - } - # Get the index file path - index_file <- grep("index", rmd_files, ignore.case = TRUE, value = TRUE) - - index_file <- normalizePath(index_file) - - if (length(index_file) == 0 || is.na(index_file)) { - index_file <- rmd_files[1] - } - message(paste("index_file is", index_file)) - - if (rmarkdown::pandoc_version() >= "2.11") { - output_format <- bookdown::gitbook(pandoc_args = "--citeproc") - output_format$pandoc$args <- c(output_format$pandoc$args, "--citeproc") - } else { - warning("Pandoc version is not greater than 2.11 so citations will not be able to be rendered properly") - output_format <- NULL - } - bookdown::render_book( - input = index_file, - output_format = output_format, - 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") - } + set_up_leanpub(...) bib_files <- list.files(pattern = "[.]bib$") if (length(bib_files) > 0) { @@ -291,23 +63,121 @@ bookdown_to_leanpub <- function(path = ".", ) } } - if (!is.null(quiz_dir)) { - #### Run quiz checks - if (run_quiz_checks) { - message("Checking quizzes") - quiz_checks <- check_quizzes(quiz_dir, - verbose = verbose - ) + ####################### Book.txt creation #################################### + out <- NULL + if (make_book_txt) { + if (verbose > 1) { + message("Running bookdown_to_book_txt") } - copy_quizzes( + bookdown_to_book_txt( + path = path, + output_dir = output_dir, quiz_dir = quiz_dir, - output_dir = output_dir + verbose = verbose ) - if (verbose) { - message("Copying quiz files") + out <- book_txt_file <- file.path(output_dir, "Book.txt") + } else { + # If false, look for Book.txt file to copy to output folder. + book_txt_file <- file.path(path, "Book.txt") + + if (file.exists(book_txt_file)) { + # Copy over an existing book.txt file if it exists + file.copy(from = book_txt_file, to = file.path(output_dir, "Book.txt")) + + out <- book_txt_file <- file.path(output_dir, "Book.txt") + } else { + # If none exists and make_book_txt is false: stop. + stop(paste0( + "Book.txt file does not exist in the main directory: ", path, "and make_book_txt is set to FALSE.", + "There is no Book.txt file. Leanpub needs one. Either make one and place it in the directory path or ", + "use make_book_txt = TRUE and one will be generated for you." + )) } } + ############################# Wrapping up #################################### + message(paste( + "Leanpub ready files are saved to", + output_dir, + "Go to https://leanpub.com/ to publish them using the GitHub writing mode." + )) +} +#' Convert Bookdown to Embed version of Leanpub +#' +#' @param path path to the bookdown book, must have a `_bookdown.yml` file +#' @param chapt_img_key File path to a TSV whose contents are the chapter urls (`url`), +#' the chapter titles (`chapt_title`), the file path to the image to be used for the chapter (`img_path`). +#' Column names `url`, `chapt_title`, and `img_path` must be used. +#' If no chapter title column supplied, the basename of the url will be used, +#' If no image column supplied, default image used. +#' @param default_img A google slide link to the default image to be used for all chapters +#' @param output_dir output directory to put files. It should likely be +#' relative to path +#' @param render if `TRUE`, then [bookdown::render_book()] will be run on each Rmd. +#' @param verbose print diagnostic messages +#' @param remove_resources_start remove the word `resources/` at the front +#' of any image path. +#' @param run_quiz_checks TRUE/FALSE run quiz checks +#' @param make_book_txt Should [ottr::bookdown_to_book_txt()] be run +#' to create a `Book.txt` in the output directory? +#' @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 footer_text Optionally can add a bit of text that will be added to the +#' end of each file before the references section. +#' +#' @return A list of output files and diagnostics +#' @export +#' +bookdown_to_embed_leanpub <- function(path = ".", + chapt_img_key = NULL, + bookdown_index = file.path("docs", "index.html"), + base_url = NULL, + default_img = NULL, + render = TRUE, + output_dir = "manuscript", + make_book_txt = FALSE, + quiz_dir = "quizzes", + run_quiz_checks = FALSE, + remove_resources_start = FALSE, + verbose = TRUE, + footer_text = NULL) { + set_up_leanpub(...) + + + if (!is.null(chapt_img_key)) { + message(paste("Reading in a chapt_img_key TSV file:", chapt_img_key)) + readr::read_tsv(chapt_img_key) + } else { + message("Creating a chapt_img_key TSV file") + chapt_df <- get_chapters(bookdown_index = bookdown_index, + base_url = base_url) + } + + + # If there's no img_path supplied, then use a default image for each. + if (!("img_path" %in% colnames(chapt_df))) { + if (is.null(default_img)) { + default_img <- "https://docs.google.com/presentation/d/1jEUxUY1qXDZ3DUtvTU6NCc6ASG5nx4Gwczv5aAglYX4/edit#slide=id.p" + } + # Set up location to store default image + img_dir <- file.path(output_dir, "embed_chapt_images") + + if (!dir.exists(img_dir)) { + dir.create(img_dir, recursive = TRUE) + } + + # Download default image + chapt_df$img_path <- leanbuild::gs_png_download(url = default_img, output_dir = file.path(img_dir, "embed_chapt_images"), overwrite = TRUE) + } + + output_files <- chapt_df %>% + # Make the data.frame be in the same order + dplyr::select(dplyr::any_of(url, chapt_title, img_path)) %>% + # Run it make_embed_markdown on each row + purrr::pmap( ~ make_embed_markdown(url = ..1, chapt_title = ..2, img_path = ..3)) + + ####################### Book.txt creation #################################### out <- NULL if (make_book_txt) { if (verbose > 1) { @@ -338,6 +208,8 @@ bookdown_to_leanpub <- function(path = ".", )) } } + + ############################# Wrapping up #################################### message(paste( "Leanpub ready files are saved to", output_dir, @@ -345,7 +217,6 @@ bookdown_to_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 @@ -370,17 +241,17 @@ bookdown_to_book_txt <- function(path = ".", rmd_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) + # 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) + # Put files in one vector + all_files <- c(rmd_files, quiz_files) - # Make a vector specifying the file type: quiz or not - file_type <- c( - rep("non-quiz", length(rmd_files)), - rep("quiz", length(quiz_files)) - ) + # Make a vector specifying the file type: quiz or not + file_type <- c( + rep("non-quiz", length(rmd_files)), + rep("quiz", length(quiz_files)) + ) } else { all_files <- rmd_files file_type <- rep("non-quiz", length(rmd_files)) @@ -417,3 +288,92 @@ bookdown_to_book_txt <- function(path = ".", # need to fix about quiz writeLines(all_files, con = book_txt) } + + +#' Make Leanpub file that has embed webpage of a chapter +#' +#' @param url The url to the chapter that is to be embed +#' @param chapt_name Title of chapter to be used as file name and printed on iframe +#' @param img File path to image to use for poster +#' @return A +#' +#' @export +make_embed_markdown <- function(url, + chapt_title, + img_path, + output_dir = "manuscript", + verbose = TRUE) { + # Arguments: + # url: The url to the chapter + # chapt_name: The title of the chapter to be used as a header + # img: file path to the image to be used in the preview + # + # Returns: A markdown document ready for Leanpub and the image copied to the manuscript folder + + chapt_file_name <- gsub(" ", "-", chapt_title) + + # Declare output file + output_file <- file.path(output_dir, paste0(chapt_file_name, ".md")) + + file_contents <- c( + paste("#", chapt_title), + " ", + paste0("{type: iframe, title:", chapt_title, " width: 400, height: 400, poster:", img_path, "}"), + paste0("![](", url, ")") + ) + + write(file_contents, file = output_file) + + manuscript_dir <- file.path(output_dir, dirname(img_path)) + + if (!dir.exists(manuscript_dir)) { + dir.create(manuscript_dir, recursive = TRUE) + } + + file.copy(from = img_path, to = file.path(output_dir, img_path), overwrite = TRUE) + + if (verbose) { + message(paste0("Output saved to: ", output_file)) + } + + return(output_file) +} + +#' Make Leanpub file that has embed webpage of a chapter +#' +#' @param bookdown_index The file path of the rendered bookdown index.html file +#' @param 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 +#' +#' @return A data.frame of the chapter urls and their titles that are to be ported to Leanpub. +#' This can be passed to +#' +#' @export +#' +get_chapters <- function(bookdown_index = file.path("docs", "index.html"), + base_url = NULL) { + + # Read in html + index_html <- suppressWarnings(try(xml2::read_html(paste(bookdown_index, collapse = "\n")))) + + # Extract chapter nodes + nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'li[@class="chapter"]')) + + if (length(nodes) > 0) { + # Format into a data.frame + chapt_data <- rvest::html_attrs(nodes) %>% + dplyr::bind_rows() %>% + dplyr::rename_with(~ gsub("-", "_", .x, fixed = TRUE)) %>% + dplyr::mutate( + chapt_name = stringr::word(rvest::html_text(nodes), sep = "\n", 1), + url = paste0(base_url, data_path) + ) %>% + dplyr::select(url, chapt_title = chapt_name) %>% + as.data.frame() %>% + dplyr::distinct(url, .keep_all = TRUE) + } else { + stop(paste("Chapter retrieval from:", bookdown_index, "Failed.")) + } + + return(chapt_data) +} diff --git a/R/set_up.R b/R/set_up.R new file mode 100644 index 0000000..66307cf --- /dev/null +++ b/R/set_up.R @@ -0,0 +1,282 @@ +#' Load in Bookdown specifications from _bookdown.yml +#' +#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory +#' +#' @return The yaml contents using yaml::yaml.load_file() +#' @export + +get_bookdown_spec <- function(path = ".") { + + # Get the file path to _bookdown.yaml + file_path <- bookdown_file(path = path) + + # Read in yaml + suppressWarnings({ + yaml_contents <- yaml::yaml.load_file(file_path) + }) + + return(yaml_contents) +} + +#' Find main Bookdown directory +#' +#' @param path Where to look for the file. By default looks in current directory. +#' +#' @return Returns the directory where the _bookdown.yml is contained. +#' @export + +bookdown_path <- function(path = ".") { + + # See what unzip is being used + operating_system <- Sys.info()[1] + + path <- rprojroot::find_root(rprojroot::has_file("_bookdown.yml"), path = file.path(path)) + + return(path) +} + +#' Find file path to _bookdown.yml +#' +#' @param path Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory +#' +#' @return The file path to _bookdown.yml +#' @export +#' +bookdown_file <- function(path = ".") { + root_dir <- bookdown_path(path = file.path(path)) + file_path <- file.path(root_dir, "_bookdown.yml") + + return(file_path) +} + +#' Get file paths all Rmds in the bookdown directory +#' +#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory +#' +#' @return The file paths to Rmds listed in the _bookdown.yml file. +#' @export +#' +bookdown_rmd_files <- function(path = ".") { + spec <- get_bookdown_spec(file.path(path)) + + files <- spec$rmd_files + if (is.null(files) || all(is.na(files)) || length(files) == 0) { + warning( + "No bookdown specification of the files, using ", + "list.files(pattern ='.Rmd')" + ) + root_dir <- bookdown_path(path = file.path(path)) + files <- list.files( + pattern = "[.]Rmd", ignore.case = TRUE, + path = root_dir, full.names = FALSE + ) + } + return(files) +} + +#' Declare file path to docs/ folder +#' +#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory +#' +#' @return The file paths to Rmds listed in the _bookdown.yml file. +#' @export +#' +bookdown_destination <- function(path = ".") { + + # Find _bookdown.yml + root_dir <- bookdown_path(path = file.path(path)) + + # Get specs from _bookdown.yml + spec <- get_bookdown_spec(path = file.path(path)) + + # Find output directory declared in the bookdown.yml + output_dir <- spec$output_dir + + # If none specified, assume its called docs/ + if (is.null(output_dir)) { + output_dir <- "docs" + } + # Get the full file path + full_output_dir <- file.path(root_dir, output_dir) + + # If the output dir doesn't exist, make it + dir.create(full_output_dir, showWarnings = FALSE, recursive = TRUE) + + # Declare full paths + full_output_dir <- normalizePath(full_output_dir, winslash = "/") + + return(full_output_dir) +} + +copy_resources <- function(path = ".", + images_dir = "resources", + output_dir = "manuscript") { + + # Get file path to bookdown.yml + path <- bookdown_path(path) + + # Assume image directory is `resources/images` + res_image_dir <- file.path(path, images_dir) + + # Create the directory if it doesn't exist + dir.create(res_image_dir, showWarnings = FALSE, recursive = TRUE) + + manuscript_image_dir <- file.path(output_dir, images_dir) + + dir.create(manuscript_image_dir, showWarnings = FALSE, recursive = TRUE) + + manuscript_image_dir <- normalizePath(manuscript_image_dir) + + if (dir.exists(res_image_dir)) { + R.utils::copyDirectory(res_image_dir, manuscript_image_dir, recursive = TRUE, overwrite = TRUE) + } +} + +copy_docs <- function(path = ".", output_dir = "manuscript") { + path <- bookdown_destination(path) + R.utils::copyDirectory(path, file.path(output_dir), recursive = TRUE, overwrite = TRUE) +} + +copy_bib <- function(path = ".", output_dir = "manuscript") { + path <- bookdown_path(path) + files <- list.files(path = path, full.names = TRUE, pattern = ".bib$") + if (length(files) > 0) { + file.copy(files, file.path(output_dir), overwrite = TRUE) + } +} + +copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { + quiz_dir <- file.path(quiz_dir) + + if (!dir.exists(quiz_dir)) { + stop(paste( + "The quiz directory specified by quiz_dir:", quiz_dir, "does not exist.", + "If you don't have quizzes, set quiz_dir = NULL" + )) + } + quizzes <- list.files(path = file.path(quiz_dir), full.names = TRUE, pattern = "\\.md$") + if (length(quizzes) > 0) { + fs::file_copy(quizzes, file.path(output_dir, basename(quizzes)), + overwrite = TRUE + ) + } +} + +#' Set up Manuscript folder for Leanpub publishing +#' +#' @param path path to the bookdown book, must have a `_bookdown.yml` file +#' @param output_dir output directory to put files. It should likely be +#' relative to path +#' @param render if `TRUE`, then [bookdown::render_book()] will be run on each Rmd. +#' @param verbose print diagnostic messages +#' @param remove_resources_start remove the word `resources/` at the front +#' of any image path. +#' @param run_quiz_checks TRUE/FALSE run quiz checks +#' @param make_book_txt Should [ottr::bookdown_to_book_txt()] be run +#' to create a `Book.txt` in the output directory? +#' @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 footer_text Optionally can add a bit of text that will be added to the +#' end of each file before the references section. +#' +#' @return A list of output files and diagnostics +#' @export +#' +set_up_leanpub <- function(path = ".", + clean_up = FALSE + render = TRUE, + output_dir = "manuscript", + make_book_txt = FALSE, + quiz_dir = "quizzes", + run_quiz_checks = FALSE, + remove_resources_start = FALSE, + verbose = TRUE, + footer_text = NULL) { + + if (clean_up && dir.exists(output_dir)) { + message(paste("Clearing out old version of output files:", output_dir)) + file.remove(output_dir, recursive = TRUE) + dir.create(output_dir, showWarnings = FALSE) + } + + # Declare regex for finding rmd files + rmd_regex <- "[.][R|r]md$" + + # Get the path to the _bookdown.yml + path <- bookdown_path(path) + + if (verbose) { + message(paste0("Looking for bookdown file in ", path)) + } + rmd_files <- bookdown_rmd_files(path = path) + + if (verbose) { + message(paste0(c("Processing these files: ", rmd_files), collapse = "\n")) + } + + if (render) { + if (verbose) { + message("Rendering the Book") + } + # Get the index file path + index_file <- grep("index", rmd_files, ignore.case = TRUE, value = TRUE) + + index_file <- normalizePath(index_file) + + if (length(index_file) == 0 || is.na(index_file)) { + index_file <- rmd_files[1] + } + message(paste("index_file is", index_file)) + + if (rmarkdown::pandoc_version() >= "2.11") { + output_format <- bookdown::gitbook(pandoc_args = "--citeproc") + output_format$pandoc$args <- c(output_format$pandoc$args, "--citeproc") + } else { + warning("Pandoc version is not greater than 2.11 so citations will not be able to be rendered properly") + output_format <- NULL + } + bookdown::render_book( + input = index_file, + output_format = output_format, + 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") + } + + if (!is.null(quiz_dir)) { + #### Run quiz checks + if (run_quiz_checks) { + message("Checking quizzes") + quiz_checks <- check_quizzes(quiz_dir, + verbose = verbose + ) + } + copy_quizzes( + quiz_dir = quiz_dir, + output_dir = output_dir + ) + if (verbose) { + message("Copying quiz files") + } + } +} From 58c894225c8f44084caaee7007be3d1b1f63f58c Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 11:07:42 -0500 Subject: [PATCH 02/11] Somewhat working --- R/bookdown_to_leanpub.R | 10 ++++------ R/set_up.R | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 9a6dee1..69ee7fd 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -144,7 +144,6 @@ bookdown_to_embed_leanpub <- function(path = ".", footer_text = NULL) { set_up_leanpub(...) - if (!is.null(chapt_img_key)) { message(paste("Reading in a chapt_img_key TSV file:", chapt_img_key)) readr::read_tsv(chapt_img_key) @@ -154,7 +153,6 @@ bookdown_to_embed_leanpub <- function(path = ".", base_url = base_url) } - # If there's no img_path supplied, then use a default image for each. if (!("img_path" %in% colnames(chapt_df))) { if (is.null(default_img)) { @@ -168,12 +166,12 @@ bookdown_to_embed_leanpub <- function(path = ".", } # Download default image - chapt_df$img_path <- leanbuild::gs_png_download(url = default_img, output_dir = file.path(img_dir, "embed_chapt_images"), overwrite = TRUE) + chapt_df$img_path <- leanbuild::gs_png_download(url = default_img, output_dir = img_dir, overwrite = TRUE) } - output_files <- chapt_df %>% + md_output_files <- chapt_df %>% # Make the data.frame be in the same order - dplyr::select(dplyr::any_of(url, chapt_title, img_path)) %>% + dplyr::select(dplyr::any_of("url"), dplyr::any_of("chapt_title"), dplyr::any_of("img_path")) %>% # Run it make_embed_markdown on each row purrr::pmap( ~ make_embed_markdown(url = ..1, chapt_title = ..2, img_path = ..3)) @@ -184,7 +182,7 @@ bookdown_to_embed_leanpub <- function(path = ".", message("Running bookdown_to_book_txt") } bookdown_to_book_txt( - path = path, + path = output_dir, output_dir = output_dir, quiz_dir = quiz_dir, verbose = verbose diff --git a/R/set_up.R b/R/set_up.R index 66307cf..6e285dc 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -184,7 +184,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' @export #' set_up_leanpub <- function(path = ".", - clean_up = FALSE + clean_up = FALSE, render = TRUE, output_dir = "manuscript", make_book_txt = FALSE, From d2b13cee78e6456138d8d585d4490b06c3883c7b Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 11:25:27 -0500 Subject: [PATCH 03/11] Update docs --- DESCRIPTION | 2 +- NAMESPACE | 4 ++ R/bookdown_to_leanpub.R | 17 ++++++- man/bookdown_destination.Rd | 2 +- man/bookdown_file.Rd | 2 +- man/bookdown_path.Rd | 2 +- man/bookdown_rmd_files.Rd | 2 +- man/bookdown_to_embed_leanpub.Rd | 76 ++++++++++++++++++++++++++++++++ man/get_bookdown_spec.Rd | 2 +- man/get_chapters.Rd | 21 +++++++++ man/make_embed_markdown.Rd | 27 ++++++++++++ man/set_up_leanpub.Rd | 50 +++++++++++++++++++++ 12 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 man/bookdown_to_embed_leanpub.Rd create mode 100644 man/get_chapters.Rd create mode 100644 man/make_embed_markdown.Rd create mode 100644 man/set_up_leanpub.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4a1b192..796ecda 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -50,4 +50,4 @@ Type: Package VignetteBuilder: knitr URL: https://github.com/jhudsl/ottr BugReports: https://github.comjhudsl/ottr/issues -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 diff --git a/NAMESPACE b/NAMESPACE index bb12a5c..b352783 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,6 +7,7 @@ export(bookdown_file) export(bookdown_path) export(bookdown_rmd_files) export(bookdown_to_book_txt) +export(bookdown_to_embed_leanpub) export(bookdown_to_leanpub) export(check_all_questions) export(check_question) @@ -24,6 +25,7 @@ export(example_repo_setup) export(extract_meta) export(extract_quiz) export(get_bookdown_spec) +export(get_chapters) export(get_image_from_slide) export(get_image_link_from_slide) export(get_slide_id) @@ -34,6 +36,7 @@ export(gs_png_download) export(gs_png_url) export(include_slide) export(leanpub_check) +export(make_embed_markdown) export(parse_q_tag) export(parse_quiz) export(parse_quiz_df) @@ -42,6 +45,7 @@ export(render_coursera) export(replace_html) export(replace_single_html) export(set_knitr_image_path) +export(set_up_leanpub) export(simple_references) importFrom(fs,dir_copy) importFrom(magrittr,"%>%") diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 69ee7fd..3192aac 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -110,6 +110,9 @@ bookdown_to_leanpub <- function(path = ".", #' Column names `url`, `chapt_title`, and `img_path` must be used. #' If no chapter title column supplied, the basename of the url will be used, #' If no image column supplied, default image used. +#' @param bookdown_index The file path of the rendered bookdown index.html file +#' @param s 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 #' @param default_img A google slide link to the default image to be used for all chapters #' @param output_dir output directory to put files. It should likely be #' relative to path @@ -128,7 +131,16 @@ bookdown_to_leanpub <- function(path = ".", #' #' @return A list of output files and diagnostics #' @export -#' +#' +#' @examples +#' \dontrun{ +#' +#' ottr::bookdown_to_embed_leanpub(base_url = ") +#' +#' ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapt) +#' +#' +#' } bookdown_to_embed_leanpub <- function(path = ".", chapt_img_key = NULL, bookdown_index = file.path("docs", "index.html"), @@ -149,6 +161,9 @@ bookdown_to_embed_leanpub <- function(path = ".", readr::read_tsv(chapt_img_key) } else { 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.") + } chapt_df <- get_chapters(bookdown_index = bookdown_index, base_url = base_url) } diff --git a/man/bookdown_destination.Rd b/man/bookdown_destination.Rd index bc86e03..c677cf0 100644 --- a/man/bookdown_destination.Rd +++ b/man/bookdown_destination.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/set_up.R \name{bookdown_destination} \alias{bookdown_destination} \title{Declare file path to docs/ folder} diff --git a/man/bookdown_file.Rd b/man/bookdown_file.Rd index 2cfedfa..78d133f 100644 --- a/man/bookdown_file.Rd +++ b/man/bookdown_file.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/set_up.R \name{bookdown_file} \alias{bookdown_file} \title{Find file path to _bookdown.yml} diff --git a/man/bookdown_path.Rd b/man/bookdown_path.Rd index aa28e6d..27a0817 100644 --- a/man/bookdown_path.Rd +++ b/man/bookdown_path.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/set_up.R \name{bookdown_path} \alias{bookdown_path} \title{Find main Bookdown directory} diff --git a/man/bookdown_rmd_files.Rd b/man/bookdown_rmd_files.Rd index b637f82..e085722 100644 --- a/man/bookdown_rmd_files.Rd +++ b/man/bookdown_rmd_files.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/set_up.R \name{bookdown_rmd_files} \alias{bookdown_rmd_files} \title{Get file paths all Rmds in the bookdown directory} diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/bookdown_to_embed_leanpub.Rd new file mode 100644 index 0000000..179f7bc --- /dev/null +++ b/man/bookdown_to_embed_leanpub.Rd @@ -0,0 +1,76 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bookdown_to_leanpub.R +\name{bookdown_to_embed_leanpub} +\alias{bookdown_to_embed_leanpub} +\title{Convert Bookdown to Embed version of Leanpub} +\usage{ +bookdown_to_embed_leanpub( + path = ".", + chapt_img_key = NULL, + bookdown_index = file.path("docs", "index.html"), + base_url = NULL, + default_img = NULL, + render = TRUE, + output_dir = "manuscript", + make_book_txt = FALSE, + quiz_dir = "quizzes", + run_quiz_checks = FALSE, + remove_resources_start = FALSE, + verbose = TRUE, + footer_text = NULL +) +} +\arguments{ +\item{path}{path to the bookdown book, must have a `_bookdown.yml` file} + +\item{chapt_img_key}{File path to a TSV whose contents are the chapter urls (`url`), +the chapter titles (`chapt_title`), the file path to the image to be used for the chapter (`img_path`). +Column names `url`, `chapt_title`, and `img_path` must be used. +If no chapter title column supplied, the basename of the url will be used, +If no image column supplied, default image used.} + +\item{bookdown_index}{The file path of the rendered bookdown index.html file} + +\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.} + +\item{output_dir}{output directory to put files. It should likely be +relative to path} + +\item{make_book_txt}{Should [ottr::bookdown_to_book_txt()] be run +to create a `Book.txt` in the output directory?} + +\item{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} + +\item{run_quiz_checks}{TRUE/FALSE run quiz checks} + +\item{remove_resources_start}{remove the word `resources/` at the front +of any image path.} + +\item{verbose}{print diagnostic messages} + +\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{s}{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} +} +\value{ +A list of output files and diagnostics +} +\description{ +Convert Bookdown to Embed version of Leanpub +} +\examples{ +\dontrun{ + +ottr::bookdown_to_embed_leanpub(base_url = ") + +ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapt) + + +} +} diff --git a/man/get_bookdown_spec.Rd b/man/get_bookdown_spec.Rd index b483ade..1bfd9b3 100644 --- a/man/get_bookdown_spec.Rd +++ b/man/get_bookdown_spec.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/set_up.R \name{get_bookdown_spec} \alias{get_bookdown_spec} \title{Load in Bookdown specifications from _bookdown.yml} diff --git a/man/get_chapters.Rd b/man/get_chapters.Rd new file mode 100644 index 0000000..9e57d5c --- /dev/null +++ b/man/get_chapters.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bookdown_to_leanpub.R +\name{get_chapters} +\alias{get_chapters} +\title{Make Leanpub file that has embed webpage of a chapter} +\usage{ +get_chapters(bookdown_index = file.path("docs", "index.html"), base_url = NULL) +} +\arguments{ +\item{bookdown_index}{The file path of the rendered bookdown index.html file} + +\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} +} +\value{ +A data.frame of the chapter urls and their titles that are to be ported to Leanpub. +This can be passed to +} +\description{ +Make Leanpub file that has embed webpage of a chapter +} diff --git a/man/make_embed_markdown.Rd b/man/make_embed_markdown.Rd new file mode 100644 index 0000000..eff5f2b --- /dev/null +++ b/man/make_embed_markdown.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bookdown_to_leanpub.R +\name{make_embed_markdown} +\alias{make_embed_markdown} +\title{Make Leanpub file that has embed webpage of a chapter} +\usage{ +make_embed_markdown( + url, + chapt_title, + img_path, + output_dir = "manuscript", + verbose = TRUE +) +} +\arguments{ +\item{url}{The url to the chapter that is to be embed} + +\item{chapt_name}{Title of chapter to be used as file name and printed on iframe} + +\item{img}{File path to image to use for poster} +} +\value{ +A +} +\description{ +Make Leanpub file that has embed webpage of a chapter +} diff --git a/man/set_up_leanpub.Rd b/man/set_up_leanpub.Rd new file mode 100644 index 0000000..9d78606 --- /dev/null +++ b/man/set_up_leanpub.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/set_up.R +\name{set_up_leanpub} +\alias{set_up_leanpub} +\title{Set up Manuscript folder for Leanpub publishing} +\usage{ +set_up_leanpub( + path = ".", + clean_up = FALSE, + render = TRUE, + output_dir = "manuscript", + make_book_txt = FALSE, + quiz_dir = "quizzes", + run_quiz_checks = FALSE, + remove_resources_start = FALSE, + verbose = TRUE, + footer_text = NULL +) +} +\arguments{ +\item{path}{path to the bookdown book, must have a `_bookdown.yml` file} + +\item{render}{if `TRUE`, then [bookdown::render_book()] will be run on each Rmd.} + +\item{output_dir}{output directory to put files. It should likely be +relative to path} + +\item{make_book_txt}{Should [ottr::bookdown_to_book_txt()] be run +to create a `Book.txt` in the output directory?} + +\item{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} + +\item{run_quiz_checks}{TRUE/FALSE run quiz checks} + +\item{remove_resources_start}{remove the word `resources/` at the front +of any image path.} + +\item{verbose}{print diagnostic messages} + +\item{footer_text}{Optionally can add a bit of text that will be added to the +end of each file before the references section.} +} +\value{ +A list of output files and diagnostics +} +\description{ +Set up Manuscript folder for Leanpub publishing +} From 8c089d1682e84329cbae0483c543593bc3824f4c Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 11:54:34 -0500 Subject: [PATCH 04/11] Update some vignette things --- R/bookdown_to_leanpub.R | 28 ++++++++++++++++--------- inst/extdata/{ => quizzes}/quiz_bad.md | 0 inst/extdata/{ => quizzes}/quiz_good.md | 0 man/bookdown_to_embed_leanpub.Rd | 8 +++---- man/bookdown_to_leanpub.Rd | 3 ++- 5 files changed, 24 insertions(+), 15 deletions(-) rename inst/extdata/{ => quizzes}/quiz_bad.md (100%) rename inst/extdata/{ => quizzes}/quiz_good.md (100%) diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 3192aac..d310532 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -28,9 +28,18 @@ bookdown_to_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL) { - set_up_leanpub(...) + footer_text = NULL, + ...) { + set_up_leanpub() + # Establish path + path <- bookdown_path(path) + + rmd_regex <- "[.][R|r]md$" + + # Extract the names of the Rmd files (the chapters) + 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))) @@ -132,13 +141,11 @@ bookdown_to_leanpub <- function(path = ".", #' @return A list of output files and diagnostics #' @export #' -#' @examples -#' \dontrun{ -#' -#' ottr::bookdown_to_embed_leanpub(base_url = ") +#' @examples \dontrun{ #' -#' ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapt) +#' ottr::bookdown_to_embed_leanpub(base_url = "") #' +#' ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapter_urls.tsv") #' #' } bookdown_to_embed_leanpub <- function(path = ".", @@ -153,8 +160,9 @@ bookdown_to_embed_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL) { - set_up_leanpub(...) + footer_text = NULL, + ...) { + set_up_leanpub() if (!is.null(chapt_img_key)) { message(paste("Reading in a chapt_img_key TSV file:", chapt_img_key)) @@ -181,7 +189,7 @@ bookdown_to_embed_leanpub <- function(path = ".", } # Download default image - chapt_df$img_path <- leanbuild::gs_png_download(url = default_img, output_dir = img_dir, overwrite = TRUE) + chapt_df$img_path <- gs_png_download(url = default_img, output_dir = img_dir, overwrite = TRUE) } md_output_files <- chapt_df %>% diff --git a/inst/extdata/quiz_bad.md b/inst/extdata/quizzes/quiz_bad.md similarity index 100% rename from inst/extdata/quiz_bad.md rename to inst/extdata/quizzes/quiz_bad.md diff --git a/inst/extdata/quiz_good.md b/inst/extdata/quizzes/quiz_good.md similarity index 100% rename from inst/extdata/quiz_good.md rename to inst/extdata/quizzes/quiz_good.md diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/bookdown_to_embed_leanpub.Rd index 179f7bc..3923782 100644 --- a/man/bookdown_to_embed_leanpub.Rd +++ b/man/bookdown_to_embed_leanpub.Rd @@ -17,7 +17,8 @@ bookdown_to_embed_leanpub( run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL + footer_text = NULL, + ... ) } \arguments{ @@ -67,10 +68,9 @@ Convert Bookdown to Embed version of Leanpub \examples{ \dontrun{ -ottr::bookdown_to_embed_leanpub(base_url = ") - -ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapt) +ottr::bookdown_to_embed_leanpub(base_url = "") +ottr::bookdown_to_embed_leanpub(chapt_img_key = "chapter_urls.tsv") } } diff --git a/man/bookdown_to_leanpub.Rd b/man/bookdown_to_leanpub.Rd index 552cf8c..de1c59e 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, + ... ) } \arguments{ From f0d4dd43c9b77a3a16d33847ec3e0bb98ea4415c Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:09:32 -0500 Subject: [PATCH 05/11] Update documentation --- DESCRIPTION | 3 ++- R/bookdown_to_leanpub.R | 17 ++++++++++------- R/set_up.R | 2 ++ man/bookdown_to_embed_leanpub.Rd | 6 +++--- man/make_embed_markdown.Rd | 11 ++++++++--- man/set_up_leanpub.Rd | 3 +++ 6 files changed, 28 insertions(+), 14 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 796ecda..36e1207 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -37,7 +37,8 @@ Imports: googledrive, yaml, stringr, knitr (>= 1.33), - qpdf + qpdf, + purrr Suggests: remotes, testthat diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index d310532..960470b 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -120,7 +120,7 @@ bookdown_to_leanpub <- function(path = ".", #' If no chapter title column supplied, the basename of the url will be used, #' If no image column supplied, default image used. #' @param bookdown_index The file path of the rendered bookdown index.html file -#' @param s The base url of where the chapters are published -- the url to provide to the iframe in Leanpub +#' @param 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 #' @param default_img A google slide link to the default image to be used for all chapters #' @param output_dir output directory to put files. It should likely be @@ -314,9 +314,12 @@ bookdown_to_book_txt <- function(path = ".", #' Make Leanpub file that has embed webpage of a chapter #' #' @param url The url to the chapter that is to be embed -#' @param chapt_name Title of chapter to be used as file name and printed on iframe -#' @param img File path to image to use for poster -#' @return A +#' @param chapt_title Title of chapter to be used as file name and printed on iframe +#' @param img_path File path to image to use for poster +#' @param output_dir output directory to put files. It should likely be +#' relative to path +#' @param verbose print diagnostic messages +#' @return A markdown file with an iframe of the provided chapter #' #' @export make_embed_markdown <- function(url, @@ -326,7 +329,7 @@ make_embed_markdown <- function(url, verbose = TRUE) { # Arguments: # url: The url to the chapter - # chapt_name: The title of the chapter to be used as a header + # chapt_title: The title of the chapter to be used as a header # img: file path to the image to be used in the preview # # Returns: A markdown document ready for Leanpub and the image copied to the manuscript folder @@ -386,10 +389,10 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"), dplyr::bind_rows() %>% dplyr::rename_with(~ gsub("-", "_", .x, fixed = TRUE)) %>% dplyr::mutate( - chapt_name = stringr::word(rvest::html_text(nodes), sep = "\n", 1), + chapt_title = stringr::word(rvest::html_text(nodes), sep = "\n", 1), url = paste0(base_url, data_path) ) %>% - dplyr::select(url, chapt_title = chapt_name) %>% + dplyr::select(url, chapt_title) %>% as.data.frame() %>% dplyr::distinct(url, .keep_all = TRUE) } else { diff --git a/R/set_up.R b/R/set_up.R index 6e285dc..d288e1a 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -167,6 +167,8 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' @param path path to the bookdown book, must have a `_bookdown.yml` file #' @param output_dir output directory to put files. It should likely be #' relative to path +#' @param clean_up TRUE/FALSE the old output directory should be deleted and +#' everything created fresh. #' @param render if `TRUE`, then [bookdown::render_book()] will be run on each Rmd. #' @param verbose print diagnostic messages #' @param remove_resources_start remove the word `resources/` at the front diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/bookdown_to_embed_leanpub.Rd index 3923782..ae6b284 100644 --- a/man/bookdown_to_embed_leanpub.Rd +++ b/man/bookdown_to_embed_leanpub.Rd @@ -32,6 +32,9 @@ If no image column supplied, default image used.} \item{bookdown_index}{The file path of the rendered bookdown index.html file} +\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{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.} @@ -55,9 +58,6 @@ 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{s}{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} } \value{ A list of output files and diagnostics diff --git a/man/make_embed_markdown.Rd b/man/make_embed_markdown.Rd index eff5f2b..b9e6cdb 100644 --- a/man/make_embed_markdown.Rd +++ b/man/make_embed_markdown.Rd @@ -15,12 +15,17 @@ make_embed_markdown( \arguments{ \item{url}{The url to the chapter that is to be embed} -\item{chapt_name}{Title of chapter to be used as file name and printed on iframe} +\item{chapt_title}{Title of chapter to be used as file name and printed on iframe} -\item{img}{File path to image to use for poster} +\item{img_path}{File path to image to use for poster} + +\item{output_dir}{output directory to put files. It should likely be +relative to path} + +\item{verbose}{print diagnostic messages} } \value{ -A +A markdown file with an iframe of the provided chapter } \description{ Make Leanpub file that has embed webpage of a chapter diff --git a/man/set_up_leanpub.Rd b/man/set_up_leanpub.Rd index 9d78606..d64d819 100644 --- a/man/set_up_leanpub.Rd +++ b/man/set_up_leanpub.Rd @@ -20,6 +20,9 @@ set_up_leanpub( \arguments{ \item{path}{path to the bookdown book, must have a `_bookdown.yml` file} +\item{clean_up}{TRUE/FALSE the old output directory should be deleted and +everything created fresh.} + \item{render}{if `TRUE`, then [bookdown::render_book()] will be run on each Rmd.} \item{output_dir}{output directory to put files. It should likely be From 47887c12e31e2ac34f63f169df0a5b4ee05bf515 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:12:46 -0500 Subject: [PATCH 06/11] Update docs and make some stops warnings instead --- R/aaa_utils.R | 2 +- R/quiz.R | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/aaa_utils.R b/R/aaa_utils.R index a2730e1..5031b48 100644 --- a/R/aaa_utils.R +++ b/R/aaa_utils.R @@ -1,7 +1,7 @@ utils::globalVariables(c( "num", "quiz_dir", "type_url", "file_name", "trimmed", "quiz", - "quiz_path", "type", "q_num", "verbose" + "quiz_path", "type", "q_num", "verbose", "chapt_title", "data_path" )) # get script path and number of paragraphs diff --git a/R/quiz.R b/R/quiz.R index c1d770b..598784e 100644 --- a/R/quiz.R +++ b/R/quiz.R @@ -245,10 +245,10 @@ extract_quiz <- function(quiz_lines) { end <- grep("^\\s*\\{\\s*/\\s*quiz", quiz_lines) if (length(start) == 0) { - stop("Quiz should start with a { } tag and end with {\\quiz}.") + warning("Quiz should start with a { } tag and end with {\\quiz}.") } if (length(end) == 0) { - stop("Could not find end tag of quiz; should end with: {\\quiz}.") + warning("Could not find end tag of quiz; should end with: {\\quiz}.") } # Extract main quiz tag quiz_tag <- quiz_lines[start] From 0f655805959a4ab0c2af27c288da81405cde3d19 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:23:17 -0500 Subject: [PATCH 07/11] Edits --- .Rbuildignore | 8 ++++++++ R/bookdown_to_leanpub.R | 6 ++---- vignettes/getting-started.Rmd | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index ab9ac70..d2a75c4 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -20,3 +20,11 @@ vignettes/.httr-oauth$ ^run_test\.R$ ^\.github$ ^pkgdown$ +^vignettes/01-intro.Rmd$ +^vignettes/02-example-chapter.Rmd$ +^vignettes/_bookdown.yml$ +^vignettes/index.Rmd$ +^vignettes/quizzes$ +^vignettes/references.bib$ +^vignettes/toc_close.css$ +^package_bundles/$ diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 960470b..6b3130a 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -28,8 +28,7 @@ bookdown_to_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL, - ...) { + footer_text = NULL) { set_up_leanpub() # Establish path @@ -160,8 +159,7 @@ bookdown_to_embed_leanpub <- function(path = ".", run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL, - ...) { + footer_text = NULL) { set_up_leanpub() if (!is.null(chapt_img_key)) { diff --git a/vignettes/getting-started.Rmd b/vignettes/getting-started.Rmd index 39b674e..1c3e4be 100644 --- a/vignettes/getting-started.Rmd +++ b/vignettes/getting-started.Rmd @@ -223,8 +223,8 @@ If there is text you would like added to the end of each chapter (like a link to survey_link <- "Please provide any feedback you have in [this survey](www.some_link.org)" # Supply the footer text in the main function -ottr::bookdown_to_leanpub(make_book_txt = TRUE, - footer_text = survey_link) +ottr::bookdown_to_leanpub(make_book_txt = TRUE, + footer_text = survey_link) ``` ### Wrapping up From 2873ab4d06cb76de47f254959e8698f3fd522f10 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:29:13 -0500 Subject: [PATCH 08/11] Update documentation --- man/bookdown_to_embed_leanpub.Rd | 3 +-- man/bookdown_to_leanpub.Rd | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/bookdown_to_embed_leanpub.Rd index ae6b284..d355c26 100644 --- a/man/bookdown_to_embed_leanpub.Rd +++ b/man/bookdown_to_embed_leanpub.Rd @@ -17,8 +17,7 @@ bookdown_to_embed_leanpub( run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL, - ... + footer_text = NULL ) } \arguments{ diff --git a/man/bookdown_to_leanpub.Rd b/man/bookdown_to_leanpub.Rd index de1c59e..552cf8c 100644 --- a/man/bookdown_to_leanpub.Rd +++ b/man/bookdown_to_leanpub.Rd @@ -13,8 +13,7 @@ bookdown_to_leanpub( run_quiz_checks = FALSE, remove_resources_start = FALSE, verbose = TRUE, - footer_text = NULL, - ... + footer_text = NULL ) } \arguments{ From 3e3085d69a363a60f106ba1d7f6e14db40d6f8b1 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:36:19 -0500 Subject: [PATCH 09/11] Move some dependencies to suggestions --- DESCRIPTION | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 36e1207..489e2ce 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,8 +22,6 @@ Imports: googledrive, httr, fs, jsonlite, - utils, - tibble, rmarkdown (>= 2.10), xml2, rvest, @@ -41,7 +39,9 @@ Imports: googledrive, purrr Suggests: remotes, - testthat + testthat, + tibble, + utils Encoding: UTF-8 LazyData: true Remotes: jhudsl/text2speech, From c084aad6f337623d28615675217fbda447485a10 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:44:28 -0500 Subject: [PATCH 10/11] Make another stop -> warning --- R/quiz.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/quiz.R b/R/quiz.R index 598784e..8563b6d 100644 --- a/R/quiz.R +++ b/R/quiz.R @@ -452,7 +452,7 @@ check_question <- function(question_df, quiz_name = NA, verbose = TRUE) { # Get prompt if its there if (!any(grepl("^\\? ", question_df$original))) { - stop("Could not find prompt for question. Question prompts start line with '?'") + warning("Could not find prompt for question. Question prompts start line with '?'") } prompt <- question_df$original[question_df$type == "prompt"] From 5dcee8a82e65a3f94f3269a3cb244e826b9e30aa Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 4 Feb 2022 12:47:45 -0500 Subject: [PATCH 11/11] Don't fail on warning --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index fb7b370..d894284 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -75,7 +75,7 @@ jobs: _R_CHECK_CRAN_INCOMING_REMOTE_: false run: | options(crayon.enabled = TRUE) - rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check") + rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), check_dir = "check") shell: Rscript {0} - name: Upload check results