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

Update quiz handling #99

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
70 changes: 37 additions & 33 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ bookdown_to_leanpub <- function(path = ".",
footer_text = NULL,
clean_up = FALSE) {
# Run the set up
set_up_leanpub(path = path,
embed = FALSE,
clean_up = clean_up,
render = render,
output_dir = output_dir,
make_book_txt = make_book_txt,
quiz_dir = quiz_dir,
run_quiz_checks = run_quiz_checks,
remove_resources_start = remove_resources_start,
verbose = verbose,
footer_text = footer_text)
set_up_leanpub(
path = path,
embed = FALSE,
clean_up = clean_up,
render = render,
output_dir = output_dir,
make_book_txt = make_book_txt,
quiz_dir = quiz_dir,
run_quiz_checks = run_quiz_checks,
remove_resources_start = remove_resources_start,
verbose = verbose,
footer_text = footer_text
)

# Establish path
path <- bookdown_path(path)
Expand Down Expand Up @@ -162,9 +164,7 @@ bookdown_to_leanpub <- function(path = ".",
#' ottrpal::bookdown_to_embed_leanpub(base_url = "")
#'
#' ottrpal::bookdown_to_embed_leanpub(chapt_img_key = "chapter_urls.tsv")
#'
#' }

bookdown_to_embed_leanpub <- function(path = ".",
chapt_img_key = NULL,
bookdown_index = file.path("docs", "index.html"),
Expand All @@ -180,32 +180,34 @@ bookdown_to_embed_leanpub <- function(path = ".",
verbose = TRUE,
footer_text = "") {
# Run the set up
set_up_leanpub(path = path,
embed = TRUE,
clean_up = clean_up,
render = render,
output_dir = output_dir,
make_book_txt = make_book_txt,
quiz_dir = quiz_dir,
run_quiz_checks = run_quiz_checks,
remove_resources_start = remove_resources_start,
verbose = verbose)
set_up_leanpub(
path = path,
embed = TRUE,
clean_up = clean_up,
render = render,
output_dir = output_dir,
make_book_txt = make_book_txt,
quiz_dir = quiz_dir,
run_quiz_checks = run_quiz_checks,
remove_resources_start = remove_resources_start,
verbose = verbose
)

# If TSV chapter image key file is specified read it in
if (!is.null(chapt_img_key)) {

message(paste("Reading in a chapt_img_key TSV file:", chapt_img_key))
chapt_df <- readr::read_tsv(chapt_img_key)

} else {
# If its not supplied, create it from the get_chapters function
message("Creating a chapt_img_key TSV file")

if (is.null(base_url)) {
stop("No base_url is supplied and no chapt_img_key file was supplied. Need one or the other.")
}
chapt_df <- get_chapters(bookdown_index = bookdown_index,
base_url = base_url)
chapt_df <- get_chapters(
bookdown_index = bookdown_index,
base_url = base_url
)
}

# If there's no img_path supplied, then use a default image for each.
Expand All @@ -230,13 +232,13 @@ bookdown_to_embed_leanpub <- function(path = ".",
# Make the data.frame be in the same order
dplyr::select(dplyr::any_of("url"), dplyr::any_of("chapt_title"), dplyr::any_of("img_path")) %>%
# Run it make_embed_markdown on each row
purrr::pmap( ~ make_embed_markdown(url = ..1, chapt_title = ..2, img_path = ..3, footer_text = footer_text))
purrr::pmap(~ make_embed_markdown(url = ..1, chapt_title = ..2, img_path = ..3, footer_text = footer_text))

####################### Book.txt creation ####################################
out <- NULL
if (make_book_txt) {
if (verbose) message("Running bookdown_to_book_txt")
md_files <- basename(unlist(md_output_files))
md_files <- basename(unlist(md_output_files))

bookdown_to_book_txt(
md_files = md_files,
Expand Down Expand Up @@ -389,10 +391,12 @@ make_embed_markdown <- function(url,
file_contents <- c(
paste("#", chapt_title),
" ",
paste0("{type: iframe, title:", chapt_title,
", width:", width_spec,
", height:", height_spec,
", poster:", img_path, "}"),
paste0(
"{type: iframe, title:", chapt_title,
", width:", width_spec,
", height:", height_spec,
", poster:", img_path, "}"
),
paste0("![](", url, ")"),
" ",
footer_text,
Expand Down
41 changes: 21 additions & 20 deletions R/set_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,19 @@ copy_bib <- function(path = ".", output_dir = "manuscript") {
copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") {
quiz_dir <- file.path(quiz_dir)

if (!dir.exists(quiz_dir)) {
stop(paste(
"The quiz directory specified by quiz_dir:", quiz_dir, "does not exist.",
"If you don't have quizzes, set quiz_dir = NULL"
))
}
quizzes <- list.files(path = file.path(quiz_dir), full.names = TRUE, pattern = "\\.md$")
if (length(quizzes) > 0) {
fs::file_copy(quizzes, file.path(output_dir, basename(quizzes)),
overwrite = TRUE
)
if (!is.null(quiz_dir)) {
if (!dir.exists(quiz_dir)) {
warning(paste(
"The quiz directory specified by quiz_dir:", quiz_dir, "does not exist.",
"If you don't have quizzes, set quiz_dir = NULL"
))
}
quizzes <- list.files(path = file.path(quiz_dir), full.names = TRUE, pattern = "\\.md$")
if (length(quizzes) > 0) {
fs::file_copy(quizzes, file.path(output_dir, basename(quizzes)),
overwrite = TRUE
)
}
}
}

Expand All @@ -167,7 +169,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") {
#' @param path path to the bookdown book, must have a `_bookdown.yml` file
#' @param output_dir output directory to put files. It should likely be
#' relative to path
#' @param clean_up TRUE/FALSE the old output directory should be deleted and
#' @param clean_up TRUE/FALSE the old output directory should be deleted and
#' everything created fresh.
#' @param render if `TRUE`, then [bookdown::render_book()] will be run on each Rmd.
#' @param verbose print diagnostic messages
Expand All @@ -194,20 +196,19 @@ set_up_leanpub <- function(path = ".",
run_quiz_checks = FALSE,
remove_resources_start = FALSE,
verbose = TRUE,
footer_text = NULL,
footer_text = NULL,
embed = NULL) {

if (clean_up) {
message(paste("Clearing out old version of output files:", output_dir))

file.remove(output_dir, recursive = TRUE, showWarnings = FALSE)
}
}

# If output directory doesn't exist, make it
if (!dir.exists(output_dir)) {
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
}

# Declare regex for finding rmd files
rmd_regex <- "[.][R|r]md$"

Expand Down Expand Up @@ -250,12 +251,12 @@ set_up_leanpub <- function(path = ".",
clean_envir = FALSE
)
}

# We only need to copy these things if we are not doing embed
if (!embed) {
if (verbose) message("Copying Resources")
copy_resources(path, output_dir = output_dir)

if (verbose) message("Copying docs files")
copy_docs(path, output_dir = output_dir)

Expand Down