Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve access to option values + use htmltools to create some css code #1868

Merged
merged 11 commits into from
Sep 3, 2024
1,681 changes: 1,681 additions & 0 deletions R/fmt_date_time.R

Large diffs are not rendered by default.

1,659 changes: 1 addition & 1,658 deletions R/format_data.R

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions R/utils_render_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ create_heading_component_g <- function(data) {
footnote_title_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_title_marks$fs_id_c
mark = footnote_title_marks
)
}
footnote_subtitle_marks <- ""
Expand All @@ -142,7 +142,7 @@ create_heading_component_g <- function(data) {
footnote_subtitle_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_subtitle_marks$fs_id_c
mark = footnote_subtitle_marks
)
}

Expand Down
72 changes: 32 additions & 40 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ footnote_mark_to_html <- function(
font_weight <- "normal"
}

paste0(
"<span ",
htmltools::tags$span(
if (is_sup) {
paste0("class=\"", sup_class, "\" ")
htmltools::tags$sup(mark, .noWS = "before")
} else {
NULL
mark
},
"style=\"",
"white-space:nowrap;",
"font-style:", font_style, ";",
"font-weight:", font_weight, ";",
"line-height: 0;",
"\">",
if (is_sup) {
paste0("<sup>", mark, "</sup>")
class = if (is_sup) {
sup_class
} else {
mark
NULL
},
"</span>"
style = htmltools::css(
`white-space` = "nowrap",
`font-style` = font_style,
`font-weight` = font_weight,
`line-height` = 0
),
.noWS = "before-end"
)
}

Expand Down Expand Up @@ -237,45 +236,29 @@ coalesce_marks <- function(
locname,
delimiter = ","
) {
filtered_tbl <- dplyr::filter(fn_tbl, locname == !!locname)
dplyr::summarize(filtered_tbl, fs_id_c = paste(fs_id, collapse = delimiter))
fs_ids <- vctrs::vec_slice(fn_tbl$fs_id, fn_tbl$locname == locname)
paste(fs_ids, collapse = delimiter)
Copy link
Collaborator Author

@olivroy olivroy Aug 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I refactored coalesce_marks() to return a single string because the resulting data frame (1 col x 1 row) previously was never used, and all code needed to access the fs_id_c variable. e86b842

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement! Yeah, a vector is all we need.

}

# Get the attributes for the table tag
get_table_defs <- function(data) {

boxh <- dt_boxhead_get(data = data)

# Get the `table-layout` value, which is set in `_options`
table_style <-
paste0(
"table-layout: ",
dt_options_get_value(
data = data,
option = "table_layout"
),
";"
)

# In the case that column widths are not set for any columns,
# there should not be a `<colgroup>` tag requirement
if (length(unlist(boxh$column_width)) < 1) {
return(list(table_style = NULL, table_colgroups = NULL))
}

# Get the `table-layout` value, which is set in `_options`
table_layout <- dt_options_get_value(data = data, option = "table_layout")

# Get the table's width (which or may not have been set)
table_width <-
dt_options_get_value(
data = data,
option = "table_width"
)
table_width <- dt_options_get_value(data = data, option = "table_width")

# Determine whether the row group is placed in the stub
row_group_as_column <-
dt_options_get_value(
data = data,
option = "row_group_as_column"
)
row_group_as_column <- dt_options_get_value(data = data, option = "row_group_as_column")

types <- c("default", "stub", if (row_group_as_column) "row_group" else NULL)

Expand Down Expand Up @@ -309,14 +292,23 @@ get_table_defs <- function(data) {
if (table_width == "auto") {

if (all(grepl("px", widths, fixed = TRUE))) {
# FIXME sometimes ends up being 0? #1532 and quarto-dev/quarto-cli#8233
table_width <- "0px"
} else if (all(grepl("%", widths, fixed = TRUE))) {
table_width <- "100%"
}
}

if (table_width != "auto") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rich-iannone to fix #1532, should we just have this condition be

Suggested change
if (table_width != "auto") {
if (!table_width %in% c("auto", "0", "0px")) {

If so, I will create a PR after this one is merged and update all snapshots.

I did the refactoring here to make the potential fix clearer in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the version that catches the zero-width table! I’d welcome a future PR that addresses that problem.

table_style <- paste(table_style, paste0("width: ", table_width), sep = "; ")
table_style <- htmltools::css(
`table-layout` = table_layout,
width = table_width
)
} else {
table_style <-
htmltools::css(
`table-layout` = table_layout
)
}

# Create the `<colgroup>` tag
Expand Down Expand Up @@ -415,7 +407,7 @@ create_heading_component_h <- function(data) {
footnote_title_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_title_marks$fs_id_c
mark = footnote_title_marks
)

} else {
Expand Down Expand Up @@ -449,7 +441,7 @@ create_heading_component_h <- function(data) {
footnote_subtitle_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_subtitle_marks$fs_id_c
mark = footnote_subtitle_marks
)

} else {
Expand Down
4 changes: 2 additions & 2 deletions R/utils_render_latex.R
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ create_heading_component_l <- function(data) {
footnote_title_marks <-
footnote_mark_to_latex(
data = data,
mark = footnote_title_marks$fs_id_c
mark = footnote_title_marks
)

} else {
Expand All @@ -361,7 +361,7 @@ create_heading_component_l <- function(data) {
footnote_subtitle_marks <-
footnote_mark_to_latex(
data = data,
mark = footnote_subtitle_marks$fs_id_c
mark = footnote_subtitle_marks
)

} else {
Expand Down
2 changes: 0 additions & 2 deletions R/utils_render_rtf.R
Original file line number Diff line number Diff line change
Expand Up @@ -1046,14 +1046,12 @@ create_heading_component_rtf <- function(data) {

if ("title" %in% footnotes_tbl$locname) {
footnote_title_marks <- coalesce_marks(fn_tbl = footnotes_tbl, locname = "title")
footnote_title_marks <- footnote_title_marks$fs_id_c
} else {
footnote_title_marks <- ""
}

if ("subtitle" %in% footnotes_tbl$locname) {
footnote_subtitle_marks <- coalesce_marks(fn_tbl = footnotes_tbl, locname = "subtitle")
footnote_subtitle_marks <- footnote_subtitle_marks$fs_id_c
} else {
footnote_subtitle_marks <- ""
}
Expand Down
9 changes: 5 additions & 4 deletions R/utils_render_xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ create_table_caption_component_xml <- function(
footnote_title_marks <-
footnote_mark_to_xml(
data = data,
mark = footnote_title_marks$fs_id_c
mark = footnote_title_marks
)
footnote_title_marks <- as_xml_node(footnote_title_marks)[[1L]]

Expand Down Expand Up @@ -1301,7 +1301,8 @@ create_table_caption_component_xml <- function(
footnote_subtitle_marks <-
footnote_mark_to_xml(
data = data,
mark = footnote_subtitle_marks$fs_id_c)
mark = footnote_subtitle_marks
)
footnote_subtitle_marks <- as_xml_node(footnote_subtitle_marks)[[1]]

xml_add_child(
Expand Down Expand Up @@ -1396,7 +1397,7 @@ create_heading_component_xml <- function(
footnote_title_marks <-
footnote_mark_to_xml(
data = data,
mark = footnote_title_marks$fs_id_c
mark = footnote_title_marks
)
footnote_title_marks <- as_xml_node(footnote_title_marks)[[1L]]

Expand Down Expand Up @@ -1426,7 +1427,7 @@ create_heading_component_xml <- function(
footnote_subtitle_marks <-
footnote_mark_to_xml(
data = data,
mark = footnote_subtitle_marks$fs_id_c
mark = footnote_subtitle_marks
)
footnote_subtitle_marks <- as_xml_node(footnote_subtitle_marks)[[1L]]

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/fmt_duration.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/fmt_fraction.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/fmt_markdown.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/footer.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/quarto.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Code
render_as_html(gt_tbl)
Output
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_heading\">\n <td colspan=\"3\" class=\"gt_heading gt_title gt_font_normal gt_bottom_border\" style><span data-qmd-base64=\"dGl0bGU=\"><span class='gt_from_md'>title</span></span></td>\n </tr>\n \n <tr class=\"gt_col_headings gt_spanner_row\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"2\" colspan=\"1\" scope=\"col\" id=\"num\">num</th>\n <th class=\"gt_center gt_columns_top_border gt_column_spanner_outer\" rowspan=\"1\" colspan=\"2\" scope=\"colgroup\" id=\"problem\">\n <div class=\"gt_column_spanner\"><span data-qmd-base64=\"cHJvYmxlbQ==\"><span class='gt_from_md'>problem</span></span></div>\n </th>\n </tr>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char<span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>1</sup></span></th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_center\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"fctr\"><span data-qmd-base64=\"RmFjdG9y\"><span class='gt_from_md'>Factor</span></span><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>2</sup></span></th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>3</sup></span> <span data-qmd-base64=\"MC4xMTEx\"><span class='gt_from_md'>0.1111</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">apricot</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">one</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"Mi4yMjI=\"><span class='gt_from_md'>2.222</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">banana</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">two</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"MzMuMzM=\"><span class='gt_from_md'>33.33</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">coconut</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">three</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"NDQ0LjQ=\"><span class='gt_from_md'>444.4</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">durian</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">four</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"NTU1MA==\"><span class='gt_from_md'>5550</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">NA</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">five</td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>1</sup></span> Not a problem</td>\n </tr>\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>2</sup></span> A problem because fctr is labelled with md</td>\n </tr>\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height: 0;\"><sup>3</sup></span> <span data-qmd-base64=\"UHJvYmxlbSBiZWNhdXNlIG51bSByb3cgMSBpcyBmbXRfbWFya2Rvd24oKSArIGFsc28gdGhlIGZvb3Rub3RlIGlzIHdyYXBwZWQgaW4gbWQu\"><span class='gt_from_md'>Problem because num row 1 is fmt_markdown() + also the footnote is wrapped in md.</span></span></td>\n </tr>\n </tfoot>\n</table>"
[1] "<table class=\"gt_table\" data-quarto-disable-processing=\"false\" data-quarto-bootstrap=\"false\">\n <thead>\n <tr class=\"gt_heading\">\n <td colspan=\"3\" class=\"gt_heading gt_title gt_font_normal gt_bottom_border\" style><span data-qmd-base64=\"dGl0bGU=\"><span class='gt_from_md'>title</span></span></td>\n </tr>\n \n <tr class=\"gt_col_headings gt_spanner_row\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_right\" rowspan=\"2\" colspan=\"1\" scope=\"col\" id=\"num\">num</th>\n <th class=\"gt_center gt_columns_top_border gt_column_spanner_outer\" rowspan=\"1\" colspan=\"2\" scope=\"colgroup\" id=\"problem\">\n <div class=\"gt_column_spanner\"><span data-qmd-base64=\"cHJvYmxlbQ==\"><span class='gt_from_md'>problem</span></span></div>\n </th>\n </tr>\n <tr class=\"gt_col_headings\">\n <th class=\"gt_col_heading gt_columns_bottom_border gt_left\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"char\">char<span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>1</sup></span></th>\n <th class=\"gt_col_heading gt_columns_bottom_border gt_center\" rowspan=\"1\" colspan=\"1\" scope=\"col\" id=\"fctr\"><span data-qmd-base64=\"RmFjdG9y\"><span class='gt_from_md'>Factor</span></span><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>2</sup></span></th>\n </tr>\n </thead>\n <tbody class=\"gt_table_body\">\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>3</sup></span> <span data-qmd-base64=\"MC4xMTEx\"><span class='gt_from_md'>0.1111</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">apricot</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">one</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"Mi4yMjI=\"><span class='gt_from_md'>2.222</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">banana</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">two</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"MzMuMzM=\"><span class='gt_from_md'>33.33</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">coconut</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">three</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"NDQ0LjQ=\"><span class='gt_from_md'>444.4</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">durian</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">four</td></tr>\n <tr><td headers=\"num\" class=\"gt_row gt_right\"><span data-qmd-base64=\"NTU1MA==\"><span class='gt_from_md'>5550</span></span></td>\n<td headers=\"char\" class=\"gt_row gt_left\">NA</td>\n<td headers=\"fctr\" class=\"gt_row gt_center\">five</td></tr>\n </tbody>\n \n <tfoot class=\"gt_footnotes\">\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>1</sup></span> Not a problem</td>\n </tr>\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>2</sup></span> A problem because fctr is labelled with md</td>\n </tr>\n <tr>\n <td class=\"gt_footnote\" colspan=\"3\"><span class=\"gt_footnote_marks\" style=\"white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;\"><sup>3</sup></span> <span data-qmd-base64=\"UHJvYmxlbSBiZWNhdXNlIG51bSByb3cgMSBpcyBmbXRfbWFya2Rvd24oKSArIGFsc28gdGhlIGZvb3Rub3RlIGlzIHdyYXBwZWQgaW4gbWQu\"><span class='gt_from_md'>Problem because num row 1 is fmt_markdown() + also the footnote is wrapped in md.</span></span></td>\n </tr>\n </tfoot>\n</table>"

Loading
Loading