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 footer function! #18

Merged
merged 2 commits into from
Aug 27, 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
12 changes: 12 additions & 0 deletions R/aaa_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
43 changes: 6 additions & 37 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -159,33 +154,15 @@ 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)
}
infile <- normalizePath(file)

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) {
Expand All @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions R/replace_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}
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.

26 changes: 14 additions & 12 deletions man/check_quizzes.Rd

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

10 changes: 6 additions & 4 deletions man/gs_png_url.Rd

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

12 changes: 7 additions & 5 deletions man/gs_to_drive_pngs.Rd

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

71 changes: 38 additions & 33 deletions man/parse_quiz.Rd

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

4 changes: 2 additions & 2 deletions man/remove_yaml_header.Rd

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

5 changes: 5 additions & 0 deletions man/replace_html.Rd

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