diff --git a/R/build_tt.R b/R/build_tt.R index b189cc9b..0b161489 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -15,6 +15,15 @@ build_tt <- function(x, output = NULL) { l[["x"]] <- tmp out <- eval(l) } + + # markdown styles need to be applied before creating the table, otherwise there's annoying parsing, etc. + if (output == "markdown") { + for (l in m$lazy_style) { + l[["x"]] <- out + out <- eval(l) + } + } + # shouldn't have to add this everywhere, but I'm too lazy to check out <- meta(out, "output", output) @@ -46,9 +55,11 @@ build_tt <- function(x, output = NULL) { out <- meta(out, "output", output) # style the table - for (l in m$lazy_style) { - l[["x"]] <- out - out <- eval(l) + if (output != "markdown") { + for (l in m$lazy_style) { + l[["x"]] <- out + out <- eval(l) + } } out <- meta(out, "output", output) diff --git a/R/style_grid.R b/R/style_grid.R new file mode 100644 index 00000000..d90346b7 --- /dev/null +++ b/R/style_grid.R @@ -0,0 +1,40 @@ +style_grid <- function(x, + i = NULL, + j = NULL, + bold = FALSE, + italic = FALSE, + monospace = FALSE, + underline = FALSE, + strikeout = FALSE, + ...) { + + if (meta(x, "output") != "markdown") return(x) + + out <- x + + ival <- if (is.null(i)) seq_len(meta(x, "nrows")) else i + jval <- if (is.null(j)) seq_len(meta(x, "ncols")) else j + + for (col in seq_along(out)) { + out[[col]] <- as.character(out[[col]]) + } + + for (row in ival) { + for (col in jval) { + if (isTRUE(bold)) { + out[row, col] <- sprintf("**%s**", out[row, col]) + } + if (isTRUE(italic)) { + out[row, col] <- sprintf("*%s*", out[row, col]) + } + if (isTRUE(strikeout)) { + out[row, col] <- sprintf("~~%s~~", out[row, col]) + } + } + } + + attr(out, "tinytable_meta") <- meta(x) + class(out) <- class(x) + return(out) +} + diff --git a/R/style_tt.R b/R/style_tt.R index 93dc35d0..cbcec060 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -1,7 +1,10 @@ #' Style a Tiny Table in either LaTeX or HTML format #' +#' @details #' This function applies styling to a table created by `tt()`. It allows customization of text style (bold, italic, monospace), text and background colors, font size, cell width, text alignment, column span, and indentation. The function supports both LaTeX (tabularray) and HTML (bootstrap) formats. #' +#' Note that Markdown and Word formats are limited to these styles: italic, bold, strikeout. +#' #' @param x A table object created by `tt()`. #' @param i Row indices where the styling should be applied. Can be a single value or a vector. If `colspan` is used, `i` must be of length 1. When i=0, the header is styled. #' @param j Column indices where the styling should be applied. Can be a single value, a vector, or a Perl-style regular expression applied to column names of the original data frame. If `colspan` is used, `j` must be of length 1. @@ -119,7 +122,7 @@ style_tt_lazy <- function (x, # sanity x if (is.null(meta(x))) stop("`x` must be generated by `tinytable::tt()`.", call. = FALSE) - if (!isTRUE(meta(x)$output %in% c("html", "latex"))) return(x) + if (!isTRUE(meta(x)$output %in% c("html", "latex", "markdown"))) return(x) out <- x @@ -175,6 +178,11 @@ style_tt_lazy <- function (x, color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent, bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule) + out <- style_grid( + x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout, + color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent, + bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule) + return(out) } diff --git a/man/style_tt.Rd b/man/style_tt.Rd index fb00de62..77ed991a 100644 --- a/man/style_tt.Rd +++ b/man/style_tt.Rd @@ -86,7 +86,12 @@ style_tt( An object of class \code{tt} representing the table. } \description{ +Style a Tiny Table in either LaTeX or HTML format +} +\details{ This function applies styling to a table created by \code{tt()}. It allows customization of text style (bold, italic, monospace), text and background colors, font size, cell width, text alignment, column span, and indentation. The function supports both LaTeX (tabularray) and HTML (bootstrap) formats. + +Note that Markdown and Word formats are limited to these styles: italic, bold, strikeout. } \section{LaTeX preamble}{