R/addins.R

Defines functions dedoc_to_new_file dedoc_to_active_file roundtrip_active_file

Documented in dedoc_to_active_file dedoc_to_new_file roundtrip_active_file

#' RStudio Addin Functions
#'
#' @description
#' These functions act on the RStudio text editor and can be used via the
#' Addins menu or the R console. `rountrip_active_file()` ("Render and Update")
#' knits the current file using [redoc()] replaces the text with text that has
#' been round-tripped to Word and back. This helps eliminate small formatting
#' differences.
#'
#' The `dedoc_*` functions de-render Word documents (using [dedoc()]),
#' and place the results in the current or new file.  By default, they will
#' also display a diff (generated via [redoc_diff()]) of edits in the word file
#' compared to its original version a compilation time. If called without
#' an input file, they will prompt for file selection via the RStudio GUI.
#'
#'
#' @param docx The input Word file, originally generated by [redoc()]
#' @param showdiff Display a diff of the current version of the document against
#'   the rendered version?
#' @aliases addins redoc_addins
#' @rdname addins
#' @export
roundtrip_active_file <- function() {
  if (!requireNamespace("rstudioapi")) {
    stop("The 'rstudioapi' package is required for this function")
  }
  active_file <- rstudioapi::getSourceEditorContext()
  cursor_position <- active_file$selection[[1]]$range$start
  rstudioapi::documentSave(active_file$id)
  docfile <- rmarkdown::render(
    normalizePath(active_file$path),
    output_format = redoc(roundtrip = TRUE),
    quiet = TRUE,
    clean = TRUE
  )
  rfile <- redoc_extract_rmd(docfile,
    type = "roundtrip", dir = tempdir(),
    overwrite = TRUE
  )
  rstudioapi::setDocumentContents(readfile(rfile), active_file$id)
  rstudioapi::setCursorPosition(cursor_position, active_file$id)
}

#' @export
#' @rdname addins
dedoc_to_active_file <- function(docx = NULL, showdiff = TRUE) {
  if (!requireNamespace("rstudioapi")) {
    stop("The 'rstudioapi' package is required for this function")
  }

  active_file <- rstudioapi::getSourceEditorContext()
  cursor_position <- active_file$selection[[1]]$range$start
  if (is.null(docx)) {
    docx <- rstudioapi::selectFile(
      caption = "Select Word file to dedoc",
      filter = "Word Files (*.docx)"
    )
  }
  tmprmd <- dedoc(docx, dir = tempdir(), overwrite = TRUE)
  rstudioapi::setDocumentContents(readfile(tmprmd), active_file$id)
  rstudioapi::setCursorPosition(cursor_position, active_file$id)
  if (showdiff) {
    print(
      redoc_diff(docx)
    )
  }
}

#' @export
#' @rdname addins
dedoc_to_new_file <- function(docx = NULL, showdiff = TRUE) {
  if (!requireNamespace("rstudioapi")) {
    stop("The 'rstudioapi' package is required for this function")
  }
  if (is.null(docx)) {
    docx <- rstudioapi::selectFile(
      caption = "Select Word file to dedoc",
      filter = "Word Files (*.docx)"
    )
  }
  tmprmd <- dedoc(docx, dir = tempdir(), overwrite = TRUE)
  rstudioapi::documentNew(readfile(tmprmd), type = "rmarkdown")
  if (showdiff) {
    print(
      redoc_diff(docx)
    )
  }
}
noamross/redoc documentation built on Aug. 7, 2022, 7:22 a.m.