From 02dac57c4499814b76625e6cf83cc55f2ab4435c Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 1 Jul 2024 10:29:43 -0400 Subject: [PATCH] Update quarto stuffs --- DESCRIPTION | 1 - R/coursera_and_leanpub.R | 3 +- R/iframe_leanpub.R | 6 +-- R/set_up.R | 107 ++++++++++++++++++++++++++------------- 4 files changed, 78 insertions(+), 39 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f0fb1eb..efbdf96 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,6 @@ Imports: curl, dplyr, fs, - glue, httr, jsonlite, knitr (>= 1.33), diff --git a/R/coursera_and_leanpub.R b/R/coursera_and_leanpub.R index faa2cec..2cb7c4d 100644 --- a/R/coursera_and_leanpub.R +++ b/R/coursera_and_leanpub.R @@ -200,6 +200,7 @@ convert_coursera_quizzes <- function(input_quiz_dir = "quizzes", #' Create TOC-less course website for use in Coursera or Leanpub #' #' Create a version of the course that does not have a TOC and has quizzes in the Coursera yaml format. +#' This is only needed to be used on Bookdown courses. Quarto has a simple command for this. #' #' @param output_dir A folder (existing or not) that the TOC-less Bookdown for Coursera files should be saved. By default is file.path("docs", "coursera") #' @param output_yaml A output.yml file to be provided to bookdown. By default is "_output.yml" @@ -220,7 +221,7 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), input_quiz_dir = "quizzes", output_quiz_dir = "coursera_quizzes", verbose = TRUE) { - # Find root directory by finding `_bookdown.yml` file + # Find root directory by finding `.git` folder root_dir <- course_path() # Output files: diff --git a/R/iframe_leanpub.R b/R/iframe_leanpub.R index 0040c62..8f7c981 100644 --- a/R/iframe_leanpub.R +++ b/R/iframe_leanpub.R @@ -1,6 +1,6 @@ #' Convert Website Course to Leanpub #' -#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or +#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` or `_quarto.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. @@ -149,7 +149,7 @@ website_to_embed_leanpub <- function(path = ".", #' Create Book.txt file from files existing in quiz directory #' -#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or +#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` or `_quarto.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 @@ -172,7 +172,7 @@ course_to_book_txt <- function(path = ".", rmd_regex <- "[.][R|r]md$" # Extract the names of the Rmd files (the chapters) - md_files <- bookdown_rmd_files(path = path) + md_files <- qrqrmd_files(path = path) } if (!is.null(quiz_dir)) { diff --git a/R/set_up.R b/R/set_up.R index 755cdf6..0aa6843 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -1,14 +1,15 @@ -#' Load in Bookdown specifications from _bookdown.yml +#' Load in yaml specifications from _bookdown.yml or _quarto.yml #' -#' @param path Where to look for the _bookdown.yml file. By default looks in current directory +#' @param path Where to look for the yaml spec file. By default looks in current directory. #' #' @return The yaml contents using yaml::yaml.load_file() #' @export -get_bookdown_spec <- function(path = ".") { +get_yaml_spec <- function(path = ".") { root_dir <- course_path(path = file.path(path)) - file_path <- file.path(root_dir, "_bookdown.yml") + + file_path <- list.files(pattern = "_bookdown.yml|_quarto.yml", full.names = TRUE) # Read in yaml suppressWarnings({ @@ -22,59 +23,73 @@ get_bookdown_spec <- function(path = ".") { #' #' @param path Where to look for the file. By default looks in current directory. #' -#' @return Returns the directory where the _bookdown.yml is contained. +#' @return Returns the directory where the .git folder is contained. #' @export course_path <- function(path = ".") { # See what unzip is being used operating_system <- Sys.info()[1] - path <- rprojroot::find_root(rprojroot::has_file(".git"), path = file.path(path)) + path <- rprojroot::find_root(rprojroot::has_dir(".git"), path = file.path(path)) return(path) } -#' Get file paths all Rmds in the bookdown directory +#' Get file paths to all qmds or rmds in the course website directory #' -#' @param path Where to look for the _bookdown.yml file. Passes toget_bookdown_spec() function. By default looks in current directory +#' @param path Where to look for the _bookdown.yml or _quarto.yml file. Passes to get_yaml_spec() function. By default looks in current directory #' -#' @return The file paths to Rmds listed in the _bookdown.yml file. +#' @return The file paths to rmds or wmds listed in the _bookdown.yml or _quarto.yml file. #' @export #' -bookdown_rmd_files <- function(path = ".") { - spec <- get_bookdown_spec(file.path(path)) +qrmd_files <- function(path = ".") { + spec <- get_yaml_spec(file.path(path)) + + rmd_files <- spec$rmd_files + qmd_files <- grep(".qmd", unlist(spec$book$chapters), value = TRUE) + + if (length(rmd_files) > 0 && length(qmd_files) > 0) stop("Both qmd and rmd files are found. Not sure what format to expect") - files <- spec$rmd_files - if (is.null(files) || all(is.na(files)) || length(files) == 0) { + # Make files whichever ones exist here + if (length(rmd_files) > 0) files <- rmd_files + if (length(qmd_files) > 0) files <- qmd_files + + if (length(rmd_files) == 0 && length(qmd_files) == 0) { warning( - "No bookdown specification of the files, using ", - "list.files(pattern ='.Rmd')" + "No rmd or qmd files found specified in the _quarto.yml or _bookdown.yml file. Going to try to find files in the repo based on suffix" ) root_dir <- course_path(path = file.path(path)) files <- list.files( - pattern = "[.]Rmd", ignore.case = TRUE, + pattern = "[.]Rmd$|[.]qmd$", ignore.case = TRUE, path = root_dir, full.names = FALSE ) } + + # If we don't find files STOP + if (length(files) == 0) stop("No rmd/qmd files found in the repo") + 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 +#' @param path Where to look for the _bookdown.yml or _quarto.yml file. Passes to get_yaml_spec() function. By default looks in current directory #' -#' @return The file paths to Rmds listed in the _bookdown.yml file. +#' @return The file paths to rmds or qmds listed in the _bookdown.yml or _quarto.ymlfile. #' @export #' -bookdown_destination <- function(path = ".") { - # Find _bookdown.yml +output_destination <- function(path = ".") { + # Find .git folder which indicates the top of the repo root_dir <- course_path(path = file.path(path)) - # Get specs from _bookdown.yml - spec <- get_bookdown_spec(path = file.path(path)) + # Get specs from _bookdown.yml or _quarto.yml + spec <- get_yaml_spec(path = file.path(path)) # Find output directory declared in the bookdown.yml output_dir <- spec$output_dir + # If that didn't work we're working with quarto so look for that + if (is.null(output_dir)) output_dir <- spec$project$`output-dir` + # If none specified, assume its called docs/ if (is.null(output_dir)) { output_dir <- "docs" @@ -115,7 +130,7 @@ copy_resources <- function(path = ".", } copy_docs <- function(path = ".", output_dir = "manuscript") { - path <- bookdown_destination(path) + path <- output_destination (path) R.utils::copyDirectory(path, file.path(output_dir), recursive = TRUE, overwrite = TRUE) } @@ -148,12 +163,12 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' Set up Manuscript folder for Leanpub publishing #' -#' @param path path to the bookdown book, must have a `_bookdown.yml` file +#' @param path path to the top of course repository (looks for .git folder) #' @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 render If NULL will not be run. If "quarto" or "bookdown" then the respective render type will be run #' @param verbose print diagnostic messages #' @param remove_resources_start remove the word `resources/` at the front #' of any image path. @@ -171,7 +186,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' set_up_leanpub <- function(path = ".", clean_up = FALSE, - render = TRUE, + render = NULL, output_dir = "manuscript", make_book_txt = FALSE, quiz_dir = "quizzes", @@ -180,6 +195,13 @@ set_up_leanpub <- function(path = ".", verbose = TRUE, footer_text = NULL, embed = NULL) { + + + # Check that render is something we can use + if (!is.null(render)) { + if (!render %in% c("quarto", "bookdown")) stop("`render` argument invalid. ") + } + if (clean_up) { message(paste("Clearing out old version of output files:", output_dir)) @@ -191,25 +213,25 @@ set_up_leanpub <- function(path = ".", dir.create(output_dir, recursive = TRUE, showWarnings = FALSE) } - if (!embed && render) { + if (!embed && !is.null(render)) { # Declare regex for finding rmd files - rmd_regex <- "[.][R|r]md$" + rmd_regex <- "[.][q|R|r]md$" - # Get the path to the _bookdown.yml + # Get the path to the _bookdown.yml or _quarto.yml path <- course_path(path) if (verbose) { - message(paste0("Looking for bookdown file in ", path)) + message(paste0("Looking for bookdown or quarto md files in ", path)) } - rmd_files <- bookdown_rmd_files(path = path) + md_files <- qrmd_files(path = path) if (verbose) { message(paste0(c("Processing these files: ", rmd_files), collapse = "\n")) } - if (render) { + if (render == "bookdown") { if (verbose) { - message("Rendering the Book") + message("Rendering bookdown Book") } # Get the index file path index_file <- grep("index", rmd_files, ignore.case = TRUE, value = TRUE) @@ -217,7 +239,7 @@ set_up_leanpub <- function(path = ".", index_file <- normalizePath(index_file) if (length(index_file) == 0 || is.na(index_file)) { - index_file <- rmd_files[1] + index_file <- md_files[1] } message(paste("index_file is", index_file)) @@ -234,6 +256,23 @@ set_up_leanpub <- function(path = ".", clean_envir = FALSE ) } + if (render == "quarto") { + if (verbose) { + message("Rendering Quarto 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 <- md_files[1] + } + message(paste("index_file is", index_file)) + + quarto::quarto_render('.') + } + } # We only need to copy these things if we are not doing embed if (!embed) {