Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions R/html_style.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ setMethod(

# rowspan/colspan spans first
if (!is.null(other) && nrow(other) > 0 && any(c("rowspan", "colspan") %in% names(other))) {
listeners <- character(0)
for (row in seq_len(nrow(other))) {
rowspan <- if ("rowspan" %in% names(other) && !is.na(other$rowspan[row])) other$rowspan[row] else 1
colspan <- if ("colspan" %in% names(other) && !is.na(other$colspan[row])) other$colspan[row] else 1
Expand All @@ -216,14 +217,17 @@ setMethod(
rowspan,
colspan
)
x@table_string <- lines_insert(
x@table_string,
listener,
"tinytable span after",
"after"
)
listeners <- c(listeners, listener)
}
}
if (length(listeners) > 0) {
x@table_string <- lines_insert(
x@table_string,
paste(rev(listeners), collapse = "\n"),
"tinytable span after",
"after"
)
}
}


Expand Down Expand Up @@ -535,6 +539,8 @@ setMethod(
group_keys_sorted <- group_keys_sorted[order(sort_keys)]
}

style_arrays <- character(0)
css_entries <- character(0)
for (group_key in group_keys_sorted) {
group_data <- css_groups[[group_key]]
css_rule <- group_data$css_rule
Expand All @@ -561,22 +567,27 @@ setMethod(
"}, "
)
arr <- paste(arr, collapse = "")
x@table_string <- lines_insert(
x@table_string,
arr,
"tinytable style arrays after",
"after"
)
style_arrays <- c(style_arrays, arr)

# Generate CSS entry - scoped to table ID to prevent CSS cascade conflicts
table_id <- paste0("tinytable_", x@id)
entry <- sprintf(
" #%s td.%s, #%s th.%s { %s }",
table_id, id_css, table_id, id_css, css_rule
)
css_entries <- c(css_entries, entry)
}

if (length(style_arrays) > 0) {
x@table_string <- lines_insert(
x@table_string,
paste(rev(style_arrays), collapse = "\n"),
"tinytable style arrays after",
"after"
)
x@table_string <- lines_insert(
x@table_string,
entry,
paste(rev(css_entries), collapse = "\n"),
"tinytable css entries after",
"after"
)
Expand Down
16 changes: 9 additions & 7 deletions R/typst_style.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ typst_apply_styles <- function(x, rec) {
)
}

# Insert style-array entries
for (entry in rev(style_array_entries)) {
# Insert all style-array entries in one pass over the table string. The old
# loop inserted rev(style_array_entries) after the same marker, which
# produced style_array_entries order in the final output.
if (length(style_array_entries) > 0) {
x@table_string <- lines_insert(
x@table_string,
paste0(" ", entry),
paste0(" ", style_array_entries, collapse = "\n"),
"tinytable cell style after",
"after"
)
Expand Down Expand Up @@ -318,10 +320,10 @@ typst_hlines <- function(x, lin) {
}
return(out)
})
for (l in tmp) {
if (length(tmp) > 0) {
x@table_string <- lines_insert(
x@table_string,
l,
paste(unlist(tmp, use.names = FALSE), collapse = "\n"),
"tinytable lines before",
"before"
)
Expand Down Expand Up @@ -483,10 +485,10 @@ typst_vlines <- function(x, lin) {
}
return(out)
})
for (l in lin) {
if (length(lin) > 0) {
x@table_string <- lines_insert(
x@table_string,
l,
paste(unlist(lin, use.names = FALSE), collapse = "\n"),
"tinytable lines before",
"before"
)
Expand Down
17 changes: 14 additions & 3 deletions R/typst_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,20 @@ typst_notes <- function(x, out) {

notes <- sapply(notes, function(n) if (is.list(n)) n$text else n)

for (k in seq_along(notes)) {
note_text <- typst_note(notes[k], lab[k], ncol(x))
out <- lines_insert(out, note_text, "tinytable notes after", "after")
note_text <- vapply(
seq_along(notes),
function(k) typst_note(notes[k], lab[k], ncol(x)),
character(1)
)
if (length(note_text) > 0) {
# Repeated insertion after one marker reverses the input, so reverse the
# batch to preserve the existing byte-for-byte output order.
out <- lines_insert(
out,
paste(rev(note_text), collapse = "\n"),
"tinytable notes after",
"after"
)
}

out
Expand Down
Loading