Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quarto get_chapters() compatibility #129

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@howardbaik howardbaik May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran get_chapters() with index.html generated by the Quarto version of ottr, and got this error message:

Error in `dplyr::mutate()`:
ℹ In argument: `url = paste0(base_url, data_path)`.
Caused by error:
! object 'data_path' not found

The error stems from Line 472.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We pair programmed on this and determined it was a simple logic issue.

Can you test this again?

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) < 0) {
cansavvy marked this conversation as resolved.
Show resolved Hide resolved
# 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.

Loading