Skip to content

Commit

Permalink
break up style_tabularray
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 16, 2024
1 parent 6edeefa commit cf141c2
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions R/style_tabularray.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf141c2

Please sign in to comment.