-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68cdb7
commit 1f7dbb7
Showing
9 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
escape_text <- function(x, output = "latex") { | ||
if (length(x) < 1 || all(is.na(x))) { | ||
return(x) | ||
} | ||
|
||
out <- x | ||
|
||
if (isTRUE(output == "latex")) { | ||
# LaTeX escaping code adapted from the `gt` package, published under MIT | ||
# https://github.com/rstudio/gt/ | ||
# YEAR: 2018-2024 | ||
# COPYRIGHT HOLDER: gt authors | ||
# If all text elements are `NA_character_` then return `text` unchanged | ||
latex_special_chars <- c( | ||
"\\" = "\\textbackslash{}", | ||
"~" = "\\textasciitilde{}", | ||
"^" = "\\textasciicircum{}", | ||
"&" = "\\&", | ||
"%" = "\\%", | ||
"$" = "\\$", | ||
"#" = "\\#", | ||
"_" = "\\_", | ||
"{" = "\\{", | ||
"}" = "\\}" | ||
) | ||
na_out <- is.na(out) | ||
m <- gregexpr("[\\\\&%$#_{}~^]", out[!na_out], perl = TRUE) | ||
special_chars <- regmatches(out[!na_out], m) | ||
escaped_chars <- lapply(special_chars, function(x) { | ||
latex_special_chars[x] | ||
}) | ||
regmatches(out[!na_out], m) <- escaped_chars | ||
} else if (isTRUE(output == "html")) { | ||
assert_dependency("htmltools") | ||
out <- htmltools::htmlEscape(out) | ||
} | ||
|
||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[1] "<!DOCTYPE html> \n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>tinytable_xnclv9tvdcq09jxgyitf</title>\n <link href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css\" rel=\"stylesheet\">\n <style>\n.table td.tinytable_css_4or5kg1sue01fe9hmaas, .table th.tinytable_css_4or5kg1sue01fe9hmaas { border-bottom: solid 0.1em #d3d8dc; }\n </style>\n <script src=\"https://polyfill.io/v3/polyfill.min.js?features=es6\"></script>\n <script id=\"MathJax-script\" async src=\"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js\"></script>\n <script>\n MathJax = {\n tex: {\n inlineMath: [['$', '$'], ['\\\\(', '\\\\)']]\n },\n svg: {\n fontCache: 'global'\n }\n };\n </script>\n </head>\n\n <body>\n <div class=\"container\">\n <table class=\"table table-borderless\" id=\"tinytable_xnclv9tvdcq09jxgyitf\" style=\"width: auto; margin-left: auto; margin-right: auto;\" data-quarto-disable-processing='true'>\n <thead>\n \n <tr>\n <th scope=\"col\">LaTeX</th>\n <th scope=\"col\">HTML</th>\n </tr>\n </thead>\n \n <tbody>\n <tr>\n <td>Dollars $</td>\n <td><br></td>\n </tr>\n <tr>\n <td>Percent %</td>\n <td><sup>4</sup></td>\n </tr>\n <tr>\n <td>Underscore _</td>\n <td><emph>blah</emph></td>\n </tr>\n </tbody>\n </table>\n </div>\n\n <script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js\"></script>\n <script>\n function styleCell_tinytable_mj8hiuwkqnddps3hrarw(i, j, css_id) {\n var table = document.getElementById(\"tinytable_xnclv9tvdcq09jxgyitf\");\n table.rows[i].cells[j].classList.add(css_id);\n }\n function insertSpanRow(i, colspan, content) {\n var table = document.getElementById('tinytable_xnclv9tvdcq09jxgyitf');\n var newRow = table.insertRow(i);\n var newCell = newRow.insertCell(0);\n newCell.setAttribute(\"colspan\", colspan);\n // newCell.innerText = content;\n // this may be unsafe, but innerText does not interpret <br>\n newCell.innerHTML = content;\n }\nwindow.addEventListener('load', function () { styleCell_tinytable_mj8hiuwkqnddps3hrarw(0, 0, 'tinytable_css_4or5kg1sue01fe9hmaas') })\nwindow.addEventListener('load', function () { styleCell_tinytable_mj8hiuwkqnddps3hrarw(0, 1, 'tinytable_css_4or5kg1sue01fe9hmaas') })\n </script>\n\n </body>\n\n</html>" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[1] "\\begin{table}\n\\centering\n\\begin{tblr}[ %% tabularray outer open\n] %% tabularray outer close\n{ %% tabularray inner open\ncolspec={Q[]Q[]},\n} %% tabularray inner close\n\\toprule\nLaTeX & HTML \\\\ \\midrule %% TinyTableHeader\nDollars \\$ & <br> \\\\\nPercent \\% & <sup>4</sup> \\\\\nUnderscore \\_ & <emph>blah</emph> \\\\\n\\bottomrule\n\\end{tblr}\n\\end{table}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source("helpers.R") | ||
using("tinysnapshot") | ||
|
||
dat <- data.frame( | ||
"LaTeX" = c("Dollars $", "Percent %", "Underscore _"), | ||
"HTML" = c("<br>", "<sup>4</sup>", "<emph>blah</emph>") | ||
) | ||
|
||
set.seed(1024) # reproducibility of html unique IDs | ||
tab <- tt(dat) |> format_tt(escape = TRUE) |> save_tt("latex") | ||
expect_snapshot_print(tab, "escape-latex") | ||
|
||
tab <- tt(dat) |> format_tt(escape = TRUE) |> save_tt("html") | ||
expect_snapshot_print(tab, "escape-html") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters