From e3d465bf4eeefb4426709ddab3a4e97625ee694c Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Fri, 19 Jan 2024 18:41:04 -0500 Subject: [PATCH] word works --- R/build_tt.R | 6 ++++++ R/save_tt.R | 22 +++------------------- R/tt_grid.R | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/R/build_tt.R b/R/build_tt.R index 08609c13..b189cc9b 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -52,6 +52,12 @@ build_tt <- function(x, output = NULL) { } out <- meta(out, "output", output) + # formal grid specification in pandoc includes lines everywhere + hlines <- getOption("tinytable_grid_hlines", default = TRUE) + if (isTRUE(hlines) && output == "markdown") { + out <- grid_hlines(out) + } + m <- meta(x) m$lazy_style <- list() attr(out, "tinytable_meta") <- m diff --git a/R/save_tt.R b/R/save_tt.R index 0b40f7c6..33c580c9 100644 --- a/R/save_tt.R +++ b/R/save_tt.R @@ -98,11 +98,9 @@ save_tt <- function(x, output, overwrite = FALSE) { } else if (file_ext == "docx") { assert_dependency("pandoc") - tmp <- markdown_hlines(x) - fn <- file.path(tempdir(), "temp.md") - writeLines(x, fn) - writeLines(x, "~/Downloads/trash.md") - pandoc::pandoc_convert(file = fn, to = "docx", output = output) + # fn <- file.path(tempdir(), "temp.md") + # write(x, file = fn) + pandoc::pandoc_convert(text = x, to = "docx", output = output) } return(invisible(TRUE)) @@ -110,17 +108,3 @@ save_tt <- function(x, output, overwrite = FALSE) { } -# insert horizontal rules everywhere (important for word) -markdown_hlines <- function(x) { - rule_line <- grid_line(meta(x, "col_widths"), "-") - lines <- strsplit(x, split = "\\n")[[1]] - if (length(lines) > 1) { - for (idlines in length(lines):2) { - if (!startsWith(lines[idlines - 1], "+") && !startsWith(lines[idlines], "+")) { - lines <- c(lines[1:(idlines - 1)], rule_line, lines[idlines:length(lines)]) - } - } - } - out <- paste(lines, collapse = "\n") - return(out) -} diff --git a/R/tt_grid.R b/R/tt_grid.R index 4bd452f3..0ee63279 100755 --- a/R/tt_grid.R +++ b/R/tt_grid.R @@ -140,6 +140,7 @@ group_grid_row <- function(x, i, ...) { cw <- sum(cw) + length(cw) - 1 for (idx in rev(seq_along(i))) { tmp <- trimws(as.character(tt_grid(matrix(names(i)[idx]), col_widths = cw))) + tmp <- strsplit(tmp, split = "\\n")[[1]][2] mid <- c(mid[1:(i[idx] - 1)], tmp, mid[i[idx]:length(body)]) } @@ -153,3 +154,17 @@ group_grid_row <- function(x, i, ...) { +# insert horizontal rules everywhere (important for word) +grid_hlines <- function(x) { + rule_line <- grid_line(meta(x, "col_widths"), "-") + lines <- strsplit(x, split = "\\n")[[1]] + if (length(lines) > 1) { + for (idlines in length(lines):2) { + if (!startsWith(lines[idlines - 1], "+") && !startsWith(lines[idlines], "+") && lines[idlines] != "") { + lines <- c(lines[1:(idlines - 1)], rule_line, lines[idlines:length(lines)]) + } + } + } + out <- paste(lines, collapse = "\n") + return(out) +}