Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to remove unused media files #571

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion R/openxml_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,30 @@ openxml_document <- R6Class(
},
relationship = function(){
private$rels_doc
}
},
clean_media = function() {
# get package directory
package_dir <- dirname(dirname(dirname(private$rels_filename)))
# find .xml.rels files
rel_files <- list.files(package_dir, pattern = "\\.xml.rels$", recursive = TRUE, full.names = TRUE)

# collect media files that are referenced in .xml.rels files
media_file_rels <- lapply(rel_files, function(xml){
zz <- xml2::read_xml(xml)
rels <- xml2::xml_children(zz)
targets <- xml2::xml_attr(rels, "Target")
grep("^\\.\\./media/", targets, value = TRUE)
})
media_file_rels <- file.path(dirname(dirname(private$rels_filename)), sub("\\.\\./", "", unique(unlist(media_file_rels))))

# get files in media folder
media_folder <- grep("/media$", list.dirs(package_dir), value = TRUE)
media_files <- list.files(media_folder, recursive = TRUE, full.names = TRUE)

# delete unused files
files_to_delete <- media_files[!(media_files %in% media_file_rels)]
unlink(files_to_delete, force = TRUE)
}
),
private = list(

Expand Down
1 change: 1 addition & 0 deletions R/ppt_classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ presentation <- R6Class(
private$slide_rid <- private$slide_rid[-dropid]

private$update_xml()
self$clean_media()

self
}
Expand Down
11 changes: 1 addition & 10 deletions R/pptx_slide_manip.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ on_slide <- function(x, index) {
#' @description Remove a slide from a pptx presentation.
#' @param x an rpptx object
#' @param index slide index, default to current slide position.
#' @param rm_images if TRUE (defaults to FALSE), images presented in
#' the slide to remove are also removed from the file.
#' @note cursor is set on the last slide.
#' @examples
#' my_pres <- read_pptx()
#' my_pres <- add_slide(my_pres)
#' my_pres <- remove_slide(my_pres)
#' @family functions slide manipulation
#' @seealso [read_pptx()], [ph_with()], [ph_remove()]
remove_slide <- function(x, index = NULL, rm_images = FALSE) {
remove_slide <- function(x, index = NULL) {
l_ <- length(x)
if (l_ < 1) {
stop("presentation contains no slide to delete", call. = FALSE)
Expand All @@ -113,13 +111,6 @@ remove_slide <- function(x, index = NULL, rm_images = FALSE) {
filename <- basename(x$presentation$slide_data()$target[index])
location <- which(x$slide$get_metadata()$name %in% filename)

if (rm_images) {
media_files <- pptx_summary(x)$media_file
if (length(media_files)) {
unlink(file.path(x$package_dir, media_files), force = TRUE)
}
}

del_file <- x$slide$remove_slide(location)

# update presentation elements
Expand Down
Loading