Skip to content

Commit

Permalink
Merge pull request #116 from jhudsl/cansavvy/fig.alt-2
Browse files Browse the repository at this point in the history
Cansavvy's attempts at getting fig.alt's to sync between Google slides and Rmd
  • Loading branch information
howardbaik authored Aug 14, 2023
2 parents 31221a9 + c37aa3e commit 43a050d
Show file tree
Hide file tree
Showing 34 changed files with 984 additions and 143 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ vignettes/.httr-oauth$
^vignettes/toc_close.css$
^package_bundles/leanbuild_0.1.2.tar.gz*
^package_bundles/leanbuild_0.1.2.tgz*
inst/extdata/tmp/default*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ tests/testthat/googlesheets_token.rds
run_test.R
/doc/
/Meta/
.secrets
inst/extdata/tmp/default*
67 changes: 34 additions & 33 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
Type: Package
Package: ottrpal
Version: 1.1.1
Title: Companion Tools for Open-Source Tools for Training Resources (OTTR)
Description: Tools for converting Open-Source Tools for Training Resources (OTTR)
courses into Leanpub or Coursera courses. 'ottrpal' is for use with the OTTR Template repository to create courses.
Authors@R: c(person(given = "Candace",family = "Savonen",
email = "cansav09@gmail.com",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-6331-7070")),
person(given = "John",family = "Muschelli",
email = "muschellij2@gmail.com",
role = c("aut"),
comment = c(ORCID = "0000-0001-6469-1750")),
person(given = "Carrie", family = "Wright",
email = "cwrigh60@jhu.edu",
role = c("ctb"))
Version: 1.1.1
Authors@R: c(
person("Candace", "Savonen", , "cansav09@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-6331-7070")),
person("John", "Muschelli", , "muschellij2@gmail.com", role = "aut",
comment = c(ORCID = "0000-0001-6469-1750")),
person("Carrie", "Wright", , "cwrigh60@jhu.edu", role = "ctb")
)
Depends: R (>= 3.5.0)
Description: Tools for converting Open-Source Tools for Training Resources
(OTTR) courses into Leanpub or Coursera courses. 'ottrpal' is for use
with the OTTR Template repository to create courses.
License: GPL-3
Imports: dplyr,
httr,
fs,
rmarkdown (>= 2.10),
xml2,
rvest,
readr,
R.utils,
URL: https://github.com/jhudsl/ottrpal
BugReports: https://github.com/jhudsl/ottrpal/issues
Depends:
R (>= 3.5.0)
Imports:
bookdown,
glue,
curl,
rprojroot,
dplyr,
fs,
glue,
httr,
jsonlite,
knitr (>= 1.33),
magrittr,
yaml,
openssl,
purrr,
R.utils,
readr,
rmarkdown (>= 2.10),
rprojroot,
rvest,
stringr,
knitr (>= 1.33),
purrr
xml2,
yaml
Suggests:
remotes,
testthat,
tibble,
utils
VignetteBuilder:
knitr
ByteCompile: true
Encoding: UTF-8
LazyData: true
ByteCompile: true
Type: Package
VignetteBuilder: knitr
URL: https://github.com/jhudsl/ottrpal
BugReports: https://github.com/jhudsl/ottrpal/issues
RoxygenNote: 7.2.3
26 changes: 25 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(auth_from_secret)
export(authorize)
export(bad_quiz_path)
export(bookdown_destination)
export(bookdown_file)
Expand All @@ -19,15 +21,17 @@ export(convert_coursera_quizzes)
export(convert_footnotes)
export(convert_quiz)
export(convert_utube_link)
export(download_gs_file)
export(example_repo_cleanup)
export(example_repo_setup)
export(extract_meta)
export(extract_object_id)
export(extract_quiz)
export(get_bookdown_spec)
export(get_chapters)
export(get_gs_pptx)
export(get_image_from_slide)
export(get_image_link_from_slide)
export(get_object_id_notes)
export(get_slide_id)
export(get_slide_page)
export(good_quiz_path)
Expand All @@ -40,14 +44,34 @@ export(make_embed_markdown)
export(parse_q_tag)
export(parse_quiz)
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(xml_notes)
importFrom(fs,dir_copy)
importFrom(httr,GET)
importFrom(httr,accept_json)
importFrom(httr,config)
importFrom(httr,content)
importFrom(httr,oauth2.0_token)
importFrom(httr,oauth_app)
importFrom(httr,oauth_endpoints)
importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(openssl,aes_cbc_decrypt)
importFrom(readr,write_tsv)
importFrom(utils,download.file)
importFrom(utils,installed.packages)
importFrom(utils,menu)
importFrom(utils,unzip)
importFrom(xml2,read_xml)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_text)
1 change: 0 additions & 1 deletion R/aaa_utils.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

utils::globalVariables(c(
"num", "quiz_dir", "type_url", "file_name", "trimmed", "quiz",
"quiz_path", "type", "q_num", "verbose", "chapt_title", "data_path", "image_dir"
Expand Down
125 changes: 125 additions & 0 deletions R/auth.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
.tokenEnv <- new.env(parent = emptyenv())
.tokenEnv$Token <- NULL

# Set token to environment
set_token <- function(value) {
.tokenEnv$Token <- value
return(value)
}

# Get token from environment
get_token <- function() {
.tokenEnv$Token
}

### Declare all the scopes
scopes_list <- c(
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/presentations",
"https://www.googleapis.com/auth/presentations.readonly"
)


#' Authorize R package to access the Google Slides API
#' @description This is a function to authorize the R package to access the Google Slides API interactively.
#' @param token An output from \code{\link{oauth2.0_token}} to set as the authentication token.
#' @param cache Should the token be cached as an .httr-oauth file?
#' @param ... Additional arguments to send to \code{\link{oauth2.0_token}}
#' @return OAuth token saved to the environment so the package can use the users' Google data
#' @importFrom utils menu installed.packages
#' @importFrom httr oauth_app oauth_endpoints oauth2.0_token
#' @export
#' @examples \dontrun{
#'
#' authorize()
#' }
authorize <- function(token = NULL, cache = FALSE, ...) {
if (!cache) {
cache_it <- menu(c("Yes store credentials as .httr-oauth file", "No do not store credentials, I will re-run this authorize() in my next R session"))
if (cache_it == 1) {
message("You chose to cache your credentials, if you change your mind, just delete the .httr-oauth. Be careful not to push this file to GitHub or share it anywhere.")
}
} else {
cache_it <- 1
}
if (is.null(token)) {
token <- httr::oauth2.0_token(
endpoint = app_set_up()$endpoint,
app = app_set_up()$app,
cache = cache_it == 1,
scope = scopes_list,
...
)
}
set_token(token)
return(invisible(token))
}

#' Use secrets to authorize R package to access Google Slides API
#' @description This is a function to authorize the R package to access the Google Slides API. If no
#' client.id and client.secret is provided, the package would provide predefined values.
#' @param access_token Access token can be obtained from running authorize() interactively: token <-authorize(); token$credentials$access_token
#' @param refresh_token Refresh token can be obtained from running authorize() interactively: token <-authorize(); token$credentials$refresh_token
#' @return OAuth token saved to the environment so the package can use the users' Google data
#' @importFrom utils menu installed.packages
#' @importFrom httr oauth_app oauth_endpoints oauth2.0_token
#' @importFrom openssl aes_cbc_decrypt
#' @export
#' @examples \dontrun{
#'
#' token <- authorize()
#'
#' auth_from_secret(
#' token$credentials$access_token,
#' token$credentials$refresh_token
#' )
#' }
#'
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(
readRDS(encrypt_creds_user_path()),
key = readRDS(key_encrypt_creds_path())
)
access_token <- unserialize(decrypted)[[1]]$access_token
refresh_token <- unserialize(decrypted)[[1]]$refresh_token
}

credentials <- list(
access_token = access_token,
expires_in = 3599L,
refresh_token = refresh_token,
scope = scopes_list,
token_type = "Bearer"
)

token <- httr::oauth2.0_token(
endpoint = app_set_up()$endpoint,
app = app_set_up()$app,
scope = scopes_list,
credentials = credentials
)

set_token(token)
return(invisible(token))
}

# This sets up the app creds no matter which way authorization is called
app_set_up <- function() {
decrypted <- openssl::aes_cbc_decrypt(
readRDS(encrypt_creds_path()),
key = readRDS(key_encrypt_creds_path())
)
app <- httr::oauth_app(
appname = "ottrpal",
key = unserialize(decrypted)$client_id,
secret = unserialize(decrypted)$client_secret
)
endpoint <- httr::oauth_endpoints("google")

return(list(app = app, endpoint = endpoint))
}
15 changes: 6 additions & 9 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Convert Bookdown to Leanpub
#'
#' @param path path to the bookdown book, must have a `_bookdown.yml` file
Expand Down Expand Up @@ -162,10 +161,11 @@ bookdown_to_leanpub <- function(path = ".",
#' @examples \dontrun{
#'
#' ottrpal::bookdown_to_embed_leanpub(
#' base_url = "https://jhudatascience.org/OTTR_Template/",
#' make_book_txt = TRUE,
#' quiz_dir = NULL)
#'}
#' base_url = "https://jhudatascience.org/OTTR_Template/",
#' make_book_txt = TRUE,
#' quiz_dir = NULL
#' )
#' }
bookdown_to_embed_leanpub <- function(path = ".",
chapt_img_key = NULL,
bookdown_index = file.path(base_url, "index.html"),
Expand Down Expand Up @@ -209,12 +209,11 @@ bookdown_to_embed_leanpub <- function(path = ".",
bookdown_index = paste0(base_url, "index.html"),
base_url = base_url
) %>%
dplyr::mutate(chapt_title = gsub("\\:|\\?|\\&|\\!|\\'", "", chapt_title))
dplyr::mutate(chapt_title = gsub("\\:|\\?|\\&|\\!|\\'", "", chapt_title))
}

# If there's no img_path supplied, then use a default image for each.
if (!("img_path" %in% colnames(chapt_df))) {

# If no default image is supplied
if (is.null(default_img)) {
default_img <- "https://docs.google.com/presentation/d/1jEUxUY1qXDZ3DUtvTU6NCc6ASG5nx4Gwczv5aAglYX4/edit#slide=id.p"
Expand Down Expand Up @@ -293,7 +292,6 @@ bookdown_to_book_txt <- function(path = ".",
output_dir = "manuscript",
quiz_dir = "quizzes",
verbose = TRUE) {

# If md_files are not specified, then try to get them
if (is.null(md_files)) {
# Establish path
Expand Down Expand Up @@ -435,7 +433,6 @@ 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"))))

Expand Down
18 changes: 8 additions & 10 deletions R/coursera.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ convert_quiz <- function(quiz_path,
convert_coursera_quizzes <- function(input_quiz_dir = "quizzes",
output_quiz_dir = "coursera_quizzes",
verbose = TRUE) {

# Create directory if it is not yet created
if (!dir.exists(output_quiz_dir)) {
dir.create(output_quiz_dir, recursive = TRUE)
Expand Down Expand Up @@ -221,10 +220,9 @@ 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
root_dir <- bookdown_path()

# Output files:
output_dir <- file.path(root_dir, output_dir)

Expand Down Expand Up @@ -297,12 +295,12 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"),

# Copy over css file(s) that's specified
org_css_file <- output_yaml_lines$`bookdown::gitbook`$css
# Check if there are multiple .css
if(length(org_css_file) > 1){

# Check if there are multiple .css
if (length(org_css_file) > 1) {
# Read all .css
css_files_read <- sapply(org_css_file, readLines)

# Make a "mega .css" and write
if (verbose) {
message("Combining .css files")
Expand All @@ -312,11 +310,11 @@ render_without_toc <- function(output_dir = file.path("docs", "no_toc"),
writeLines(css_lines_cat, css_file)
} else {
css_file <- file.path(output_dir, org_css_file)

# Write it as "style.css"
fs::file_copy(org_css_file,
css_file,
overwrite = TRUE
css_file,
overwrite = TRUE
)
}

Expand Down
Loading

0 comments on commit 43a050d

Please sign in to comment.