From 090394d5ed03c1beab8ac66e9cfea7197128cacf Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Wed, 26 Jun 2024 16:02:27 -0400 Subject: [PATCH 01/17] reorg --- NAMESPACE | 14 +- R/coursera_and_leanpub.R | 4 +- R/data.R | 26 +++ R/example_data.R | 89 -------- R/{bookdown_to_leanpub.R => iframe_leanpub.R} | 17 +- R/leanpub_checks.R | 206 ------------------ R/remove_yaml.R | 51 ----- R/set_up.R | 39 ++-- R/simple_references.R | 80 ------- R/utils.R | 32 --- man/bad_quiz_path.Rd | 2 +- man/bookdown_file.Rd | 17 -- man/{bookdown_path.Rd => course_path.Rd} | 10 +- ...n_to_book_txt.Rd => course_to_book_txt.Rd} | 10 +- man/coursera.Rd | 4 +- man/example_repo_cleanup.Rd | 29 --- man/example_repo_setup.Rd | 24 -- man/get_bookdown_spec.Rd | 2 +- man/get_chapters.Rd | 6 +- man/good_quiz_path.Rd | 19 -- man/leanpub_check.Rd | 19 -- man/make_embed_markdown.Rd | 2 +- man/remove_yaml_header.Rd | 22 -- man/set_up_leanpub.Rd | 2 +- man/simple_references.Rd | 23 -- ...leanpub.Rd => website_to_embed_leanpub.Rd} | 19 +- 26 files changed, 77 insertions(+), 691 deletions(-) delete mode 100644 R/example_data.R rename R/{bookdown_to_leanpub.R => iframe_leanpub.R} (95%) delete mode 100644 R/leanpub_checks.R delete mode 100644 R/remove_yaml.R delete mode 100644 R/simple_references.R delete mode 100644 man/bookdown_file.Rd rename man/{bookdown_path.Rd => course_path.Rd} (67%) rename man/{bookdown_to_book_txt.Rd => course_to_book_txt.Rd} (76%) delete mode 100644 man/example_repo_cleanup.Rd delete mode 100644 man/example_repo_setup.Rd delete mode 100644 man/good_quiz_path.Rd delete mode 100644 man/leanpub_check.Rd delete mode 100644 man/remove_yaml_header.Rd delete mode 100644 man/simple_references.Rd rename man/{bookdown_to_embed_leanpub.Rd => website_to_embed_leanpub.Rd} (82%) diff --git a/NAMESPACE b/NAMESPACE index 6d54d6f..e63a04a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,11 +5,7 @@ export(auth_from_secret) export(authorize) export(bad_quiz_path) export(bookdown_destination) -export(bookdown_file) -export(bookdown_path) export(bookdown_rmd_files) -export(bookdown_to_book_txt) -export(bookdown_to_embed_leanpub) export(check_all_questions) export(check_question) export(check_quiz) @@ -19,9 +15,9 @@ export(check_quizzes) export(convert_coursera_quizzes) export(convert_quiz) export(convert_utube_link) +export(course_path) +export(course_to_book_txt) export(download_ottr_template) -export(example_repo_cleanup) -export(example_repo_setup) export(extract_meta) export(extract_object_id) export(extract_quiz) @@ -33,12 +29,10 @@ export(get_image_link_from_slide) export(get_object_id_notes) export(get_slide_id) export(get_slide_page) -export(good_quiz_path) export(gs_id_from_slide) 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) @@ -46,16 +40,14 @@ export(parse_quiz_df) export(pptx_notes) export(pptx_slide_note_df) export(pptx_slide_text_df) -export(remove_yaml_header) export(render_without_toc) export(replace_html) export(replace_single_html) export(set_knitr_image_path) export(set_up_leanpub) -export(simple_references) export(unzip_pptx) +export(website_to_embed_leanpub) export(xml_notes) -importFrom(fs,dir_copy) importFrom(httr,GET) importFrom(httr,accept_json) importFrom(httr,config) diff --git a/R/coursera_and_leanpub.R b/R/coursera_and_leanpub.R index df478e8..32afae0 100644 --- a/R/coursera_and_leanpub.R +++ b/R/coursera_and_leanpub.R @@ -197,9 +197,9 @@ convert_coursera_quizzes <- function(input_quiz_dir = "quizzes", ) } -#' Create TOC-less Bookdown for use in Coursera +#' Create TOC-less course website for use in Coursera or Leanpub #' -#' Create a version of Leanpub that does not have a TOC and has quizzes in the Coursera yaml format. Requires Bookdown output files including "assets", "resources", and "libs". +#' Create a version of the course that does not have a TOC and has quizzes in the Coursera yaml format. #' #' @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" diff --git a/R/data.R b/R/data.R index 09b36e8..29e2342 100644 --- a/R/data.R +++ b/R/data.R @@ -73,3 +73,29 @@ encrypt_creds_user_path <- function() { full.names = TRUE ) } + +good_quiz_path <- function() { + list.files( + pattern = "quiz_good.md$", + recursive = TRUE, + system.file("extdata", package = "ottrpal"), + full.names = TRUE + ) +} + +#' Path to bad example quiz +#' +#' @export +#' @return The file path to an example bad quiz included in the package that will fail the quiz checks. +#' +#' @examples +#' +#' quiz_path <- bad_quiz_path() +bad_quiz_path <- function() { + list.files( + pattern = "quiz_bad.md$", + recursive = TRUE, + system.file("extdata", package = "ottrpal"), + full.names = TRUE + ) +} diff --git a/R/example_data.R b/R/example_data.R deleted file mode 100644 index 1af693f..0000000 --- a/R/example_data.R +++ /dev/null @@ -1,89 +0,0 @@ -# C. Savonen 2021 - -#' Path to good example quiz -#' -#' @export -#' @return The file path to an example good quiz included in the package that will pass the quiz checks. -#' @examples -#' -#' quiz_path <- good_quiz_path() -#' -good_quiz_path <- function() { - list.files( - pattern = "quiz_good.md$", - recursive = TRUE, - system.file("extdata", package = "ottrpal"), - full.names = TRUE - ) -} - -#' Path to bad example quiz -#' -#' @export -#' @return The file path to an example bad quiz included in the package that will fail the quiz checks. -#' -#' @examples -#' -#' quiz_path <- bad_quiz_path() -bad_quiz_path <- function() { - list.files( - pattern = "quiz_bad.md$", - recursive = TRUE, - system.file("extdata", package = "ottrpal"), - full.names = TRUE - ) -} - -#' Set up example repo files -#' -#' @param dest_dir The destination directory you would like the example repo files to be placed. By default is current directory. -#' @return Sets up example files that can be used to test 'ottrpal' functions. -#' -#' @export -#' -#' @importFrom fs dir_copy -#' @examples \dontrun{ -#' -#' # Run this to get the files we need -#' example_files <- ottrpal::example_repo_setup() -#' } -example_repo_setup <- function(dest_dir = tempdir()) { - bookdown_path <- list.files( - pattern = "_bookdown.yml$", - system.file("extdata/", package = "ottrpal"), - full.names = TRUE - ) - - # Copy over whole directory - fs::dir_copy(dirname(bookdown_path), dest_dir, overwrite = TRUE) - - copied_files <- list.files(dirname(bookdown_path), full.names = TRUE) - - return(copied_files) -} - -#' Clean up example repo files -#' -#' @param files_to_remove List of example files to delete. -#' @param verbose TRUE/FALSE would you like progress messages? -#' @return Will delete example files copied from [ottrpal::example_repo_setup()] function -#' @export -#' -#' @examples \dontrun{ -#' -#' # Run this to get the files we need -#' example_files <- ottrpal::example_repo_setup() -#' -#' # Run this to delete them -#' example_repo_cleanup(files_to_remove = basename(example_files)) -#' } -example_repo_cleanup <- function(files_to_remove, verbose = FALSE) { - message("Cleaning up and removing example repo files") - - lapply(files_to_remove, function(file2remove, verbose = verbose) { - if (file.exists(file2remove)) { - message(paste0("Removing: ", file2remove)) - file.remove(file2remove) - } - }) -} diff --git a/R/bookdown_to_leanpub.R b/R/iframe_leanpub.R similarity index 95% rename from R/bookdown_to_leanpub.R rename to R/iframe_leanpub.R index 4ae7a95..a5215fb 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/iframe_leanpub.R @@ -1,12 +1,11 @@ -#' Convert Bookdown to Leanpub +#' Convert Website Course to Leanpub #' -#' @param path path to the bookdown book, must have a `_bookdown.yml` file +#' @param path path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or #' @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 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 #' @param default_img A google slide link to the default image to be used for all chapters @@ -32,15 +31,14 @@ #' #' @examples \dontrun{ #' -#' ottrpal::bookdown_to_embed_leanpub( +#' ottrpal::website_to_embed_leanpub( #' base_url = "https://jhudatascience.org/OTTR_Template/", #' make_book_txt = TRUE, #' quiz_dir = NULL #' ) #' } -bookdown_to_embed_leanpub <- function(path = ".", +website_to_embed_leanpub <- function(path = ".", chapt_img_key = NULL, - bookdown_index = file.path(base_url, "index.html"), base_url = NULL, clean_up = FALSE, default_img = NULL, @@ -148,7 +146,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 path path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or #' @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 @@ -158,7 +156,7 @@ bookdown_to_embed_leanpub <- function(path = ".", #' @return A list of quiz and chapter files in order in a file called Book.txt -- How Leanpub wants it. #' @export #' -bookdown_to_book_txt <- function(path = ".", +course_to_book_txt <- function(path = ".", md_files = NULL, output_dir = "manuscript", quiz_dir = "quizzes", @@ -166,7 +164,7 @@ bookdown_to_book_txt <- function(path = ".", # If md_files are not specified, then try to get them if (is.null(md_files)) { # Establish path - path <- bookdown_path(path) + path <- course_path(path) rmd_regex <- "[.][R|r]md$" @@ -293,7 +291,6 @@ make_embed_markdown <- function(url, #' 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 #' diff --git a/R/leanpub_checks.R b/R/leanpub_checks.R deleted file mode 100644 index 6f32e1a..0000000 --- a/R/leanpub_checks.R +++ /dev/null @@ -1,206 +0,0 @@ -#' Check Leanpub Course or Book -#' -#' @param path path to the Leanpub book/course -#' @param verbose print diagnostic messages -#' -#' @return A list of output files and diagnostics -#' @export -#' -leanpub_check <- function(path = ".", - verbose = TRUE) { - if (verbose) { - message("Checking the Book.txt files") - } - check_book_txt(path = path) - extra_book_result <- check_extra_md_files(path = path) - - if (verbose) { - message("Checking if HTML is present") - } - html_result <- full_html_check(path = path) - attribute_result <- full_attribute_check(path = path) - - files <- get_md_files(path) - - L <- list( - extra_book_result = extra_book_result, - html_result = html_result, - attribute_result = attribute_result - ) - return(L) -} - -check_book_txt <- function(path = ".") { - file <- file.path(path, "Book.txt") - # if Book.txt not there, fail - stopifnot(file.exists(file)) - x <- readLines(file, warn = FALSE) - files <- file.path(path, x) - fe <- file.exists(files) - if (!all(fe)) { - msg <- paste(x[fe], collapse = ", ") - msg <- paste0("Book.txt has files specified, but missing: ", msg) - stop(msg) - } - return(NULL) -} -get_md_files <- function(path) { - files <- list.files(pattern = ".md$", ignore.case = TRUE, path = path) - files -} - -check_extra_md_files <- function(path = ".") { - files <- get_md_files(path) - bn <- basename(files) - file <- file.path(path, "Book.txt") - # if Book.txt not there, fail - stopifnot(file.exists(file)) - x <- readLines(file, warn = FALSE) - sd <- setdiff(files, x) - if (length(sd) > 0) { - sd <- paste(sd, collapse = ", ") - warning( - "Markdown files exist in path but are not in Book.txt, may be left out ", - sd - ) - return(FALSE) - } - return(FALSE) -} - -full_html_check <- function(path = ".") { - files <- get_md_files(path) - out <- lapply(files, check_html) - names(out) <- basename(files) - html_result <- any(sapply(out, function(x) x$result)) - if (any(html_result)) { - tag_list <- mapply(function(x, name) { - if (!x$result) { - return(NULL) - } - paste0("file ", name, ": ", paste(x$tags_found, collapse = ", ")) - }, SIMPLIFY = FALSE) - tag_list <- c(unlist(tag_list)) - tag_list <- paste(tag_list, collapse = "\n\n") - message(tag_list) - warning(tag_list) - return(FALSE) - } - return(TRUE) -} - -check_html <- function(x) { - if (length(x) == 1 && file.exists(x)) { - x <- readLines(x, warn = FALSE) - } - # taken from dput(names(shiny::tags)) - tags_to_check <- c( - "a", "abbr", "address", "animate", "animateMotion", "animateTransform", - "area", "article", "aside", "audio", "b", "base", "bdi", "bdo", - "blockquote", "body", "br", "button", "canvas", "caption", "circle", - "cite", "clipPath", "code", "col", "colgroup", "color-profile", - "command", "data", "datalist", "dd", "defs", "del", "desc", "details", - "dfn", "dialog", "discard", "div", "dl", "dt", "ellipse", "em", - "embed", "eventsource", "feBlend", "feColorMatrix", "feComponentTransfer", - "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", - "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", - "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", - "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", - "feSpotLight", "feTile", "feTurbulence", "fieldset", "figcaption", - "figure", "filter", "footer", "foreignObject", "form", "g", "h1", - "h2", "h3", "h4", "h5", "h6", "hatch", "hatchpath", "head", "header", - "hgroup", "hr", "html", "i", "iframe", "image", "img", "input", - "ins", "kbd", "keygen", "label", "legend", "li", "line", "linearGradient", - "link", "main", "map", "mark", "marker", "mask", "menu", "meta", - "metadata", "meter", "mpath", "nav", "noscript", "object", "ol", - "optgroup", "option", "output", "p", "param", "path", "pattern", - "picture", "polygon", "polyline", "pre", "progress", "q", "radialGradient", - "rb", "rect", "rp", "rt", "rtc", "ruby", "s", "samp", "script", - "section", "select", "set", "slot", "small", "solidcolor", "source", - "span", "stop", "strong", "style", "sub", "summary", "sup", "svg", - "switch", "symbol", "table", "tbody", "td", "template", "text", - "textarea", "textPath", "tfoot", "th", "thead", "time", "title", - "tr", "track", "tspan", "u", "ul", "use", "var", "video", "view", - "wbr" - ) - agrepl <- function(...) { - any(grepl(...)) - } - start_strings <- paste("<\\s*", tags_to_check, ".*>") - start_res <- sapply(start_strings, function(pattern) { - agrepl(x = x, pattern = pattern) - }) - end_strings <- paste("<\\s*/", tags_to_check, ".*>") - end_res <- sapply(start_strings, function(pattern) { - agrepl(x = x, pattern = pattern) - }) - res <- start_res | end_res - tag_df <- tibble::tibble( - tag = tags_to_check, - start_present = start_res, - end_present = end_res, - present = res - ) - L <- list( - result = any(res), - tag_data = tag_df - ) - if (any(res)) { - L$tags_found <- tag_df$tag[tag_df$present] - } - L -} - -is_html_present <- function(x) { - out <- check_html(x) - out$result -} - - - -make_sure_attributes_above <- function(x) { - if (length(x) == 1 && file.exists(x)) { - x <- readLines(x, warn = FALSE) - } - index <- lag_attribute <- lag_trimmed <- trimmed <- link <- link_index <- NULL - rm(list = c( - "link_index", "link", "trimmed", "lag_trimmed", - "lag_attribute", "index" - )) - df <- tibble::tibble( - x = x, - index = 1:length(x), - trimmed = trimws(x) - ) %>% - dplyr::mutate( - link = grepl(trimmed, pattern = "^!\\s*\\[.*\\]\\s*\\("), - attributes = grepl(pattern = "^\\{", trimmed), - lag_trimmed = dplyr::lag(trimmed, n = 1), - lag_attribute = grepl(pattern = "^\\{", lag_trimmed) - ) - if (!any(df$link)) { - return(TRUE) - } - bad <- df %>% - dplyr::filter(link & !lag_attribute) - if (nrow(bad) > 0) { - bad_lines <- df %>% - dplyr::filter(index %in% c(bad$index, bad$index - 1)) %>% - dplyr::select(x, index) %>% - as.data.frame() - warning("Links without attributes in the last") - print(bad_lines) - return(FALSE) - } - return(TRUE) -} - -# See if any filenames are duplicated in resources/ and resources/images/ -# just PNGs -# list.files() -# if image specified, see if image actually exists *somewhere* - -full_attribute_check <- function(path = ".") { - files <- get_md_files(path) - out <- lapply(files, make_sure_attributes_above) -} diff --git a/R/remove_yaml.R b/R/remove_yaml.R deleted file mode 100644 index e98dde9..0000000 --- a/R/remove_yaml.R +++ /dev/null @@ -1,51 +0,0 @@ -partition_yaml_front_matter <- function(input_lines) { - # taken from rmarkdown package - validate_front_matter <- function(delimiters) { - if (length(delimiters) >= 2 && (delimiters[2] - delimiters[1] > - 1) && grepl("^---\\s*$", input_lines[delimiters[1]])) { - if (delimiters[1] == 1) { - TRUE - } else { - all(grepl( - "^\\s*()?\\s*$", - input_lines[1:delimiters[1] - 1] - )) - } - } else { - FALSE - } - } - delimiters <- grep("^(---|\\.\\.\\.)\\s*$", input_lines) - if (validate_front_matter(delimiters)) { - front_matter <- input_lines[(delimiters[1]):(delimiters[2])] - input_body <- c() - if (delimiters[1] > 1) { - input_body <- c(input_body, input_lines[1:delimiters[1] - - 1]) - } - if (delimiters[2] < length(input_lines)) { - input_body <- c(input_body, input_lines[-(1:delimiters[2])]) - } - list(front_matter = front_matter, body = input_body) - } else { - list(front_matter = NULL, body = input_lines) - } -} - -#' Remove YAML header -#' -#' @param file file name of the markdown file -#' -#' @return A character vector of the text without the YAML header -#' @export -#' -#' @examples -#' file <- system.file("extdata/00_template.Rmd", package = "ottrpal") -#' out <- remove_yaml_header(file) -#' head(out) -remove_yaml_header <- function(file) { - file <- readLines(file, warn = FALSE) - out <- partition_yaml_front_matter(file) - out <- out$body - return(out) -} diff --git a/R/set_up.R b/R/set_up.R index d0fe7bf..78d5d32 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -1,13 +1,14 @@ #' 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 +#' @param path Where to look for the _bookdown.yml file. 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) + + root_dir <- course_path(path = file.path(path)) + file_path <- file.path(root_dir, "_bookdown.yml") # Read in yaml suppressWarnings({ @@ -17,35 +18,21 @@ get_bookdown_spec <- function(path = ".") { return(yaml_contents) } -#' Find main Bookdown directory +#' Find main course git 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 = ".") { +course_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)) + path <- rprojroot::find_root(rprojroot::has_file(".git"), 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 @@ -62,7 +49,7 @@ bookdown_rmd_files <- function(path = ".") { "No bookdown specification of the files, using ", "list.files(pattern ='.Rmd')" ) - root_dir <- bookdown_path(path = file.path(path)) + root_dir <- course_path(path = file.path(path)) files <- list.files( pattern = "[.]Rmd", ignore.case = TRUE, path = root_dir, full.names = FALSE @@ -80,7 +67,7 @@ bookdown_rmd_files <- function(path = ".") { #' bookdown_destination <- function(path = ".") { # Find _bookdown.yml - root_dir <- bookdown_path(path = file.path(path)) + root_dir <- course_path(path = file.path(path)) # Get specs from _bookdown.yml spec <- get_bookdown_spec(path = file.path(path)) @@ -108,7 +95,7 @@ copy_resources <- function(path = ".", images_dir = "resources", output_dir = "manuscript") { # Get file path to bookdown.yml - path <- bookdown_path(path) + path <- course_path(path) # Assume image directory is `resources/images` res_image_dir <- file.path(path, images_dir) @@ -133,7 +120,7 @@ copy_docs <- function(path = ".", output_dir = "manuscript") { } copy_bib <- function(path = ".", output_dir = "manuscript") { - path <- bookdown_path(path) + path <- course_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) @@ -178,7 +165,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 +#' @param embed is this being run by website_to_embed_leanpub? TRUE/FALSE #' @return A list of output files and diagnostics #' @export #' @@ -209,7 +196,7 @@ set_up_leanpub <- function(path = ".", rmd_regex <- "[.][R|r]md$" # Get the path to the _bookdown.yml - path <- bookdown_path(path) + path <- course_path(path) if (verbose) { message(paste0("Looking for bookdown file in ", path)) diff --git a/R/simple_references.R b/R/simple_references.R deleted file mode 100644 index c4e3efa..0000000 --- a/R/simple_references.R +++ /dev/null @@ -1,80 +0,0 @@ -#' Run Simple References -#' -#' @param x A filename of a markdown or Rmarkdown file, or the full output -#' from `readLines` on that file -#' @param bib_files bibliography files to use for pandoc -#' @param add_reference_header Should the `## References` header be added at the -#' end of the output? -#' -#' @return A character vector of the file, with references subbed in -#' @export -simple_references <- function(x, bib_files, add_reference_header = FALSE) { - if (length(x) == 1 && file.exists(x)) { - x <- readLines(x, warn = FALSE) - } - # indices = grepl("\\[\\s*[@]", x) - indices <- grepl("@", x) - # [@bookdown2016 #bookdown2016] - all_refs <- x[indices] - # out = x - bad_string <- "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" - result <- lapply(all_refs, function(out) { - stopifnot(!any(grepl(bad_string, out))) - out <- c(out, "", bad_string) - tfile <- tempfile(fileext = ".md") - writeLines(out, tfile, sep = "\n") - pandoc_args <- c( - paste0("--bibliography=", path.expand(normalizePath(bib_files))), - "--wrap=none" - ) - o2 <- tempfile(fileext = ".md") - rmarkdown::pandoc_convert( - input = tfile, - to = "markdown_strict", - output = o2, - citeproc = TRUE, - options = pandoc_args - ) - o2_text <- readLines(o2) - }) - all_references <- lapply(result, function(o2_text) { - references <- grep(bad_string, o2_text) - stopifnot(length(references) == 1) - if (references == length(o2_text)) { - return(NULL) - } - references <- o2_text[(references + 1):length(o2_text)] - references <- references[!references %in% ""] - references - }) - # dangerous - final_refs <- unique(unlist(unique(all_references))) - if (length(final_refs) > 0 && all(nchar(final_refs) > 0)) { - final_refs <- paste0(final_refs, "\n\n") - } - - all_output <- lapply(result, function(o2_text) { - references <- grep(bad_string, o2_text) - stopifnot(length(references) == 1) - stopifnot(o2_text[references - 1] == "") - out <- o2_text[-((references - 1):length(o2_text))] - }) - all_output <- mapply(function(x, y, z) { - if (is.null(z)) { - out <- x - } else { - out <- y - } - paste(out, collapse = "\n") - }, all_refs, all_output, all_references, SIMPLIFY = TRUE) - all_output <- c(unlist(all_output)) - out_x <- x - out_x[indices] <- all_output - - out_x <- c( - out_x, "", - if (add_reference_header && length(final_refs) > 0) "## References", - final_refs - ) - out_x -} diff --git a/R/utils.R b/R/utils.R index 701902d..c590607 100644 --- a/R/utils.R +++ b/R/utils.R @@ -9,38 +9,6 @@ utils::globalVariables(c( )) -# get script path and number of paragraphs -paragraph_from_script <- function(x) { - if (file.exists(x)) { - para <- readLines(x, warn = FALSE) - para <- trimws(para) - para <- para[!para %in% c("", " ")] - return(para) - } else { - return(NA) - } -} - -n_para <- function(x) { - x <- paragraph_from_script(x) - if (length(x) == 0) { - return(0) - } - ifelse(all(is.na(x)), NA, length(x)) -} - -length0 <- function(x) { - length(x) == 0 -} - -length0_to_NA <- function(x) { - if (length0(x)) { - x <- NA - } - x -} - - #' Google Slides Helper Functions #' #' @param file markdown file for manuscript diff --git a/man/bad_quiz_path.Rd b/man/bad_quiz_path.Rd index 1dd4bd3..2655347 100644 --- a/man/bad_quiz_path.Rd +++ b/man/bad_quiz_path.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/example_data.R +% Please edit documentation in R/data.R \name{bad_quiz_path} \alias{bad_quiz_path} \title{Path to bad example quiz} diff --git a/man/bookdown_file.Rd b/man/bookdown_file.Rd deleted file mode 100644 index 78d133f..0000000 --- a/man/bookdown_file.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/set_up.R -\name{bookdown_file} -\alias{bookdown_file} -\title{Find file path to _bookdown.yml} -\usage{ -bookdown_file(path = ".") -} -\arguments{ -\item{path}{Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory} -} -\value{ -The file path to _bookdown.yml -} -\description{ -Find file path to _bookdown.yml -} diff --git a/man/bookdown_path.Rd b/man/course_path.Rd similarity index 67% rename from man/bookdown_path.Rd rename to man/course_path.Rd index 27a0817..b26792d 100644 --- a/man/bookdown_path.Rd +++ b/man/course_path.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/set_up.R -\name{bookdown_path} -\alias{bookdown_path} -\title{Find main Bookdown directory} +\name{course_path} +\alias{course_path} +\title{Find main course git directory} \usage{ -bookdown_path(path = ".") +course_path(path = ".") } \arguments{ \item{path}{Where to look for the file. By default looks in current directory.} @@ -13,5 +13,5 @@ bookdown_path(path = ".") Returns the directory where the _bookdown.yml is contained. } \description{ -Find main Bookdown directory +Find main course git directory } diff --git a/man/bookdown_to_book_txt.Rd b/man/course_to_book_txt.Rd similarity index 76% rename from man/bookdown_to_book_txt.Rd rename to man/course_to_book_txt.Rd index a0b46cd..2b963f0 100644 --- a/man/bookdown_to_book_txt.Rd +++ b/man/course_to_book_txt.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R -\name{bookdown_to_book_txt} -\alias{bookdown_to_book_txt} +% Please edit documentation in R/iframe_leanpub.R +\name{course_to_book_txt} +\alias{course_to_book_txt} \title{Create Book.txt file from files existing in quiz directory} \usage{ -bookdown_to_book_txt( +course_to_book_txt( path = ".", md_files = NULL, output_dir = "manuscript", @@ -13,7 +13,7 @@ bookdown_to_book_txt( ) } \arguments{ -\item{path}{path to the bookdown book, must have a `_bookdown.yml` file} +\item{path}{path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or} \item{md_files}{vector of file path of the md's to be included} diff --git a/man/coursera.Rd b/man/coursera.Rd index 4b45007..ff7e610 100644 --- a/man/coursera.Rd +++ b/man/coursera.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/coursera_and_leanpub.R \name{render_without_toc} \alias{render_without_toc} -\title{Create TOC-less Bookdown for use in Coursera} +\title{Create TOC-less course website for use in Coursera or Leanpub} \usage{ render_without_toc( output_dir = file.path("docs", "no_toc"), @@ -30,5 +30,5 @@ render_without_toc( A folder of coursera ready quiz files and html chapter files saved to output directories specified. } \description{ -Create a version of Leanpub that does not have a TOC and has quizzes in the Coursera yaml format. Requires Bookdown output files including "assets", "resources", and "libs". +Create a version of the course that does not have a TOC and has quizzes in the Coursera yaml format. } diff --git a/man/example_repo_cleanup.Rd b/man/example_repo_cleanup.Rd deleted file mode 100644 index b896198..0000000 --- a/man/example_repo_cleanup.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/example_data.R -\name{example_repo_cleanup} -\alias{example_repo_cleanup} -\title{Clean up example repo files} -\usage{ -example_repo_cleanup(files_to_remove, verbose = FALSE) -} -\arguments{ -\item{files_to_remove}{List of example files to delete.} - -\item{verbose}{TRUE/FALSE would you like progress messages?} -} -\value{ -Will delete example files copied from [ottrpal::example_repo_setup()] function -} -\description{ -Clean up example repo files -} -\examples{ -\dontrun{ - -# Run this to get the files we need -example_files <- ottrpal::example_repo_setup() - -# Run this to delete them -example_repo_cleanup(files_to_remove = basename(example_files)) -} -} diff --git a/man/example_repo_setup.Rd b/man/example_repo_setup.Rd deleted file mode 100644 index aa64399..0000000 --- a/man/example_repo_setup.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/example_data.R -\name{example_repo_setup} -\alias{example_repo_setup} -\title{Set up example repo files} -\usage{ -example_repo_setup(dest_dir = tempdir()) -} -\arguments{ -\item{dest_dir}{The destination directory you would like the example repo files to be placed. By default is current directory.} -} -\value{ -Sets up example files that can be used to test 'ottrpal' functions. -} -\description{ -Set up example repo files -} -\examples{ -\dontrun{ - -# Run this to get the files we need -example_files <- ottrpal::example_repo_setup() -} -} diff --git a/man/get_bookdown_spec.Rd b/man/get_bookdown_spec.Rd index 1bfd9b3..162ce0b 100644 --- a/man/get_bookdown_spec.Rd +++ b/man/get_bookdown_spec.Rd @@ -7,7 +7,7 @@ get_bookdown_spec(path = ".") } \arguments{ -\item{path}{Where to look for the _bookdown.yml file. Passes to the bookdown_file() function. By default looks in current directory} +\item{path}{Where to look for the _bookdown.yml file. By default looks in current directory} } \value{ The yaml contents using yaml::yaml.load_file() diff --git a/man/get_chapters.Rd b/man/get_chapters.Rd index 348fa23..010e0bb 100644 --- a/man/get_chapters.Rd +++ b/man/get_chapters.Rd @@ -1,14 +1,12 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/bookdown_to_leanpub.R +% Please edit documentation in R/iframe_leanpub.R \name{get_chapters} \alias{get_chapters} \title{Make Leanpub file that has embed webpage of a chapter} \usage{ -get_chapters(html_page = file.path("docs", "index.html")) +get_chapters(html_page = file.path("docs", "index.html"), base_url = ".") } \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} } diff --git a/man/good_quiz_path.Rd b/man/good_quiz_path.Rd deleted file mode 100644 index 3e93973..0000000 --- a/man/good_quiz_path.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/example_data.R -\name{good_quiz_path} -\alias{good_quiz_path} -\title{Path to good example quiz} -\usage{ -good_quiz_path() -} -\value{ -The file path to an example good quiz included in the package that will pass the quiz checks. -} -\description{ -Path to good example quiz -} -\examples{ - -quiz_path <- good_quiz_path() - -} diff --git a/man/leanpub_check.Rd b/man/leanpub_check.Rd deleted file mode 100644 index ff27850..0000000 --- a/man/leanpub_check.Rd +++ /dev/null @@ -1,19 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/leanpub_checks.R -\name{leanpub_check} -\alias{leanpub_check} -\title{Check Leanpub Course or Book} -\usage{ -leanpub_check(path = ".", verbose = TRUE) -} -\arguments{ -\item{path}{path to the Leanpub book/course} - -\item{verbose}{print diagnostic messages} -} -\value{ -A list of output files and diagnostics -} -\description{ -Check Leanpub Course or Book -} diff --git a/man/make_embed_markdown.Rd b/man/make_embed_markdown.Rd index ea34f1d..59c2fdd 100644 --- a/man/make_embed_markdown.Rd +++ b/man/make_embed_markdown.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/iframe_leanpub.R \name{make_embed_markdown} \alias{make_embed_markdown} \title{Make Leanpub file that has embed webpage of a chapter} diff --git a/man/remove_yaml_header.Rd b/man/remove_yaml_header.Rd deleted file mode 100644 index b2fcae5..0000000 --- a/man/remove_yaml_header.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/remove_yaml.R -\name{remove_yaml_header} -\alias{remove_yaml_header} -\title{Remove YAML header} -\usage{ -remove_yaml_header(file) -} -\arguments{ -\item{file}{file name of the markdown file} -} -\value{ -A character vector of the text without the YAML header -} -\description{ -Remove YAML header -} -\examples{ -file <- system.file("extdata/00_template.Rmd", package = "ottrpal") -out <- remove_yaml_header(file) -head(out) -} diff --git a/man/set_up_leanpub.Rd b/man/set_up_leanpub.Rd index e65eec2..e506ba0 100644 --- a/man/set_up_leanpub.Rd +++ b/man/set_up_leanpub.Rd @@ -46,7 +46,7 @@ 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} +\item{embed}{is this being run by website_to_embed_leanpub? TRUE/FALSE} } \value{ A list of output files and diagnostics diff --git a/man/simple_references.Rd b/man/simple_references.Rd deleted file mode 100644 index 8ecedc9..0000000 --- a/man/simple_references.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/simple_references.R -\name{simple_references} -\alias{simple_references} -\title{Run Simple References} -\usage{ -simple_references(x, bib_files, add_reference_header = FALSE) -} -\arguments{ -\item{x}{A filename of a markdown or Rmarkdown file, or the full output -from `readLines` on that file} - -\item{bib_files}{bibliography files to use for pandoc} - -\item{add_reference_header}{Should the `## References` header be added at the -end of the output?} -} -\value{ -A character vector of the file, with references subbed in -} -\description{ -Run Simple References -} diff --git a/man/bookdown_to_embed_leanpub.Rd b/man/website_to_embed_leanpub.Rd similarity index 82% rename from man/bookdown_to_embed_leanpub.Rd rename to man/website_to_embed_leanpub.Rd index b741b9d..9960b2a 100644 --- a/man/bookdown_to_embed_leanpub.Rd +++ b/man/website_to_embed_leanpub.Rd @@ -1,13 +1,12 @@ % 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 Leanpub} +% Please edit documentation in R/iframe_leanpub.R +\name{website_to_embed_leanpub} +\alias{website_to_embed_leanpub} +\title{Convert Website Course to Leanpub} \usage{ -bookdown_to_embed_leanpub( +website_to_embed_leanpub( path = ".", chapt_img_key = NULL, - bookdown_index = file.path(base_url, "index.html"), base_url = NULL, clean_up = FALSE, default_img = NULL, @@ -22,7 +21,7 @@ bookdown_to_embed_leanpub( ) } \arguments{ -\item{path}{path to the bookdown book, must have a `_bookdown.yml` file} +\item{path}{path to the bookdown or quarto course repository, must have a `_bookdown.yml` file or} \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`). @@ -30,8 +29,6 @@ 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{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} @@ -66,12 +63,12 @@ end of each file before the references section.} A directory of output files in a folder 'manuscript' for publishing on Leanpub. } \description{ -Convert Bookdown to Leanpub +Convert Website Course to Leanpub } \examples{ \dontrun{ -ottrpal::bookdown_to_embed_leanpub( +ottrpal::website_to_embed_leanpub( base_url = "https://jhudatascience.org/OTTR_Template/", make_book_txt = TRUE, quiz_dir = NULL From 8244b8f228bcad2357dc629be667dabe8543ea09 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 1 Jul 2024 09:21:36 -0400 Subject: [PATCH 02/17] Update check_testthat.R --- .github/workflows/R-CMD-check.yaml | 1 - .github/workflows/check_testthat.R | 29 +++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index b542e23..091012f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -39,7 +39,6 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - name: Check testthat - if: runner.os != 'Windows' id: check_check run: | error_num=$(Rscript --vanilla '.github/workflows/check_testthat.R') diff --git a/.github/workflows/check_testthat.R b/.github/workflows/check_testthat.R index 7f33880..831d1d2 100644 --- a/.github/workflows/check_testthat.R +++ b/.github/workflows/check_testthat.R @@ -8,21 +8,38 @@ library(magrittr) # Find .git root directory root_dir <- rprojroot::find_root(rprojroot::has_dir(".git")) +# Read in the testthat results out_file <- list.files(pattern = "testthat.Rout$|Rout.fail$", file.path(root_dir, "check"), recursive = TRUE, full.names = TRUE) -check_content <- readLines(out_file) -test_result <- grep("\\[ FAIL", check_content, value = TRUE)[1] -test_result <- unlist(strsplit(test_result, "\\||\\[|\\]")) +# Extract testhat results +testthat_check_content <- readLines(out_file) +testthat_result <- grep("\\[ FAIL", testthat_check_content, value = TRUE)[1] +testthat_result <- unlist(strsplit(testthat_result, "\\||\\[|\\]")) + +# Read in standard check results +check_content <- readLines(file.path(root_dir, "check", "ottrpal.Rcheck", "00check.log")) + +# Extract standard check results +check_result <- grep("Status\\:", check_content, value = TRUE) +check_result <- unlist(strsplit(check_result, ",")) +check_result <- stringr::str_remove(check_result, "Status:| ") # Format the data into a dataframe -test_result_df <- data.frame(result = trimws(test_result)) %>% +testthat_result_df <- data.frame(result = trimws(testthat_result)) %>% dplyr::filter(result != "") %>% tidyr::separate(result, sep = " ", into = c("test_name", "num")) %>% dplyr::mutate(num = as.numeric(num)) -fail_num <- test_result_df %>% - dplyr::filter(test_name %in% c("FAIL", "WARN")) %>% +# Do the same for the check results +check_result_df <- data.frame(result = trimws(check_result)) %>% + tidyr::separate(result, sep = " ", into = c("num", "test_name")) %>% + dplyr::mutate(num = as.numeric(num)) %>% + dplyr::select("test_name", "num") + +# We only want warnings or errors or fails +fail_num <- dplyr::bind_rows(check_result_df, testthat_result_df) %>% + dplyr::filter(test_name %in% c("FAIL", "WARN", "WARNINGs", "ERROR")) %>% dplyr::summarize(total = sum(num)) fail_num <- as.character(fail_num$total) From 6484c235039efa4abf5303a07627262a7c584650 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 1 Jul 2024 09:25:39 -0400 Subject: [PATCH 03/17] Export good_quiz_path() --- R/data.R | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/R/data.R b/R/data.R index b3bc611..c0d1c2f 100644 --- a/R/data.R +++ b/R/data.R @@ -47,55 +47,61 @@ download_ottr_template <- function(dir = "inst/extdata", type = "rmd") { return(output_dir) } -#' Get file path to an key encryption RDS -key_encrypt_creds_path <- function() { + +#' Path to good example quiz +#' +#' @export +#' @return The file path to an example good quiz included in the package that should pass the quiz checks. +#' +good_quiz_path <- function() { list.files( - pattern = "encrypt_pass.rds", + pattern = "quiz_good.md$", recursive = TRUE, system.file("extdata", package = "ottrpal"), full.names = TRUE ) } -#' Get file path to an encrypted credentials RDS -encrypt_creds_path <- function() { + +#' Path to bad example quiz +#' +#' @export +#' @return The file path to an example bad quiz included in the package that will fail the quiz checks. +#' +#' @examples +#' +#' quiz_path <- bad_quiz_path() +bad_quiz_path <- function() { list.files( - pattern = "encrypt.rds", + pattern = "quiz_bad.md$", recursive = TRUE, system.file("extdata", package = "ottrpal"), full.names = TRUE ) } -#' Get file path to an default credentials RDS -encrypt_creds_user_path <- function() { +#' Get file path to an key encryption RDS +key_encrypt_creds_path <- function() { list.files( - pattern = "encrypted_default_user_creds.rds", + pattern = "encrypt_pass.rds", recursive = TRUE, system.file("extdata", package = "ottrpal"), full.names = TRUE ) } - -good_quiz_path <- function() { +#' Get file path to an encrypted credentials RDS +encrypt_creds_path <- function() { list.files( - pattern = "quiz_good.md$", + pattern = "encrypt.rds", recursive = TRUE, system.file("extdata", package = "ottrpal"), full.names = TRUE ) } -#' Path to bad example quiz -#' -#' @export -#' @return The file path to an example bad quiz included in the package that will fail the quiz checks. -#' -#' @examples -#' -#' quiz_path <- bad_quiz_path() -bad_quiz_path <- function() { +#' Get file path to an default credentials RDS +encrypt_creds_user_path <- function() { list.files( - pattern = "quiz_bad.md$", + pattern = "encrypted_default_user_creds.rds", recursive = TRUE, system.file("extdata", package = "ottrpal"), full.names = TRUE From 190cc635676726851fdfecf3afd0f4128392dc8d Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 1 Jul 2024 09:28:16 -0400 Subject: [PATCH 04/17] Fixing some notes --- R/coursera_and_leanpub.R | 4 ++-- R/iframe_leanpub.R | 6 +++--- R/set_up.R | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/coursera_and_leanpub.R b/R/coursera_and_leanpub.R index 32afae0..faa2cec 100644 --- a/R/coursera_and_leanpub.R +++ b/R/coursera_and_leanpub.R @@ -199,7 +199,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. +#' Create a version of the course that does not have a TOC and has quizzes in the Coursera yaml format. #' #' @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" @@ -221,7 +221,7 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), output_quiz_dir = "coursera_quizzes", verbose = TRUE) { # Find root directory by finding `_bookdown.yml` file - root_dir <- bookdown_path() + root_dir <- course_path() # Output files: output_dir <- file.path(root_dir, output_dir) diff --git a/R/iframe_leanpub.R b/R/iframe_leanpub.R index 5e3c179..0040c62 100644 --- a/R/iframe_leanpub.R +++ b/R/iframe_leanpub.R @@ -17,7 +17,7 @@ #' @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 [ottrpal::bookdown_to_book_txt()] be run +#' @param make_book_txt Should [ottrpal::course_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, @@ -110,10 +110,10 @@ website_to_embed_leanpub <- function(path = ".", ####################### Book.txt creation #################################### out <- NULL if (make_book_txt) { - if (verbose) message("Running bookdown_to_book_txt") + if (verbose) message("Running course_to_book_txt") md_files <- basename(unlist(md_output_files)) - bookdown_to_book_txt( + course_to_book_txt( md_files = md_files, output_dir = output_dir, quiz_dir = quiz_dir, diff --git a/R/set_up.R b/R/set_up.R index 78d5d32..755cdf6 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -158,7 +158,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") { #' @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 [ottrpal::bookdown_to_book_txt()] be run +#' @param make_book_txt Should [ottrpal::course_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, From 451511a8241d0ba033085929a1f74288e8cecaab Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 1 Jul 2024 09:31:27 -0400 Subject: [PATCH 05/17] I don't think we need replace_html.R --- R/replace_html.R | 514 ----------------------------------------------- 1 file changed, 514 deletions(-) delete mode 100644 R/replace_html.R diff --git a/R/replace_html.R b/R/replace_html.R deleted file mode 100644 index 26026bc..0000000 --- a/R/replace_html.R +++ /dev/null @@ -1,514 +0,0 @@ -#' Convert youtube link -#' -#' @param utube_link a link to a youtube video that may or may not be "www.youtube.com/embed" or "www.youtube.com/watch?v=" -#' format. -#' -#' @return Returns a youtube link in the "watch" format so it will render properly in Leanpub or Coursera-friendly files -#' @export -#' -convert_utube_link <- function(utube_link) { - # If it has a youtube embed link, switch to the watch format link - if (grepl("www.youtube.com/embed", utube_link)) { - utube_link <- paste0( - "https://www.youtube.com/watch?v=", - strsplit(utube_link, - split = "www.youtube.com/embed/" - )[[1]][2] - ) - } - return(utube_link) -} - - -get_html_element <- function(x, element = "img") { - x <- paste(x, collapse = "\n") - doc <- xml2::read_html(x) - nodes <- rvest::html_nodes(doc, xpath = paste0("//", element)) - nodes -} - - -get_figure_div <- function(x) { - div <- get_html_element(x, element = 'div[@class="figure"]') - types <- div %>% - rvest::html_elements(xpath = ".//img|.//iframe") %>% - rvest::html_name() - img <- div %>% - rvest::html_elements(xpath = ".//img|.//iframe") %>% - rvest::html_attrs() - # missing something - not a img or iframe - stopifnot(length(img) == length(div)) - div_aligns <- div %>% - rvest::html_attr(name = "style") - div_aligns <- trimws(sub("text-align: ", "", div_aligns)) - # markua - div_aligns[div_aligns == "center"] <- "middle" - div_aligns[is.na(div_aligns)] <- "middle" - captions <- div %>% - rvest::html_elements(xpath = './p[@class="caption"]') %>% - rvest::html_text() - img <- mapply(function(x, y) { - c(x, caption = y) - }, img, captions, SIMPLIFY = FALSE) - img <- mapply(function(x, y) { - c(x, element_type = y) - }, img, types, SIMPLIFY = FALSE) - img <- mapply(function(x, y) { - y[y %in% ""] <- NA_character_ - if (!all(is.na(y))) { - x <- c(x, align = y) - } - x - }, img, div_aligns, SIMPLIFY = FALSE) - - return(img) -} - - - -get_html_attr <- function(x, element = "img", name = "src") { - x <- get_html_element(x, element = element) - rvest::html_attr(x, name) -} - -get_iframe_attr <- function(x, name = "src") { - x <- get_html_attr(x, element = "iframe", name = name) -} - -get_img_attr <- function(x, name = "src") { - x <- get_html_attr(x, element = "img", name = name) -} - -get_iframe_src <- get_iframe_attr -get_img_src <- get_img_attr - -get_iframe_alt <- function(x) { - get_iframe_attr(x, name = "alt") -} - -get_img_alt <- function(x) { - get_img_attr(x, name = "alt") -} - - - - -find_figure_div <- function(x) { - regex <- paste0('
") - end <- which(grepl(regex, x = x) & !grepl("^ - - - + + + + - Page not found (404) • ottrpal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + - - - - -
-
- + +
+ +
+ - - -
+
+
-
- +
+ + - - diff --git a/docs/LICENSE.html b/docs/LICENSE.html index 500ddc9..0a6986f 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -1,66 +1,12 @@ - - - - - - - -GNU General Public License • ottrpal - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -GNU General Public License • ottrpal - - - - + + -
-
- -
- -
+