Skip to content

Commit

Permalink
issue #93 bootstrap_class argument instead of themes
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 21, 2024
1 parent aa02ad0 commit 895775d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 55 deletions.
4 changes: 3 additions & 1 deletion R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ build_tt <- function(x, output = NULL) {
out <- eval(l)
}
}
out <- meta(out, "output", output)

# finalize
out <- finalize_bootstrap(out)

# formal grid specification in pandoc includes lines everywhere
hlines <- getOption("tinytable_grid_hlines", default = TRUE)
Expand Down
9 changes: 9 additions & 0 deletions R/style_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ style_bootstrap <- function(x,
align = NULL,
colspan = NULL,
indent = 0,
bootstrap_class = "table",
bootstrap_css = NULL,
bootstrap_css_rule = NULL) {

Expand Down Expand Up @@ -120,6 +121,14 @@ style_bootstrap <- function(x,
out <- bootstrap_setting(out, bootstrap_css_rule, component = "css")
}

if (!is.null(bootstrap_class)) {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
bootstrap_class,
out,
fixed = TRUE)
}

# Changing function names to table ID to avoid conflict with other tables functions
out <- gsub("styleCell_\\w+\\(", paste0("styleCell_", meta(x, "id"), "("), out)

Expand Down
12 changes: 9 additions & 3 deletions R/style_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#' @param align A single character or a string with a number of characters equal to the number of columns in `j`. Valid characters include 'c' (center), 'l' (left), or 'r' (right).
#' @param colspan Number of columns a cell should span. Can only be used if both `i` and `j` are of length 1. Must be an integer greater than 1.
#' @param indent Text indentation in em units. Positive values only.
#' @param bootstrap_class String. A Bootstrap table class such as `"table"`, `"table table-dark"` or `"table table-dark table-hover"`. See the bootstrap documentation.
#' @param bootstrap_css A vector of CSS style declarations to be applied (ex: `"font-weight: bold"`). Each element corresponds to a cell defined by `i` and `j`.
#' @param bootstrap_css_rule A string with complete CSS rules that apply to the table class specified using the `theme` argument of the `tt()` function.
#' @param tabularray_inner A string that specifies the "inner" settings of a tabularray LaTeX table.
Expand Down Expand Up @@ -62,6 +63,7 @@ style_tt <- function (x,
indent = 0,
tabularray_inner = NULL,
tabularray_outer = NULL,
bootstrap_class = "table",
bootstrap_css = NULL,
bootstrap_css_rule = NULL,
...) {
Expand All @@ -86,6 +88,7 @@ style_tt <- function (x,
indent = indent,
tabularray_inner = tabularray_inner,
tabularray_outer = tabularray_outer,
bootstrap_class = bootstrap_class,
bootstrap_css = bootstrap_css,
bootstrap_css_rule = bootstrap_css_rule)

Expand Down Expand Up @@ -117,6 +120,7 @@ style_tt_lazy <- function (x,
indent,
tabularray_inner,
tabularray_outer,
bootstrap_class,
bootstrap_css,
bootstrap_css_rule) {

Expand Down Expand Up @@ -166,7 +170,7 @@ style_tt_lazy <- function (x,
x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
tabularray_inner = tabularray_inner, tabularray_outer = tabularray_outer, bootstrap_css = bootstrap_css,
bootstrap_css_rule = bootstrap_css_rule)
bootstrap_css_rule = bootstrap_css_rule, bootstrap_class = bootstrap_class)

out <- style_tabularray(
x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
Expand All @@ -176,7 +180,7 @@ style_tt_lazy <- function (x,
out <- style_bootstrap(
x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
color = color, background = background, fontsize = fontsize, width = width, align = align, colspan = colspan, indent = indent,
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule)
bootstrap_css = bootstrap_css, bootstrap_css_rule = bootstrap_css_rule, bootstrap_class = bootstrap_class)

out <- style_grid(
x = out, i = i, j = j, bold = bold, italic = italic, monospace = monospace, underline = underline, strikeout = strikeout,
Expand Down Expand Up @@ -204,7 +208,8 @@ assert_style_tt <- function (x,
indent,
tabularray_inner,
tabularray_outer,
bootstrap_css,
bootstrap_class = NULL,
bootstrap_css = NULL,
bootstrap_css_rule = NULL) {

m <- meta(x)
Expand All @@ -220,6 +225,7 @@ assert_style_tt <- function (x,
assert_logical(monospace)
assert_logical(underline)
assert_logical(strikeout)
assert_character(bootstrap_class, null.ok = TRUE)
assert_character(bootstrap_css, null.ok = TRUE)
assert_string(bootstrap_css_rule, null.ok = TRUE)

Expand Down
44 changes: 0 additions & 44 deletions R/template.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,3 @@ template_tabularray <- function(theme = "default") {
}
return(out)
}


template_bootstrap <- function(theme = "default") {
assert_string(theme)
out <- readLines(system.file("templates/bootstrap.html", package = "tinytable"))

if (theme == "default") {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table",
out,
fixed = TRUE)

} else if (theme == "void") {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table table-borderless",
out,
fixed = TRUE)

} else if (theme == "grid") {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table table-bordered",
out,
fixed = TRUE)

} else if (theme == "striped") {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
"table table-striped",
out,
fixed = TRUE)

} else {
out <- gsub(
"$tinytable_BOOTSTRAP_CLASS",
theme,
out,
fixed = TRUE)
}

return(out)
}
2 changes: 1 addition & 1 deletion R/tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' @param width A numeric value between 0 and 1 indicating the proportion of the line width that the table should cover.
#' @param theme The theme to apply to the table.
#' * LaTeX: "default", "striped", "void", or "grid".
#' * HTML: "default", "striped", "void", "grid", or a (composite) Bootstrap class such as `"table table-dark"` or `"table table-dark table-hover"`. See
#' * HTML: "default", "striped", "void", "grid", or a (composite)
#' @param notes A single string or a (named) list of strings to append at the bottom of the table.
#'
#' @param placement A string to control the position of tables in LaTeX. Will be inserted in square brackets like: `\\begin{table}[H]`
Expand Down
2 changes: 1 addition & 1 deletion R/tt_bootstrap.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tt_bootstrap <- function(x, caption, theme, width, notes, ...) {

template <- template_bootstrap(theme)
template <- readLines(system.file("templates/bootstrap.html", package = "tinytable"))
m <- meta(x)


Expand Down
3 changes: 3 additions & 0 deletions man/style_bootstrap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/style_tt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/tt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vignettes/tutorial.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -598,20 +598,20 @@ The HTML customization options described in this section are not available for L
## Themes

::: {.content-visible when-format="html"}
The Bootstrap framework provides a number of built-in themes to style tables. To use them, we call `bootstrapOptions()` with the `class` argument, and we specify the Bootstrap class. A list of available Bootstrap classes can be found here: <https://getbootstrap.com/docs/5.3/content/tables/>
The Bootstrap framework provides a number of built-in themes to style tables, using "classes." To use them, we call `style_tt()` with the `bootstrap_class` argument. A list of available Bootstrap classes can be found here: <https://getbootstrap.com/docs/5.3/content/tables/>

For example, to produce a "dark" table, we use the `table-dark` class:

```{r, eval = knitr::is_html_output()}
tt(x, theme = "table table-dark")
tt(x) |> style_tt(bootstrap_class = "table table-dark")
```

We can also combine several Bootstrap classes. Here, we get a "striped" table with the "warning" color and hover feature:

```{r, eval = knitr::is_html_output()}
tt(x, theme = "table table-striped table-warning table-hover")
tt(x) |> style_tt(
bootstrap_class = "table table-striped table-warning table-hover")
```

:::

## CSS declarations
Expand Down

0 comments on commit 895775d

Please sign in to comment.