From 323bbbdaf25ea6a3d9073c2fa777a9dc3d0125e4 Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Sun, 8 Dec 2024 15:07:58 -0500 Subject: [PATCH] caption style start --- R/build_tt.R | 1 + R/class.R | 1 + R/sanity.R | 3 ++- R/style_notes.R | 45 --------------------------------------------- R/style_string.R | 40 ++++++++++++++++++++++++++++++++++++++++ R/style_tt.R | 6 ++++-- 6 files changed, 48 insertions(+), 48 deletions(-) diff --git a/R/build_tt.R b/R/build_tt.R index e773274a..435489ff 100644 --- a/R/build_tt.R +++ b/R/build_tt.R @@ -20,6 +20,7 @@ build_tt <- function(x, output = NULL) { # apply the style_notes x <- style_notes(x) + x <- style_caption(x) for (th in x@lazy_theme) { fn <- th[[1]] diff --git a/R/class.R b/R/class.R index d0a9ed91..99a09962 100644 --- a/R/class.R +++ b/R/class.R @@ -45,6 +45,7 @@ setClass( bootstrap_css_rule = "character", css = "data.frame", style = "data.frame", + style_caption = "list", style_notes = "list", lazy_format = "list", lazy_group = "list", diff --git a/R/sanity.R b/R/sanity.R index 6c2966d5..5f92d5a3 100644 --- a/R/sanity.R +++ b/R/sanity.R @@ -13,7 +13,8 @@ sanity_align <- function(align, i) { sanitize_i <- function(i, x, pre_group_i = FALSE, lazy = TRUE) { - if (identical(i, "notes")) { + if (is.character(i)) { + assert_choice(i, c("notes", "caption")) return(i) } out <- seq_len(nrow(x)) diff --git a/R/style_notes.R b/R/style_notes.R index 9d05568e..e69de29b 100644 --- a/R/style_notes.R +++ b/R/style_notes.R @@ -1,45 +0,0 @@ -setGeneric( - name = "style_notes", - def = function(x, ...) standardGeneric("style_notes") -) - -# default method -setMethod( - f = "style_notes", - signature = "ANY", - definition = function(x, ...) { - return(x) - }) - - -# HTML: bootstrap -setMethod( - f = "style_notes", - signature = "tinytable_bootstrap", - definition = function(x, ...) { - styles <- x@style_notes - x@notes <- lapply(x@notes, style_string_html, styles) - return(x) - }) - - -# LaTeX: tabularray -setMethod( - f = "style_notes", - signature = "tinytable_tabularray", - definition = function(x, ...) { - styles <- x@style_notes - x@notes <- lapply(x@notes, style_string_latex, styles) - return(x) - }) - - -# Typst -setMethod( - f = "style_notes", - signature = "tinytable_typst", - definition = function(x, ...) { - styles <- x@style_notes - x@notes <- lapply(x@notes, style_string_typst, styles) - return(x) - }) diff --git a/R/style_string.R b/R/style_string.R index 0ee5bc79..091b0a8e 100644 --- a/R/style_string.R +++ b/R/style_string.R @@ -78,3 +78,43 @@ style_string_typst <- function(n, styles) { out <- sprintf(template, n) return(out) } + + + +style_notes <- function(x) { + fun <- switch(x@output, + "typst" = style_string_typst, + "html" = style_string_html, + "html_portable" = style_string_html, + "latex" = style_string_latex, + function(k, ...) identity(k) + ) + x@notes <- lapply(x@notes, fun, x@style_notes) + return(x) +} + +style_notes <- function(x) { + fun <- switch(x@output, + "typst" = style_string_typst, + "html" = style_string_html, + "html_portable" = style_string_html, + "latex" = style_string_latex, + function(k, ...) identity(k) + ) + x@notes <- lapply(x@notes, fun, x@style_notes) + return(x) +} + +style_caption <- function(x) { + fun <- switch(x@output, + "typst" = style_string_typst, + "html" = style_string_html, + "html_portable" = style_string_html, + "latex" = style_string_latex, + function(k, ...) identity(k) + ) + if (length(x@caption) > 0) { + x@caption <- fun(x@caption, x@style_caption) + } + return(x) +} diff --git a/R/style_tt.R b/R/style_tt.R index 4c47d88a..3b68ae1c 100644 --- a/R/style_tt.R +++ b/R/style_tt.R @@ -158,8 +158,8 @@ style_tt <- function(x, tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule) - if (identical(i, "notes")) { - out@style_notes <- list( + if (isTRUE(i %in% c("notes", "caption"))) { + tmp <- list( color = color, fontsize = fontsize, italic = italic, @@ -167,6 +167,8 @@ style_tt <- function(x, strikeout = strikeout, underline = underline ) + if (identical(i, "notes")) out@style_notes <- tmp + if (identical(i, "caption")) out@style_caption <- tmp return(out) }