From 9e29e7663ab44676270289d63eac9714d2c98602 Mon Sep 17 00:00:00 2001 From: Patrice Lecharpentier Date: Mon, 12 Oct 2020 14:09:18 +0200 Subject: [PATCH] New function for building book part and current part open in editor ! --- R/generate_book.R | 39 +++++++++++++++++++++++++++++++++++++++ inst/rstudio/addins.dcf | 5 +++++ 2 files changed, 44 insertions(+) diff --git a/R/generate_book.R b/R/generate_book.R index bc9fe1d..198da02 100644 --- a/R/generate_book.R +++ b/R/generate_book.R @@ -87,3 +87,42 @@ get_output_dir <- function(dir = getwd()) { gsub(pattern = "output_dir: (.*)", x = l[idx], replacement = "\\1") } + + + +build_book_part <- function( file_name = NULL, other_names = c("index", "Appendices")){ + + # For a single file name + if (is.null(file_name)) { + in_doc <- get_active_doc() + } else { + in_doc <- file_name + } + + if(length(file_name) > 1) stop("Only one file name is required") + + if (!grepl(pattern = "\\.Rmd$", x = in_doc)) stop("Not a Rmd file") + + if( !file.exists(in_doc)) stop("File does not exist!") + + # check if this is a bookdown part + book_dir <- dirname(in_doc) + print(book_dir) + if(!file.exists(file.path(book_dir, "index.Rmd"))) stop("Not a bookdown part") + + # Getting appendices and references parts + other_files <- unlist(lapply(other_names , function(x) grep(pattern = x, x = list.files(book_dir, pattern = "\\.Rmd$"), value = TRUE))) + + + # Adding extra parts + if(length(other_files)) in_doc <- c(in_doc, other_files) + + + generate_book(input_rmd = unique(in_doc)) + +} + + +build_current_book_part <- function() { + build_book_part() +} diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index defc1c9..fbb47f0 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -2,3 +2,8 @@ Name: Find and insert Stics names Description: Searching names based on partial match, insert formatted name for RMarkdown. Binding: find_stics_names Interactive: true + +Name: Build current book part +Description: Buidling a bookdown part open in editor ... +Binding: build_current_book_part +Interactive: false