Skip to content
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
8 changes: 6 additions & 2 deletions R/gtsave.R
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,18 @@ gt_save_docx <- function(
con = word_md_file
)

temp_filename <- tempfile(fileext = paste0(".",tools::file_ext(filename)))
rmarkdown::pandoc_convert(
input = word_md_file,
output = filename
output = temp_filename,
)

if (needs_gt_as_word_post_processing(word_md_text)) {
gt_as_word_post_processing(path = filename)
gt_as_word_post_processing(path = temp_filename)
}

file.rename(temp_filename, filename)

}

#' Get the lowercase extension from a filename
Expand Down
19 changes: 16 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ markdown_to_xml <- function(text) {
USE.NAMES = FALSE,
FUN = function(x, ...) commonmark::markdown_xml(linebreak_br(x), ...)
)

vapply(
res,
FUN.VALUE = character(1L),
Expand Down Expand Up @@ -1225,9 +1226,21 @@ markdown_to_xml <- function(text) {
}
}

res <- lapply(children, apply_rules)
res <- vapply(res, FUN = as.character, FUN.VALUE = character(1L))
res <- paste0(res, collapse = "")
if(length(children)){
res <- lapply(children, apply_rules)
res <- vapply(res, FUN = as.character, FUN.VALUE = character(1L))
res <- paste0(res, collapse = "")
}else{
res <- xml_p(
xml_pPr(
xml_spacing(before = 0, after = 60)
),
xml_r(
xml_rPr(),
xml_t()
)
)
}
paste0("<md_container>", res, "</md_container>")
}
)
Expand Down
2 changes: 1 addition & 1 deletion R/utils_render_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -2958,7 +2958,7 @@ parse_to_xml <- function(x, ...) {
##check if wrapped in ooxml
## get what it starts with and assign

if (is.null(x)) {
if (is.null(x) | identical(x,"<md_container></md_container>")) {
x <-
xml_p(
xml_pPr(
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-as_word.R
Original file line number Diff line number Diff line change
Expand Up @@ -2835,3 +2835,33 @@ test_that("multicolumn stub are supported", {
expect_equal(xml_attr(xml_find_all(tcPr[[1]], ".//w:gridSpan"), "val"), "3")

})

test_that("word ooxml handles empty markdown cells gt object", {

# Create a one-row table for these tests
gt_empty_md <- exibble[1, 1:2] |>
dplyr::mutate(num = " ") |>
gt() |>
cols_label(
num = md(" "),
char = md("char")
) |>
fmt_markdown()

## convert to word xml nodeset
xml <- gt_empty_md %>% as_word() %>% xml2::read_xml()

table_cells <- xml_find_all(xml, ".//w:tc//w:t")

expect_equal(
as.character(table_cells),
c(
"<w:t xml:space=\"default\"/>",
"<w:t xml:space=\"preserve\">char</w:t>",
"<w:t xml:space=\"default\"/>",
"<w:t xml:space=\"preserve\">apricot</w:t>"
)
)

})