From a44d3ff372989c385e80ad98c0b3b5c31795d216 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Tue, 16 Apr 2024 16:12:03 -0400 Subject: [PATCH 1/5] Adding quarto compatibility. Initial mapping --- R/coursera.R | 61 ++++++++++++++++++++++++++++++++-------------------- R/set_up.R | 1 - 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/R/coursera.R b/R/coursera.R index 37d1b8d..e7c28ed 100644 --- a/R/coursera.R +++ b/R/coursera.R @@ -215,14 +215,20 @@ convert_coursera_quizzes <- function(input_quiz_dir = "quizzes", #' @importFrom utils download.file #' @import bookdown_path #' +# TODO: add a quarto/rmd argument here render_without_toc <- function(output_dir = file.path("docs", "no_toc"), + render_type = "rmd" output_yaml = "_output.yml", convert_quizzes = FALSE, input_quiz_dir = "quizzes", output_quiz_dir = "coursera_quizzes", verbose = TRUE) { - # Find root directory by finding `_bookdown.yml` file - root_dir <- bookdown_path() + + # TODO: Look for a _quarto.yml OR a _bookdown.yml file for the root_dir + # TODO: declare in render_type whether this is an Rmd or quarto + + # TODO: root directory will be where _quarto.yml OR a _bookdown.yml is found + root_dir <- list.file(pattern = "") # Output files: output_dir <- file.path(root_dir, output_dir) @@ -235,20 +241,8 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), } ###### Declare all the file paths relative to root directory ###### - # Input files: - toc_close_css <- file.path(root_dir, "assets", "toc_close.css") - - if (!file.exists(toc_close_css)) { - download.file("https://raw.githubusercontent.com/jhudsl/ottrpal/master/inst/extdata/toc_close.css", - destfile = toc_close_css - ) - } output_yaml_file <- file.path(root_dir, output_yaml) - # Make sure we have that file - if (!file.exists(toc_close_css)) { - stop(paste0("Could not find: ", toc_close_css)) - } # Make sure we know where the output yaml is if (!file.exists(output_yaml_file)) { stop(paste0("Could not find: ", output_yaml_file)) @@ -280,15 +274,29 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), fs::dir_copy(needed_dir, file.path(output_dir, needed_dir), overwrite = TRUE) } }) - - # Slightly different path for the libs folder - libs_path <- file.path("docs", "libs") - if (!dir.exists(file.path(output_dir, "libs"))) { - if (verbose) { - message(file.path("docs", "libs")) - } - fs::dir_copy(libs_path, file.path(output_dir, "libs"), overwrite = TRUE) - } + + if (render_type == "rmd") { + # Input files: + toc_close_css <- file.path(root_dir, "assets", "toc_close.css") + + if (!file.exists(toc_close_css)) { + download.file("https://raw.githubusercontent.com/jhudsl/ottrpal/master/inst/extdata/toc_close.css", + destfile = toc_close_css + ) + } + # Make sure we have that file + if (!file.exists(toc_close_css)) { + stop(paste0("Could not find: ", toc_close_css)) + } + + # Slightly different path for the libs folder + libs_path <- file.path("docs", "libs") + if (!dir.exists(file.path(output_dir, "libs"))) { + if (verbose) { + message(file.path("docs", "libs")) + } + fs::dir_copy(libs_path, file.path(output_dir, "libs"), overwrite = TRUE) + } ###### Copy over CSS file ###### # Retrieve yaml file specs @@ -339,6 +347,13 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), # Write to "style.css" writeLines(append(full_css, toc_close_css_lines), css_file) + } + + if (render_type == "quarto") { + + # TODO: Put code here about how to get rid of TOC + + } # Only convert the quizzes if set to TRUE if (convert_quizzes) { diff --git a/R/set_up.R b/R/set_up.R index 7c10807..d0fe7bf 100644 --- a/R/set_up.R +++ b/R/set_up.R @@ -23,7 +23,6 @@ get_bookdown_spec <- function(path = ".") { #' #' @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] From 1073ca6b77e86784be15eb53e128bf6847243a87 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Thu, 16 May 2024 13:46:59 -0400 Subject: [PATCH 2/5] quarto compatibility --- .Rbuildignore | 1 + CRAN-SUBMISSION | 3 +++ R/auth.R | 1 - R/bookdown_to_leanpub.R | 31 ++++++++++++++++++++++++++----- R/data.R | 1 - R/notes_to_fig_alt.R | 29 ++++++++++++++++------------- tests/testthat.R | 4 ---- tests/testthat/test-gs_download.R | 15 --------------- 8 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 CRAN-SUBMISSION delete mode 100644 tests/testthat.R delete mode 100644 tests/testthat/test-gs_download.R diff --git a/.Rbuildignore b/.Rbuildignore index 7c91135..f3f8d1e 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -33,3 +33,4 @@ vignettes/.httr-oauth$ ^package_bundles/leanbuild_0.1.2.tgz* inst/extdata/tmp/default* +^CRAN-SUBMISSION$ diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION new file mode 100644 index 0000000..10227b0 --- /dev/null +++ b/CRAN-SUBMISSION @@ -0,0 +1,3 @@ +Version: 1.2.1 +Date: 2024-01-09 15:20:24 UTC +SHA: 60a69efa2a507c472471cf7ffb7c8d7d370d66ab diff --git a/R/auth.R b/R/auth.R index 2b871ef..25cb2a0 100644 --- a/R/auth.R +++ b/R/auth.R @@ -78,7 +78,6 @@ authorize <- function(token = NULL, cache = FALSE, ...) { #' } #' auth_from_secret <- function(access_token = NULL, refresh_token = NULL) { - # If no tokens are specified, we'll grab the default ones. if (is.null(access_token) | is.null(refresh_token)) { decrypted <- openssl::aes_cbc_decrypt( diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index fbe9522..1848810 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -434,13 +434,32 @@ make_embed_markdown <- function(url, 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")))) + index_html <- suppressWarnings(try(xml2::read_html(bookdown_index))) - # Extract chapter nodes + # Extract chapter nodes the Rmd way nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'li[@class="chapter"]')) - if (length(nodes) > 0) { - # Format into a data.frame + # If the Rmd way didn't work, lets try the quarto way + if (length(nodes) < 0) { + # Get the sidebar stuff + nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'div[@class="sidebar-item-container"]')) + + # Extract chapter nodes from the sidebar + chapt_titles <- nodes %>% + rvest::html_nodes('span.chapter-title') %>% + rvest::html_text() + + data_level <- nodes %>% + rvest::html_nodes('span.chapter-number') %>% + rvest::html_text() + + data_path <- rvest::html_nodes(nodes, xpath = paste0("//", 'a[@class="sidebar-item-text sidebar-link"]')) %>% + rvest::html_attr('href') %>% + stringr::str_remove("^\\.\\/") + + chapt_data <- data.frame(chapt_titles, data_level, data_path) + + } else { chapt_data <- rvest::html_attrs(nodes) %>% dplyr::bind_rows() %>% dplyr::rename_with(~ gsub("-", "_", .x, fixed = TRUE)) %>% @@ -451,7 +470,9 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"), dplyr::select(url, chapt_title) %>% as.data.frame() %>% dplyr::distinct(url, .keep_all = TRUE) - } else { + } + + if (nrow(chapt_data) < 1) { stop(paste("Chapter retrieval from:", bookdown_index, "Failed.")) } diff --git a/R/data.R b/R/data.R index 40c1c60..da70c76 100644 --- a/R/data.R +++ b/R/data.R @@ -1,4 +1,3 @@ - #' Get file path to an key encryption RDS key_encrypt_creds_path <- function() { list.files( diff --git a/R/notes_to_fig_alt.R b/R/notes_to_fig_alt.R index 7640f8a..06bec33 100644 --- a/R/notes_to_fig_alt.R +++ b/R/notes_to_fig_alt.R @@ -21,7 +21,7 @@ get_gs_pptx <- function(id) { fr_header <- result$headers$`x-frame-options` if (!is.null(fr_header)) { if (all(fr_header == "DENY")) { - warn_them <- TRUE + warn_them <- TRUE } } if (httr::status_code(result) >= 300) { @@ -30,7 +30,7 @@ get_gs_pptx <- function(id) { # don't write something if not really a pptx ctype <- result$headers$`content-type` if (httr::status_code(result) >= 400 && - !is.null(ctype) && grepl("html", ctype)) { + !is.null(ctype) && grepl("html", ctype)) { file.remove(pptx_file) } if (grepl("ServiceLogin", result$url)) { @@ -38,7 +38,7 @@ get_gs_pptx <- function(id) { } if (warn_them) { warning( - paste0( + paste0( "This presentation may not be available, ", "did you turn link sharing on?" ) @@ -50,11 +50,12 @@ get_gs_pptx <- function(id) { export_url <- function(id, page_id = NULL, type = "pptx") { - url = paste0( + url <- paste0( "https://docs.google.com/presentation/d/", - id, "/export/", type, "?id=", id) + id, "/export/", type, "?id=", id + ) if (!is.null(page_id)) { - url = paste0(url, "&pageid=", page_id) + url <- paste0(url, "&pageid=", page_id) } url } @@ -300,22 +301,25 @@ xml_notes <- function(file, collapse_text = TRUE, xpath = "//a:r//a:t") { #' extract_object_id(slide_url = "https://docs.google.com/presentation/d/1H5aF_ROKVxE-H #' FHhoOy9vU2Y-y2M_PiV0q-JBL17Gss/edit?usp=sharing") #' } -extract_object_id = function(slide_url, token = NULL, access_token = NULL, refresh_token = NULL) { +extract_object_id <- function(slide_url, token = NULL, access_token = NULL, refresh_token = NULL) { # Get Slide ID from URL id <- get_slide_id(slide_url) # Using Slide ID, create url that we'll send to GET get_url <- gsub("{presentationId}", id, - "https://slides.googleapis.com/v1/presentations/{presentationId}", fixed=TRUE) + "https://slides.googleapis.com/v1/presentations/{presentationId}", + fixed = TRUE + ) # if token not provided, fetch token if (is.null(token)) { - token_try <- try(get_token(), silent = TRUE) # We will supply credentials if none can be grabbed by get_token() if (is.null(token_try)) { - auth_from_secret(access_token = access_token, - refresh_token = refresh_token) + auth_from_secret( + access_token = access_token, + refresh_token = refresh_token + ) } token <- get_token() } # else user provides token @@ -341,7 +345,7 @@ extract_object_id = function(slide_url, token = NULL, access_token = NULL, refre #' #' @examples #' \dontrun{ -#' get_object_id_notes("https://docs.google.com/presentation/d/ +#' get_object_id_notes("https://docs.google.com/presentation/d/ #' 1H5aF_ROKVxE-HFHhoOy9vU2Y-y2M_PiV0q-JBL17Gss/edit?usp=sharing") #' } get_object_id_notes <- function(slide_url) { @@ -356,4 +360,3 @@ get_object_id_notes <- function(slide_url) { data.frame(id = object_ids, notes = speaker_notes) } - diff --git a/tests/testthat.R b/tests/testthat.R deleted file mode 100644 index 137d47a..0000000 --- a/tests/testthat.R +++ /dev/null @@ -1,4 +0,0 @@ -library(testthat) -library(ottrpal) - -test_check("ottrpal") diff --git a/tests/testthat/test-gs_download.R b/tests/testthat/test-gs_download.R deleted file mode 100644 index 62c5600..0000000 --- a/tests/testthat/test-gs_download.R +++ /dev/null @@ -1,15 +0,0 @@ -testthat::test_that("Download a Slide ", { - - if (.Platform$OS.type != "windows") { - output_dir = tempdir() - outfile = ottrpal::include_slide( - "https://docs.google.com/presentation/d/1-7UvgVq5tP1pasTEErUM3bJFH2fU_pilH6i6_81CCXU/edit#slide=id.p", - output_dir = output_dir, - overwrite = FALSE) - testthat::expect_true(file.exists(outfile)) - bn = basename(outfile) - testthat::expect_true( - bn == "12DPZgPteQBwgal6kSPP58zhPhjZ7QSPZLe3NkA8M3eo_gc87451c247_0_17.png" - ) - } -}) From a8a0b81f2521427750950c7760355394ccda3e10 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Thu, 16 May 2024 14:32:32 -0400 Subject: [PATCH 3/5] Make it so only chapters are collected --- R/bookdown_to_leanpub.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 1848810..749edaf 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -444,6 +444,9 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"), # Get the sidebar stuff nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'div[@class="sidebar-item-container"]')) + # We only want chapters + nodes <- nodes[grep("chapter",as.character(nodes))] + # Extract chapter nodes from the sidebar chapt_titles <- nodes %>% rvest::html_nodes('span.chapter-title') %>% @@ -453,7 +456,8 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"), rvest::html_nodes('span.chapter-number') %>% rvest::html_text() - data_path <- rvest::html_nodes(nodes, xpath = paste0("//", 'a[@class="sidebar-item-text sidebar-link"]')) %>% + data_path <- nodes %>% + rvest::html_nodes('a.sidebar-item-text.sidebar-link') %>% rvest::html_attr('href') %>% stringr::str_remove("^\\.\\/") From f2cbcf07da385f147d1e4a86e0a86eeebdfb41ae Mon Sep 17 00:00:00 2001 From: cansavvy Date: Thu, 16 May 2024 15:41:48 -0400 Subject: [PATCH 4/5] Undo coursera.R changes --- R/coursera.R | 61 ++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/R/coursera.R b/R/coursera.R index e7c28ed..37d1b8d 100644 --- a/R/coursera.R +++ b/R/coursera.R @@ -215,20 +215,14 @@ convert_coursera_quizzes <- function(input_quiz_dir = "quizzes", #' @importFrom utils download.file #' @import bookdown_path #' -# TODO: add a quarto/rmd argument here render_without_toc <- function(output_dir = file.path("docs", "no_toc"), - render_type = "rmd" output_yaml = "_output.yml", convert_quizzes = FALSE, input_quiz_dir = "quizzes", output_quiz_dir = "coursera_quizzes", verbose = TRUE) { - - # TODO: Look for a _quarto.yml OR a _bookdown.yml file for the root_dir - # TODO: declare in render_type whether this is an Rmd or quarto - - # TODO: root directory will be where _quarto.yml OR a _bookdown.yml is found - root_dir <- list.file(pattern = "") + # Find root directory by finding `_bookdown.yml` file + root_dir <- bookdown_path() # Output files: output_dir <- file.path(root_dir, output_dir) @@ -241,8 +235,20 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), } ###### Declare all the file paths relative to root directory ###### + # Input files: + toc_close_css <- file.path(root_dir, "assets", "toc_close.css") + + if (!file.exists(toc_close_css)) { + download.file("https://raw.githubusercontent.com/jhudsl/ottrpal/master/inst/extdata/toc_close.css", + destfile = toc_close_css + ) + } output_yaml_file <- file.path(root_dir, output_yaml) + # Make sure we have that file + if (!file.exists(toc_close_css)) { + stop(paste0("Could not find: ", toc_close_css)) + } # Make sure we know where the output yaml is if (!file.exists(output_yaml_file)) { stop(paste0("Could not find: ", output_yaml_file)) @@ -274,29 +280,15 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), fs::dir_copy(needed_dir, file.path(output_dir, needed_dir), overwrite = TRUE) } }) - - if (render_type == "rmd") { - # Input files: - toc_close_css <- file.path(root_dir, "assets", "toc_close.css") - - if (!file.exists(toc_close_css)) { - download.file("https://raw.githubusercontent.com/jhudsl/ottrpal/master/inst/extdata/toc_close.css", - destfile = toc_close_css - ) - } - # Make sure we have that file - if (!file.exists(toc_close_css)) { - stop(paste0("Could not find: ", toc_close_css)) - } - - # Slightly different path for the libs folder - libs_path <- file.path("docs", "libs") - if (!dir.exists(file.path(output_dir, "libs"))) { - if (verbose) { - message(file.path("docs", "libs")) - } - fs::dir_copy(libs_path, file.path(output_dir, "libs"), overwrite = TRUE) - } + + # Slightly different path for the libs folder + libs_path <- file.path("docs", "libs") + if (!dir.exists(file.path(output_dir, "libs"))) { + if (verbose) { + message(file.path("docs", "libs")) + } + fs::dir_copy(libs_path, file.path(output_dir, "libs"), overwrite = TRUE) + } ###### Copy over CSS file ###### # Retrieve yaml file specs @@ -347,13 +339,6 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"), # Write to "style.css" writeLines(append(full_css, toc_close_css_lines), css_file) - } - - if (render_type == "quarto") { - - # TODO: Put code here about how to get rid of TOC - - } # Only convert the quizzes if set to TRUE if (convert_quizzes) { From b4c9c6b7c9800da9ba59ff734859336612f0fc33 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 11 Jun 2024 09:34:35 -0400 Subject: [PATCH 5/5] Add Howard logic fix Co-authored-by: Howard Baek <50791792+howardbaek@users.noreply.github.com> --- R/bookdown_to_leanpub.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index 749edaf..0b54a3f 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -440,7 +440,7 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"), nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'li[@class="chapter"]')) # If the Rmd way didn't work, lets try the quarto way - if (length(nodes) < 0) { + if (length(nodes) < 1) { # Get the sidebar stuff nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'div[@class="sidebar-item-container"]'))