5  prerender_book()

Diagram

Code
prerender_book <- function(my_files, out_name = "quarto", ext_name = "document-fn", book_yaml_lines = "book:", hide = NULL) {

  ## Tests ---------

  ### Test that Quarto book template has not been pulled yet -------
1  tmp0 <- out_name
  assertthat::assert_that(
    !dir.exists(tmp0),
    msg = "Output folder already exists. Overwrite disabled."
  )

  ### Test that all files to be documented exist -------
  qc0 <- my_files[which(!file.exists(my_files))]
  assertthat::assert_that(
    length(qc0) == 0,
    msg = paste0("The following files do not exist:\n\t", paste0(qc0,collapse="\n\t"))
  )

  ## Side effect -- prepare Quarto book documenting the functions --------

  ### Pull quarto template ----------
2  use_quarto_ext(out_name = out_name, ext_name = ext_name)
  dir.create(here(out_name, "results"))

  ### Insert README.Rmd into index.qmd ----------
  assertthat::assert_that(
    file.exists(here::here("README.Rmd")),
    msg = "README.Rmd must exist at root level of project."
  )
  write("\n\n{{< include ../README.Rmd >}}",
        file = here::here(tmp0, "index.qmd"),
        append = TRUE)

  ### Platenize each function in the files ----------
3  my_fns <- c()
  for(my_file in my_files) {
    tmp2 <- readr::read_lines(my_file)
    tmp3 <- c(0, which(tmp2 == "}"))
    if (length(tmp3) == 2) {
      my_fn_txts <- list(tmp2)
    } else if (length(tmp3) > 2) {
      my_fn_txts <- purrr::map(1:(length(tmp3)-1), ~tmp2[(tmp3[.x]+1):tmp3[.x+1]])
    }
    for(my_fn_txt in my_fn_txts) {
      tmp4 <- platenize(my_fn_txt = my_fn_txt, out_name = out_name, hide = hide)$fn_name
      my_fns[tmp4] <- tmp4
    }
  }

  ### Edit the YAML for the Quarto book to include a chapter for each function ----------
4  yaml_in <- readr::read_lines(here::here(tmp0, "_quarto.yml"))
  tmp1 <- which(stringr::str_equal(yaml_in, "book:"))
  tmp2 <- which(stringr::str_equal(yaml_in, "    - index.qmd"))
  yaml_out <- c(
    yaml_in[1:(tmp1-1)],
    book_yaml_lines,
    yaml_in[(tmp1+1):tmp2],
    paste0("    - results/", my_fns, "/results.qmd"),
    yaml_in[(tmp2 + 1):length(yaml_in)]
  )
  yaml_out %>% readr::write_lines(here::here(tmp0, "_quarto.yml"))

  ## Return path to Quarto book project -----------
5  return(tmp0)
}
1
First step
2
Second step
3
Third step
4
Fourth step
5
Fifth step