Skip to content

Commit

Permalink
Merge pull request #92 from jhudsl/cansavy/book_txt_handling
Browse files Browse the repository at this point in the history
Improve Book_txt handling
  • Loading branch information
cansavvy authored Feb 4, 2022
2 parents edd7b7c + 161d2e4 commit dfe67cc
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 49 deletions.
79 changes: 61 additions & 18 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#' @param quiz_dir directory that contains the quiz .md files that should be
#' checked and incorporated into the Book.txt file. If you don't have quizzes,
#' set this to NULL
#' @param clean_up TRUE/FALSE the old output directory should be deleted and
#' everything created fresh.
#' @param footer_text Optionally can add a bit of text that will be added to the
#' end of each file before the references section.
#'
Expand All @@ -28,8 +30,20 @@ bookdown_to_leanpub <- function(path = ".",
run_quiz_checks = FALSE,
remove_resources_start = FALSE,
verbose = TRUE,
footer_text = NULL) {
set_up_leanpub()
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)

# Establish path
path <- bookdown_path(path)
Expand All @@ -40,6 +54,7 @@ bookdown_to_leanpub <- function(path = ".",
rmd_files <- bookdown_rmd_files(path = path)

bib_files <- list.files(pattern = "[.]bib$")

if (length(bib_files) > 0) {
pandoc_args <- paste0("--bibliography=", path.expand(normalizePath(bib_files)))
} else {
Expand Down Expand Up @@ -78,7 +93,7 @@ bookdown_to_leanpub <- function(path = ".",
message("Running bookdown_to_book_txt")
}
bookdown_to_book_txt(
path = path,
md_files = rmd_files,
output_dir = output_dir,
quiz_dir = quiz_dir,
verbose = verbose
Expand Down Expand Up @@ -134,6 +149,8 @@ bookdown_to_leanpub <- function(path = ".",
#' @param quiz_dir directory that contains the quiz .md files that should be
#' checked and incorporated into the Book.txt file. If you don't have quizzes,
#' set this to NULL
#' @param clean_up TRUE/FALSE the old output directory should be deleted and
#' everything created fresh.
#' @param footer_text Optionally can add a bit of text that will be added to the
#' end of each file before the references section.
#'
Expand All @@ -147,10 +164,12 @@ bookdown_to_leanpub <- function(path = ".",
#' ottr::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"),
base_url = NULL,
clean_up = FALSE,
default_img = NULL,
render = TRUE,
output_dir = "manuscript",
Expand All @@ -160,13 +179,29 @@ bookdown_to_embed_leanpub <- function(path = ".",
remove_resources_start = FALSE,
verbose = TRUE,
footer_text = NULL) {
set_up_leanpub()

# 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,
footer_text = footer_text)

# 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.")
}
Expand All @@ -176,6 +211,8 @@ bookdown_to_embed_leanpub <- function(path = ".",

# 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 All @@ -199,11 +236,11 @@ bookdown_to_embed_leanpub <- function(path = ".",
####################### Book.txt creation ####################################
out <- NULL
if (make_book_txt) {
if (verbose > 1) {
message("Running bookdown_to_book_txt")
}
if (verbose) message("Running bookdown_to_book_txt")
md_files <- basename(unlist(md_output_files))
bookdown_to_book_txt(
path = output_dir,
md_files = ,
output_dir = output_dir,
quiz_dir = quiz_dir,
verbose = verbose
Expand Down Expand Up @@ -239,6 +276,7 @@ bookdown_to_embed_leanpub <- function(path = ".",
#' Create Book.txt file from files existing in quiz directory
#'
#' @param path path to the bookdown book, must have a `_bookdown.yml` file
#' @param md_files vector of file path of the md's to be included
#' @param output_dir output directory to put files. It should likely be
#' relative to path
#' @param quiz_dir Where are the quizzes stored? Default looks for folder called "quizzes".
Expand All @@ -248,32 +286,37 @@ bookdown_to_embed_leanpub <- function(path = ".",
#' @export
#'
bookdown_to_book_txt <- function(path = ".",
md_files = NULL,
output_dir = "manuscript",
quiz_dir = "quizzes",
verbose = TRUE) {
# Establish path
path <- bookdown_path(path)

# If md_files are not speciied, then try to get them
if (is.null(md_files)) {
# Establish path
path <- bookdown_path(path)

rmd_regex <- "[.][R|r]md$"
rmd_regex <- "[.][R|r]md$"

# Extract the names of the Rmd files (the chapters)
rmd_files <- bookdown_rmd_files(path = path)
# Extract the names of the Rmd files (the chapters)
md_files <- bookdown_rmd_files(path = path)
}

if (!is.null(quiz_dir)) {
# Find the quiz files in the quiz directory
quiz_files <- list.files(pattern = "\\.md$", quiz_dir)

# Put files in one vector
all_files <- c(rmd_files, quiz_files)
all_files <- c(md_files, quiz_files)

# Make a vector specifying the file type: quiz or not
file_type <- c(
rep("non-quiz", length(rmd_files)),
rep("non-quiz", length(md_files)),
rep("quiz", length(quiz_files))
)
} else {
all_files <- rmd_files
file_type <- rep("non-quiz", length(rmd_files))
all_files <- md_files
file_type <- rep("non-quiz", length(md_files))
}
# Put all files in one data.frame
all_files <- data.frame(
Expand Down
53 changes: 25 additions & 28 deletions R/set_up.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ copy_quizzes <- function(quiz_dir = "quizzes", output_dir = "manuscript") {
#' set this to NULL
#' @param footer_text Optionally can add a bit of text that will be added to the
#' end of each file before the references section.
#'
#' @param embed is this being run by bookdown_to_embed_leanpub? TRUE/FALSE
#' @return A list of output files and diagnostics
#' @export
#'
Expand All @@ -194,12 +194,18 @@ 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 && dir.exists(output_dir)) {
if (clean_up) {
message(paste("Clearing out old version of output files:", output_dir))
file.remove(output_dir, recursive = TRUE)
dir.create(output_dir, showWarnings = FALSE)

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
Expand Down Expand Up @@ -244,41 +250,32 @@ set_up_leanpub <- function(path = ".",
clean_envir = FALSE
)
}

# may be irrelevant since copy_docs does everything
if (verbose) {
message("Copying Resources")
}
copy_resources(path, output_dir = output_dir)

if (verbose) {
message("Copying Docs folder")
}

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

copy_bib(path, output_dir = output_dir)
if (verbose) {
message("Copying bib files")

# 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)

if (verbose) message("Copying bib files")
copy_bib(path, output_dir = output_dir)
}

if (!is.null(quiz_dir)) {
#### Run quiz checks
if (run_quiz_checks) {
message("Checking quizzes")
quiz_checks <- check_quizzes(quiz_dir,
quiz_checks <- check_quizzes(
quiz_dir,
verbose = verbose
)
}
if (verbose) message("Copying quiz files")
copy_quizzes(
quiz_dir = quiz_dir,
output_dir = output_dir
)
if (verbose) {
message("Copying quiz files")
}
}
}
3 changes: 3 additions & 0 deletions man/bookdown_to_book_txt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/bookdown_to_embed_leanpub.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/bookdown_to_leanpub.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_chapters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/set_up_leanpub.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfe67cc

Please sign in to comment.