From cf141c2594d1663d865a21e854e64dac48f5121e Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Mon, 15 Jan 2024 20:19:21 -0500 Subject: [PATCH] break up style_tabularray --- R/style_tabularray.R | 88 ++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/R/style_tabularray.R b/R/style_tabularray.R index c2ac22d5..ea8bbf62 100644 --- a/R/style_tabularray.R +++ b/R/style_tabularray.R @@ -14,49 +14,67 @@ style_tabularray <- function(x, att <- attributes(x) out <- strsplit(x, "\n")[[1]] + out <- style_tabularray_inner(out, inner) + out <- style_tabularray_outer(out, outer) + out <- style_tabularray_body(out, body) - if (!is.null(inner)) { - inner <- trimws(inner) - if (!grepl(",$", inner)) inner <- paste0(inner, ",") - idx <- grep("% tabularray inner close", out) - out <- c( - out[1:(idx - 1)], - # empty lines can break latex - inner, - out[idx:length(out)]) - } - - if (!is.null(outer)) { - outer <- trimws(outer) - if (!grepl(",$", outer)) outer <- paste0(outer, ",") - idx <- grep("% tabularray outer close", out) - out <- c( - out[1:(idx - 1)], - # empty lines can break latex - outer, - out[idx:length(out)]) - } + # important for group_tt() + out <- meta(out, "body", body) - if (!is.null(body)) { - idx <- grep("% tabularray inner close", out) - out <- c( - out[1:idx], - # empty lines can break latex - trimws(body), - out[(idx + 1):length(out)]) - } + return(out) +} - # rebuild +style_tabularray_body <- function(x, body = NULL) { + if (is.null(body)) return(x) + m <- meta(x) + out <- strsplit(x, "\n")[[1]] + idx <- grep("% tabularray inner close", out) + out <- c( + out[1:idx], + # empty lines can break latex + trimws(body), + out[(idx + 1):length(out)]) out <- paste(out, collapse = "\n") - attributes(out) <- att class(out) <- class(x) + attr(out, "tinytable_meta") <- m + return(out) +} - # important for group_tt() - out <- meta(out, "body", body) - +style_tabularray_outer <- function(x, outer = NULL) { + if (is.null(outer)) return(x) + m <- meta(x) + out <- strsplit(x, "\n")[[1]] + outer <- trimws(outer) + if (!grepl(",$", outer)) outer <- paste0(outer, ",") + idx <- grep("% tabularray outer close", out) + out <- c( + out[1:(idx - 1)], + # empty lines can break latex + outer, + out[idx:length(out)]) + out <- paste(out, collapse = "\n") + class(out) <- class(x) + attr(out, "tinytable_meta") <- m return(out) -} +} +style_tabularray_inner <- function(x, inner = NULL) { + if (is.null(inner)) return(x) + m <- meta(x) + out <- strsplit(x, "\n")[[1]] + inner <- trimws(inner) + if (!grepl(",$", inner)) inner <- paste0(inner, ",") + idx <- grep("% tabularray inner close", out) + out <- c( + out[1:(idx - 1)], + # empty lines can break latex + inner, + out[idx:length(out)]) + out <- paste(out, collapse = "\n") + class(out) <- class(x) + attr(out, "tinytable_meta") <- m + return(out) +} ## not longer used, but took a while to collect and might be useful in the future