Skip to content

Commit

Permalink
word works
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 19, 2024
1 parent d84f106 commit e3d465b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
6 changes: 6 additions & 0 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 3 additions & 19 deletions R/save_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,13 @@ 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))

}


# 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)
}
15 changes: 15 additions & 0 deletions R/tt_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
}

Expand All @@ -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)
}

0 comments on commit e3d465b

Please sign in to comment.