Skip to content

Commit

Permalink
Merge pull request #129 from jhudsl/cansavvy/quarto
Browse files Browse the repository at this point in the history
quarto `get_chapters()` compatibility
  • Loading branch information
cansavvy authored Jun 11, 2024
2 parents 5d44949 + b4c9c6b commit 828539f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 40 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ vignettes/.httr-oauth$
^package_bundles/leanbuild_0.1.2.tgz*
inst/extdata/tmp/default*

^CRAN-SUBMISSION$
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 1.2.1
Date: 2024-01-09 15:20:24 UTC
SHA: 60a69efa2a507c472471cf7ffb7c8d7d370d66ab
1 change: 0 additions & 1 deletion R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 30 additions & 5 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,36 @@ 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) < 1) {
# 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') %>%
rvest::html_text()

data_level <- nodes %>%
rvest::html_nodes('span.chapter-number') %>%
rvest::html_text()

data_path <- nodes %>%
rvest::html_nodes('a.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)) %>%
Expand All @@ -451,7 +474,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."))
}

Expand Down
1 change: 0 additions & 1 deletion R/data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Get file path to an key encryption RDS
key_encrypt_creds_path <- function() {
list.files(
Expand Down
29 changes: 16 additions & 13 deletions R/notes_to_fig_alt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,15 +30,15 @@ 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)) {
warn_them <- TRUE
}
if (warn_them) {
warning(
paste0(
paste0(
"This presentation may not be available, ",
"did you turn link sharing on?"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -356,4 +360,3 @@ get_object_id_notes <- function(slide_url) {

data.frame(id = object_ids, notes = speaker_notes)
}

1 change: 0 additions & 1 deletion R/set_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat.R

This file was deleted.

15 changes: 0 additions & 15 deletions tests/testthat/test-gs_download.R

This file was deleted.

0 comments on commit 828539f

Please sign in to comment.