r
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

83 Zeilen
3.1KB

  1. #' Compare changes in an edited redoc Word Document against the original
  2. #'
  3. #' `redoc_diff()` produces a diff object comparing the current contents of a Word
  4. #' document originally generated by [redoc()] to the original R markdown file
  5. #' used to create it.
  6. #'
  7. #' When an `.docx` file is created with `redoc()`, it internally stores the
  8. #' original R Markdown file as well as a version that is round-tripped to
  9. #' `.docx` and back. `redoc_diff()` de-renders the current `.docx` to R
  10. #' Markdown (with [dedoc()]) and compares against these versions.
  11. #'
  12. #' @param docx Path to an MS Word `.docx` file originally generated with
  13. #' `redoc()` and since edited.
  14. #' @param target,current Which versions of the document to compare. One of
  15. #' "original", "roundtrip", or "current".
  16. #' @param track_changes,block_missing,inline_missing Arguments passed to
  17. #' [dedoc()] to determine how to handle edits in the Word document.
  18. #' @param wrap Width to wrap text lines when converting from docx to markdown.
  19. #' If `NULL`, no wrapping. Set the default with `"redoc.wrap"` in `options()`.
  20. #' @param mode,context,tar.banner,cur.banner,... Arguments passed to
  21. #' [diffobj::diffFile()] to customize printing of the diff.
  22. #' @return A [`Diff`][diffobj::diffPrint()] object, which will be displayed in the RStudio
  23. #' Viewer, a browser, or the console, depending on the context.
  24. #'
  25. #' @importFrom diffobj diffFile
  26. #' @export
  27. redoc_diff <- function(docx,
  28. target = "original",
  29. current = "current",
  30. track_changes = "comments_only",
  31. block_missing = "comment",
  32. inline_missing = "omit",
  33. wrap = getOption("redoc.wrap", 80),
  34. mode = "sidebyside", context = "auto",
  35. tar.banner = NULL, cur.banner = NULL,
  36. ...) {
  37. stopifnot(target %in% c("original", "roundtrip", "current"))
  38. stopifnot(current %in% c("original", "roundtrip", "current"))
  39. if (!is_redoc(docx)) stop("Word file not generated by redoc")
  40. tmpd <- tempdir()
  41. comps <- lapply(c(target, current), function(x) {
  42. switch(x,
  43. original = redoc_extract_rmd(docx,
  44. type = "original", dir = tmpd,
  45. overwrite = TRUE
  46. ),
  47. roundtrip = redoc_extract_rmd(docx,
  48. type = "roundtrip", dir = tmpd,
  49. overwrite = TRUE
  50. ),
  51. current = dedoc(docx,
  52. to = "current.Rmd", dir = tmpd,
  53. track_changes = track_changes,
  54. inline_missing = inline_missing,
  55. wrap = wrap, overwrite = TRUE
  56. )
  57. )
  58. })
  59. labs <- lapply(c(target, current), function(x) {
  60. switch(x,
  61. original = "Original R Markdown",
  62. roundtrip = "Original R Markdown (roundtripped)",
  63. current = "Current Word Document"
  64. )
  65. })
  66. if (is.null(tar.banner)) tar.banner <- labs[[1]]
  67. if (is.null(tar.banner)) tar.banner <- labs[[2]]
  68. diff <- diffFile(
  69. target = comps[[1]], comps[[2]],
  70. mode = mode, context = context,
  71. tar.banner = labs[[1]], cur.banner = labs[[2]],
  72. pager = list(file.path = tempfile(fileext = ".html")),
  73. ...
  74. )
  75. return(diff)
  76. }