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

Add a first vignette! #47

Merged
merged 22 commits into from
Sep 15, 2021
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
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ Imports: googledrive,
jsonlite,
utils,
tibble,
rmarkdown,
rmarkdown (>= 2.10),
xml2,
rvest,
readr,
R.utils,
bookdown,
glue,
curl,
rprojroot,
magrittr,
yaml,
stringr
stringr,
knitr (>= 1.33),
qpdf
Suggests:
knitr,
remotes,
testthat
Encoding: UTF-8
LazyData: true
Expand Down
7 changes: 6 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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_quiz)
export(get_bookdown_spec)
Expand All @@ -33,6 +35,7 @@ export(gs_png_url)
export(gs_to_drive_pngs)
export(include_slide)
export(leanpub_check)
export(parse_q_tag)
export(parse_quiz)
export(parse_quiz_df)
export(remove_yaml_header)
Expand All @@ -42,4 +45,6 @@ export(replace_single_html)
export(set_knitr_image_path)
export(simple_references)
importFrom(magrittr,"%>%")
importFrom(utils,write.csv)
importFrom(readr,write_tsv)
importFrom(utils,download.file)
importFrom(utils,unzip)
32 changes: 22 additions & 10 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ get_bookdown_spec <- function(path = ".") {
#' @export

bookdown_path <- function(path = ".") {
path <- rprojroot::find_root(rprojroot::has_file("_bookdown.yml"), path = path)

# See what unzip is being used
operating_system <- Sys.info()[1]

path <- rprojroot::find_root(rprojroot::has_file("_bookdown.yml"), path = file.path(path))

return(path)
}
Expand All @@ -40,7 +44,7 @@ bookdown_path <- function(path = ".") {
#' @export
#'
bookdown_file <- function(path = ".") {
root_dir <- bookdown_path(path = path)
root_dir <- bookdown_path(path = file.path(path))
file_path <- file.path(root_dir, "_bookdown.yml")

return(file_path)
Expand All @@ -54,15 +58,15 @@ bookdown_file <- function(path = ".") {
#' @export
#'
bookdown_rmd_files <- function(path = ".") {
spec <- get_bookdown_spec(path)
spec <- get_bookdown_spec(file.path(path))

files <- spec$rmd_files
if (is.null(files) || all(is.na(files)) || length(files) == 0) {
warning(
"No bookdown specification of the files, using ",
"list.files(pattern ='.Rmd')"
)
root_dir <- bookdown_path(path = path)
root_dir <- bookdown_path(path = file.path(path))
files <- list.files(
pattern = "[.]Rmd", ignore.case = TRUE,
path = root_dir, full.names = FALSE
Expand All @@ -81,10 +85,10 @@ bookdown_rmd_files <- function(path = ".") {
bookdown_destination <- function(path = ".") {

# Find _bookdown.yml
root_dir <- bookdown_path(path = path)
root_dir <- bookdown_path(path = file.path(path))

# Get specs from _bookdown.yml
spec <- get_bookdown_spec(path = path)
spec <- get_bookdown_spec(path = file.path(path))

# Find output directory declared in the bookdown.yml
output_dir <- spec$output_dir
Expand Down Expand Up @@ -131,14 +135,14 @@ copy_resources <- function(path = ".",

copy_docs <- function(path = ".", output_dir = "manuscript") {
path <- bookdown_destination(path)
R.utils::copyDirectory(path, output_dir, recursive = TRUE, overwrite = TRUE)
R.utils::copyDirectory(path, file.path(output_dir), recursive = TRUE, overwrite = TRUE)
}

copy_bib <- function(path = ".", output_dir = "manuscript") {
path <- bookdown_path(path)
files <- list.files(path = path, full.names = TRUE, pattern = ".bib$")
if (length(files) > 0) {
file.copy(files, output_dir, overwrite = TRUE)
file.copy(files, file.path(output_dir), overwrite = TRUE)
}
}

Expand Down Expand Up @@ -204,8 +208,12 @@ bookdown_to_leanpub <- function(path = ".",
}
message(paste("index_file is", index_file))

output_format <- bookdown::gitbook(pandoc_args = "--citeproc")
output_format$pandoc$args <- c(output_format$pandoc$args, "--citeproc")
if (rmarkdown::pandoc_version() > 2.11) {
output_format <- bookdown::gitbook(pandoc_args = "--citeproc")
output_format$pandoc$args <- c(output_format$pandoc$args, "--citeproc")
} else {
output_format = NULL
}
bookdown::render_book(
input = index_file,
output_format = output_format,
Expand Down Expand Up @@ -309,6 +317,7 @@ bookdown_to_leanpub <- function(path = ".",
#'
#' @return A list of quiz and chapter files in order in a file called Book.txt -- How Leanpub wants it.
#' @export
#'
bookdown_to_book_txt <- function(path = ".",
output_dir = "manuscript",
quiz_dir = "quizzes",
Expand Down Expand Up @@ -353,6 +362,9 @@ bookdown_to_book_txt <- function(path = ".",
# Declare output file name
book_txt <- file.path(output_dir, "Book.txt")

if (verbose) {
message(paste0("Autogenerated Book.txt saved to: ", book_txt))
}
# need to fix about quiz
writeLines(all_files, con = book_txt)
}
114 changes: 114 additions & 0 deletions R/example_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# C. Savonen 2021

#' Path to good example quiz
#'
#' @export
#'
#' @examples
#'
#' quiz_path <- good_quiz_path()
good_quiz_path <- function() {
list.files(
pattern = "quiz_good.md$",
system.file("extdata", package = "leanbuild"),
full.names = TRUE
)
}

#' Path to bad example quiz
#'
#' @export
#'
#' @examples
#'
#' quiz_path <- bad_quiz_path()
bad_quiz_path <- function() {
list.files(
pattern = "quiz_bad.md$",
system.file("extdata", package = "leanbuild"),
full.names = TRUE
)
}

#' Set up example repo files
#'
#' @param dest_dir The destination directory you would like the example repo files to be placed. By default is current directory.
#' @export
#'
#' @importFrom utils download.file
#' @importFrom utils unzip
#' @examples
#'
#' example_repo_setup()
example_repo_setup <- function(dest_dir = ".") {

zip_file <- file.path(dest_dir, "example-repo.zip")

if (!dir.exists(dest_dir)) {
dir.create(dest_dir)
}
download.file(url = "https://github.com/jhudsl/DaSL_Course_Template_Leanpub/raw/main/example-repo.zip",
destfile = zip_file)

# See what unzip is being used
operating_system <- Sys.info()[1]
message(operating_system)
if (operating_system == "Windows" ){
# Unzip function doesn't always work for windows
system(paste("7z a -tzip", zip_file, dest_dir))
} else {
# Unzip the folder
utils::unzip(zip_file, exdir = dest_dir)
}
}

#' Clean up example repo files
#'
#' @param dir What directory to delete the example files from
#' @export
#'
#' @examples
#'
#' example_repo_cleanup()
#'
example_repo_cleanup <- function(dir = ".") {

file_list <- file.path(dir, "resources", "needed_leanbuild_files.txt")
if (file.exists(file_list)) {
file_list <- readLines(file.path(dir, "resources", "needed_leanbuild_files.txt"))
} else {
file_list <- NULL
}
# Find example folder file
files_to_remove <- c(
file_list,
"_bookdown.yml",
"_output.yml",
"01-intro.Rmd",
"02-chapter_of_course.Rmd",
"About.Rmd",
"assets",
"docs",
"example-repo.zip",
"index.Rmd",
"quizzes",
"resources",
"Course_Name.rds",
"manuscript",
"question_error_report.tsv",
"packages.bib")


message("Cleaning up and removing example repo files")

# Now remove it all
lapply(files_to_remove, function(file) {
if (file.exists(file) | dir.exists(file)) {
system(paste0("chmod +w -R", file))
system(paste0("sudo rm -r ", file))
}
})
}

# save(bad_quiz, bad_quiz, file = "bad_quiz.RData")
# save(good_quiz, good_quiz, file = "good_quiz.RData")
Loading