R/diff.R

Defines functions redoc_diff

Documented in redoc_diff

#' Compare changes in an edited redoc Word Document against the original
#'
#' `redoc_diff()` produces a diff object comparing the current contents of a Word
#' document originally generated by [redoc()] to the original R markdown file
#' used to create it.
#'
#' When an `.docx` file is created with `redoc()`, it internally stores the
#' original R Markdown file as well as a version that is round-tripped to
#' `.docx` and back. `redoc_diff()` de-renders the current `.docx` to R
#' Markdown (with [dedoc()]) and compares against these versions.
#'
#' @param docx Path to an MS Word `.docx` file originally generated with
#'   `redoc()` and since edited.
#' @param target,current Which versions of the document to compare. One of
#' "original", "roundtrip", or "current".
#' @param track_changes,block_missing,inline_missing Arguments passed to
#'   [dedoc()] to determine how to handle edits in the Word document.
#' @param wrap Width to wrap text lines when converting from docx to markdown.
#'   If `NULL`, no wrapping. Set the default with `"redoc.wrap"` in `options()`.
#' @param mode,context,tar.banner,cur.banner,... Arguments passed to
#'   [diffobj::diffFile()] to customize printing of the diff.
#' @return A [`Diff`][diffobj::diffPrint()] object, which will be displayed in the RStudio
#'   Viewer, a browser, or the console, depending on the context.
#'
#' @importFrom diffobj diffFile
#' @export
redoc_diff <- function(docx,
                       target = "original",
                       current = "current",
                       track_changes = "comments_only",
                       block_missing = "comment",
                       inline_missing = "omit",
                       wrap = getOption("redoc.wrap", 80),
                       mode = "sidebyside", context = "auto",
                       tar.banner = NULL, cur.banner = NULL,
                       ...) {
  stopifnot(target %in% c("original", "roundtrip", "current"))
  stopifnot(current %in% c("original", "roundtrip", "current"))

  if (!is_redoc(docx)) stop("Word file not generated by redoc")

  tmpd <- tempdir()
  comps <- lapply(c(target, current), function(x) {
    switch(x,
      original = redoc_extract_rmd(docx,
        type = "original", dir = tmpd,
        overwrite = TRUE
      ),
      roundtrip = redoc_extract_rmd(docx,
        type = "roundtrip", dir = tmpd,
        overwrite = TRUE
      ),
      current = dedoc(docx,
        to = "current.Rmd", dir = tmpd,
        track_changes = track_changes,
        inline_missing = inline_missing,
        wrap = wrap, overwrite = TRUE
      )
    )
  })

  labs <- lapply(c(target, current), function(x) {
    switch(x,
      original = "Original R Markdown",
      roundtrip = "Original R Markdown (roundtripped)",
      current = "Current Word Document"
    )
  })

  if (is.null(tar.banner)) tar.banner <- labs[[1]]
  if (is.null(tar.banner)) tar.banner <- labs[[2]]

  diff <- diffFile(
    target = comps[[1]], comps[[2]],
    mode = mode, context = context,
    tar.banner = labs[[1]], cur.banner = labs[[2]],
    pager = list(file.path = tempfile(fileext = ".html")),
    ...
  )

  return(diff)
}
noamross/redoc documentation built on Aug. 7, 2022, 7:22 a.m.