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

Fix footnote + source notes rendering double <tfoot> #1876

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

* Improved footnote rendering in Quarto with `fmt_markdown()` (@olivroy, #1773)

* Improved rendering in Quarto if footnotes and source notes are present (@olivroy, #1296).

# gt 0.11.0

## New features
Expand Down
14 changes: 12 additions & 2 deletions R/render_as_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ render_as_html <- function(data) {
# Create the footnotes component
footnotes_component <- create_footnotes_component_h(data = data)

# Create the footer component <tfoot>
if (identical(source_notes_component, "") && identical(footnotes_component, "")) {
# FIXME Put NULL in next PR
footer_component <- "\n "
} else {
footer_component <- htmltools::tags$tfoot(
source_notes_component,
footnotes_component
)
}

# Get attributes for the gt table
table_defs <- get_table_defs(data = data)

Expand All @@ -90,8 +101,7 @@ render_as_html <- function(data) {
table_defs$table_colgroups,
table_head,
body_component,
source_notes_component,
footnotes_component
footer_component
)
}

Expand Down
20 changes: 12 additions & 8 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -1648,13 +1648,14 @@ create_source_notes_component_h <- function(data) {
# Handle the multiline source notes case (each footnote takes up one line)
if (multiline) {
# Create the source notes component as a series of `<tr><td>` (one per
# source note) inside of a `<tfoot>`
# source note) inside of a `<div>`
return(
htmltools::tags$tfoot(
htmltools::tags$div(
class = "gt_sourcenotes",
lapply(
source_notes,
function(x) {
# TODO maybe one of those is not necessary? keep either tr or td?
htmltools::tags$tr(
htmltools::tags$td(
class = "gt_sourcenote",
Expand All @@ -1674,8 +1675,9 @@ create_source_notes_component_h <- function(data) {
separator <- gsub(" (?= )", "&nbsp;", separator, perl = TRUE)

# Create the source notes component as a single `<tr><td>` inside
# of a `<tfoot>`
htmltools::tags$tfoot(
# of a `<div>`
htmltools::tags$div(
# TODO maybe one of those is not necessary? keep either tr or td?
htmltools::tags$tr(
class = "gt_sourcenotes",
style = source_notes_styles,
Expand Down Expand Up @@ -1772,13 +1774,14 @@ create_footnotes_component_h <- function(data) {
if (multiline) {

# Create the footnotes component as a series of `<tr><td>` (one per
# footnote) inside of a `<tfoot>`
# footnote) inside of a `<div>`
return(
htmltools::tags$tfoot(
htmltools::tags$div(
class = "gt_footnotes",
lapply(
footnotes,
function(x) {
# TODO maybe one of those is not necessary? keep either tr or td?
htmltools::tags$tr(
htmltools::tags$td(
class = "gt_footnote",
Expand All @@ -1798,10 +1801,11 @@ create_footnotes_component_h <- function(data) {
separator <- gsub(" (?= )", "&nbsp;", separator, perl = TRUE)

# Create the footnotes component as a single `<tr><td>` inside
# of a `<tfoot>`
htmltools::tags$tfoot(
# of a `<div>`
htmltools::tags$div(
htmltools::tags$tr(
class = "gt_footnotes",
# TODO maybe one of those is not necessary? keep either tr or td?
style = footnotes_styles,
htmltools::tags$td(
class = "gt_footnote",
Expand Down
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.

64 changes: 32 additions & 32 deletions tests/testthat/_snaps/group_column_label.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 <tfoot>\n \n <div 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 </div>\n </tfoot>\n</table>"

Loading
Loading