diff --git a/R/aaa_utils.R b/R/aaa_utils.R index 974f7f8..4ec6b1d 100644 --- a/R/aaa_utils.R +++ b/R/aaa_utils.R @@ -215,3 +215,15 @@ download_png_urls <- function(urls) { }, FUN.VALUE = character(1)) return(res) } + + +add_footer <- function(rmd_path, footer_text = NULL) { + + if (is.null(footer_text)) { + stop("Need character string in footer_text argument to append to end of file.") + } + + write(as.character(footer_text), + file = rmd_path, + append = TRUE) +} diff --git a/R/bookdown_to_leanpub.R b/R/bookdown_to_leanpub.R index c8c97df..20e3d31 100644 --- a/R/bookdown_to_leanpub.R +++ b/R/bookdown_to_leanpub.R @@ -45,14 +45,6 @@ bookdown_destination <- function(path = ".") { full_output_dir } -# copy_md_files = function(path = ".") { -# path = bookdown_path(path) -# files = bookdown_rmd_files(path) -# files = sub(rmd_regex, ".md", files) -# files = file.path(bookdown_destination(path), files) -# -# } - copy_directory_contents <- function(from, to) { x <- list.files( path = from, full.names = TRUE, all.files = TRUE, @@ -100,6 +92,8 @@ copy_bib <- function(path = ".", output_dir = "manuscript") { #' of any image path. #' @param make_book_txt Should [leanbuild::bookdown_to_book_txt()] be run #' to create a `Book.txt` in the output directory? +#' @param footer_text Optionally can add a bit of text that will be added to the +#' end of each file before the references section. #' #' @return A list of output files and diagnostics #' @export @@ -109,7 +103,8 @@ bookdown_to_leanpub <- function(path = ".", output_dir = "manuscript", make_book_txt = TRUE, remove_resources_start = FALSE, - verbose = TRUE) { + verbose = TRUE, + footer_text = NULL) { rmd_regex <- "[.][R|r]md$" path <- bookdown_path(path) @@ -159,25 +154,6 @@ bookdown_to_leanpub <- function(path = ".", for (file in md_files) { - # if (render) { - # rmarkdown::render( - # file, - # output_file = output_file, - # output_dir = output_dir, - # intermediates_dir = output_dir, - # envir = run_env, - # output_format = rmarkdown::output_format( - # knitr = rmarkdown::knitr_options( - # opts_chunk = list(fig.path = "resources/images/") - # ), - # pandoc = rmarkdown::pandoc_options( - # # to = "markdown_strict+autolink_bare_uris+tex_math_single_backslash", - # to = "gfm+autolink_bare_uris", - # args = c(pandoc_args, "--citeproc") - # ) - # ) - # ) - # } if (verbose > 1) { message("Replacing HTML for ", file) } @@ -185,7 +161,8 @@ bookdown_to_leanpub <- function(path = ".", infile <- replace_single_html(infile, verbose = verbose > 1, - remove_resources_start = remove_resources_start + remove_resources_start = remove_resources_start, + footer_text = footer_text ) if (length(bib_files) > 0) { @@ -196,14 +173,6 @@ bookdown_to_leanpub <- function(path = ".", con = infile, sep = "\n" ) } - # rmarkdown::pandoc_convert( - # input = infile, - # output = infile, - # options = pandoc_args, - # to = "markdown_strict+autolink_bare_uris+tex_math_single_backslash", - # # to = "markdown_strict+autolink_bare_uris+tex_math_single_backslash", - # citeproc = TRUE, - # verbose = verbose) } out <- NULL if (make_book_txt) { diff --git a/R/replace_html.R b/R/replace_html.R index ea42096..be23b95 100644 --- a/R/replace_html.R +++ b/R/replace_html.R @@ -423,21 +423,26 @@ replace_image_data <- function(x, element = c("img", "iframe"), fullbleed = FALS #' @param fullbleed should the image have the attribute `fullbleed: true`? #' @param remove_resources_start remove the word `resources/` at the front #' of any image path. +#' @param footer_text a bit of text that will be added to the +#' end of each file before the references section. #' @param verbose print diagnostic messages #' #' @return A list of output files and diagnostics #' @export replace_html <- function(path = "manuscript", remove_resources_start = TRUE, + footer_text = NULL, fullbleed = FALSE, verbose = TRUE) { md_files <- list.files( path = path, pattern = "[.]md$", ignore.case = TRUE, full.names = TRUE ) + md_files <- lapply(md_files, replace_single_html, fullbleed = fullbleed, - verbose = verbose + verbose = verbose, + footer_text = footer_text ) return(md_files) } @@ -446,6 +451,7 @@ replace_html <- function(path = "manuscript", #' @export #' @rdname replace_html replace_single_html <- function(file, + footer_text = NULL, remove_resources_start = TRUE, fullbleed = FALSE, verbose = TRUE) { stopifnot(length(file) == 1 && file.exists(file)) @@ -473,8 +479,14 @@ replace_single_html <- function(file, element = "iframe", fullbleed = fullbleed, remove_resources_start = remove_resources_start ) - + # need to actually do changes writeLines(x, con = file) + + # Add on footer if it was given + if (!is.null(footer_text)) { + add_footer(rmd_path = file, + footer_text) + } return(file) } diff --git a/man/bookdown_to_leanpub.Rd b/man/bookdown_to_leanpub.Rd index ce49853..8f80801 100644 --- a/man/bookdown_to_leanpub.Rd +++ b/man/bookdown_to_leanpub.Rd @@ -10,7 +10,8 @@ bookdown_to_leanpub( output_dir = "manuscript", make_book_txt = TRUE, remove_resources_start = FALSE, - verbose = TRUE + verbose = TRUE, + footer_text = NULL ) } \arguments{ @@ -28,6 +29,9 @@ to create a `Book.txt` in the output directory?} of any image path.} \item{verbose}{print diagnostic messages} + +\item{footer_text}{Optionally can add a bit of text that will be added to the +end of each file before the references section.} } \value{ A list of output files and diagnostics diff --git a/man/check_quizzes.Rd b/man/check_quizzes.Rd index fcc045b..713b66b 100644 --- a/man/check_quizzes.Rd +++ b/man/check_quizzes.Rd @@ -23,19 +23,21 @@ Check Quizzes } \examples{ -x = c('{quiz, id: quiz_00_filename}', -"### Lesson Name quiz", -"{choose-answers: 4}", -"? What do you think?", -"C) The answer to this one", -"o) Not the answer", -"o) Not the answer either", -"C) Another correct answer", -"m) Mandatory different answer", -"{/quiz}") -tdir = tempfile() +x <- c( + "{quiz, id: quiz_00_filename}", + "### Lesson Name quiz", + "{choose-answers: 4}", + "? What do you think?", + "C) The answer to this one", + "o) Not the answer", + "o) Not the answer either", + "C) Another correct answer", + "m) Mandatory different answer", + "{/quiz}" +) +tdir <- tempfile() dir.create(tdir, showWarnings = FALSE, recursive = TRUE) -tfile = tempfile(pattern = "quiz_", fileext = ".md", tmpdir = tdir) +tfile <- tempfile(pattern = "quiz_", fileext = ".md", tmpdir = tdir) writeLines(x, tfile) check_quizzes(path = tdir) diff --git a/man/gs_png_url.Rd b/man/gs_png_url.Rd index b5a5da4..c387cff 100644 --- a/man/gs_png_url.Rd +++ b/man/gs_png_url.Rd @@ -37,9 +37,11 @@ A character vector of URLs Get Google Slide PNG URL } \examples{ -url = paste0("https://docs.google.com/presentation/d/", -"12DPZgPteQBwgal6kSPP58zhPhjZ7QSPZLe3NkA8M3eo/edit", -"#slide=id.gc8648f14c3_0_397&t=4") -id = ariExtra::get_slide_id(url) +url <- paste0( + "https://docs.google.com/presentation/d/", + "12DPZgPteQBwgal6kSPP58zhPhjZ7QSPZLe3NkA8M3eo/edit", + "#slide=id.gc8648f14c3_0_397&t=4" +) +id <- ariExtra::get_slide_id(url) gs_png_url(url) } diff --git a/man/gs_to_drive_pngs.Rd b/man/gs_to_drive_pngs.Rd index 5ed4b2b..fb8cd63 100644 --- a/man/gs_to_drive_pngs.Rd +++ b/man/gs_to_drive_pngs.Rd @@ -30,11 +30,13 @@ Convert Google Slides to PNGs and Upload to Drive folder } \examples{ \dontrun{ -url = paste0("https://docs.google.com/presentation/d/", -"1ywZbtFacZK0UIsnt2g-sheC9du_rw_7XZ1FX4rRt27M", -"/export/png?", -"id=1ywZbtFacZK0UIsnt2g-sheC9du_rw_7XZ1FX4rRt27M&pageid=g325fd519ca_0_5") +url <- paste0( + "https://docs.google.com/presentation/d/", + "1ywZbtFacZK0UIsnt2g-sheC9du_rw_7XZ1FX4rRt27M", + "/export/png?", + "id=1ywZbtFacZK0UIsnt2g-sheC9du_rw_7XZ1FX4rRt27M&pageid=g325fd519ca_0_5" +) -out = gs_to_drive_pngs(url) +out <- gs_to_drive_pngs(url) } } diff --git a/man/parse_quiz.Rd b/man/parse_quiz.Rd index 1083a48..b6f02a3 100644 --- a/man/parse_quiz.Rd +++ b/man/parse_quiz.Rd @@ -39,44 +39,49 @@ Check Quiz Information } \examples{ -x = c('{quiz, id: quiz_00_filename}', -"### Lesson Name quiz", -"{choose-answers: 4}", -"? What do you think?", -"", -"C) The answer to this one", -"o) Not the answer", -"o) Not the answer either", -"C) Another correct answer", -"m) Mandatory different answer", -"", -"{/quiz}") -out = parse_quiz(x) +x <- c( + "{quiz, id: quiz_00_filename}", + "### Lesson Name quiz", + "{choose-answers: 4}", + "? What do you think?", + "", + "C) The answer to this one", + "o) Not the answer", + "o) Not the answer either", + "C) Another correct answer", + "m) Mandatory different answer", + "", + "{/quiz}" +) +out <- parse_quiz(x) check_quiz_attributes(out) -x = c('{quiz, id: quiz_00_filename, choose-answers: 4}', -"### Lesson Name quiz", -"{choose-answers: 4, attempts: 25}", -"? What do you think?", -"C) The answer to this one", -"o) Not the answer", -"o) Not the answer either", -"C) Another correct answer", -"m) Mandatory different answer", -"{/quiz}") -out = parse_quiz(x) +x <- c( + "{quiz, id: quiz_00_filename, choose-answers: 4}", + "### Lesson Name quiz", + "{choose-answers: 4, attempts: 25}", + "? What do you think?", + "C) The answer to this one", + "o) Not the answer", + "o) Not the answer either", + "C) Another correct answer", + "m) Mandatory different answer", + "{/quiz}" +) +out <- parse_quiz(x) check_quiz_attributes(out) check_quiz_question_attributes(out) -x = c('{quiz, id: quiz_00_filename, choose-answers: 4}', -"### Lesson Name quiz", -"{choose-answers: 4, attempts: 25}", -"", -"? What do you think?", -"! The answer to this one", -"{/quiz}") -out = parse_quiz(x) +x <- c( + "{quiz, id: quiz_00_filename, choose-answers: 4}", + "### Lesson Name quiz", + "{choose-answers: 4, attempts: 25}", + "", + "? What do you think?", + "! The answer to this one", + "{/quiz}" +) +out <- parse_quiz(x) check_quiz_attributes(out) check_quiz_question_attributes(out) - } diff --git a/man/remove_yaml_header.Rd b/man/remove_yaml_header.Rd index 9792770..fbfee87 100644 --- a/man/remove_yaml_header.Rd +++ b/man/remove_yaml_header.Rd @@ -16,7 +16,7 @@ A character vector of the text without the YAML header Remove YAML header } \examples{ -file = system.file("extdata/00_template.Rmd", package = "leanbuild") -out = remove_yaml_header(file) +file <- system.file("extdata/00_template.Rmd", package = "leanbuild") +out <- remove_yaml_header(file) head(out) } diff --git a/man/replace_html.Rd b/man/replace_html.Rd index 1fa0d10..8c594f6 100644 --- a/man/replace_html.Rd +++ b/man/replace_html.Rd @@ -8,12 +8,14 @@ replace_html( path = "manuscript", remove_resources_start = TRUE, + footer_text = NULL, fullbleed = FALSE, verbose = TRUE ) replace_single_html( file, + footer_text = NULL, remove_resources_start = TRUE, fullbleed = FALSE, verbose = TRUE @@ -25,6 +27,9 @@ replace_single_html( \item{remove_resources_start}{remove the word `resources/` at the front of any image path.} +\item{footer_text}{a bit of text that will be added to the +end of each file before the references section.} + \item{fullbleed}{should the image have the attribute `fullbleed: true`?} \item{verbose}{print diagnostic messages}