r
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4KB

  1. # Functions in this file should probably be migrated to the `officer` package
  2. embed_files <- function(docx, files, internal_dir = NULL) {
  3. for (file in files) {
  4. if (!file.exists(file)) next
  5. if (file.info(file)$isdir) {
  6. embed_files(docx, list.files(file, full.names = TRUE),
  7. internal_dir = do.call(
  8. file.path,
  9. as.list(c(internal_dir, basename(file)))
  10. )
  11. )
  12. } else {
  13. embed_file(docx, file, internal_dir = internal_dir)
  14. }
  15. }
  16. return(docx)
  17. }
  18. #' @importFrom mime guess_type
  19. embed_file <- function(docx, file, content_type = guess_type(file),
  20. internal_dir = NULL) {
  21. if (!is.null(internal_dir) &&
  22. !dir.exists(file.path(docx$package_dir, internal_dir))) {
  23. dir.create(file.path(docx$package_dir, internal_dir), recursive = TRUE)
  24. }
  25. file.copy(file,
  26. to = do.call(
  27. file.path,
  28. as.list(
  29. c(docx$package_dir, internal_dir, basename(file))
  30. )
  31. )
  32. )
  33. extension <- tools::file_ext(file)
  34. docx$content_type$add_ext(extension, content_type)
  35. # docx$content_type$save()
  36. rel <- docx$doc_obj$relationship()
  37. new_rid <- sprintf("rId%.0f", rel$get_next_id())
  38. rel$add(
  39. id = new_rid,
  40. type = paste0(
  41. "http://schemas.openxmlformats.org/officeDocument/2006/relationships/",
  42. extension
  43. ),
  44. target = file.path("..", file.path(internal_dir, basename(file)))
  45. )
  46. }