Skip to content

Commit

Permalink
fix: with body_add_docx(), basename can not contain ' '
Browse files Browse the repository at this point in the history
fix #572
  • Loading branch information
davidgohel committed Aug 27, 2024
1 parent 626f18f commit a6d4064
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: officer
Title: Manipulation of Microsoft Word and PowerPoint Documents
Version: 0.6.7.004
Version: 0.6.7.005
Authors@R: c(
person("David", "Gohel", , "[email protected]", role = c("aut", "cre")),
person("Stefan", "Moog", , "[email protected]", role = "aut"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- store paddings as numeric values and not integer values.
- remove_fields in `docx_summary()` now also removes "w:fldData" nodes.
- complete the manual of `body_add_docx()` with a note about the file basename
that can not contain ' ' and trigger an error if it contains a ' '.

## Features

Expand Down
14 changes: 12 additions & 2 deletions R/docx_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") {
#' @export
#' @title Add an external docx in a 'Word' document
#' @description Add content of a docx into an rdocx object.
#' @note
#'
#' The function is using a 'Microsoft Word' feature: when the
#' document will be edited, the content of the file will be
#' inserted in the main document.
#'
#' This feature is unlikely to work as expected if the
#' resulting document is edited by another software.
#'
#' The file is added when the method `print()` that
#' produces the final Word file is called, so don't remove
#' file defined with `src` before.
#' @inheritParams body_add_break
#' @param src docx filename
#' @param src docx filename, the path of the file must not contain
#' any '&' and the basename must not contain any space.
#' @examples
#' file1 <- tempfile(fileext = ".docx")
#' file2 <- tempfile(fileext = ".docx")
Expand All @@ -92,6 +97,11 @@ body_add_img <- function(x, src, style = NULL, width, height, pos = "after") {
#' @export
#' @family functions for adding content
body_add_docx <- function(x, src, pos = "after") {

if(grepl(" ", basename(src))){
stop("The basename of the file to be inserted cannot contain spaces.")
}

xml_elt <- to_wml(block_pour_docx(file = src), add_ns = TRUE)
body_add_xml(x = x, str = xml_elt, pos = pos)
}
Expand Down
5 changes: 4 additions & 1 deletion R/ooxml_block_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ block_pour_docx <- function(file){
stop("file {", file, "} is not a docx file.", call. = FALSE)
}
if(grepl("&", file, ignore.case = TRUE)){
stop("file path {", file, "} contains '&', please rename your file to use it with `block_pour_docx()`.", call. = FALSE)
stop("file path {", file, "} contains '&', please rename your file.", call. = FALSE)
}
if(grepl(" ", basename(file), ignore.case = TRUE)){
stop("file path {", basename(file), "} contains ' ', please rename your file.", call. = FALSE)
}

z <- list(file = file)
Expand Down
10 changes: 7 additions & 3 deletions man/body_add_docx.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6d4064

Please sign in to comment.