Skip to content

Commit

Permalink
Merge pull request #995 from rstudio/remove-html-entities
Browse files Browse the repository at this point in the history
Improve implementation of all `vec_*()` functions
  • Loading branch information
rich-iannone authored Aug 5, 2022
2 parents faf9f40 + 99c962a commit 165c6d0
Show file tree
Hide file tree
Showing 44 changed files with 5,297 additions and 2,167 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ export(vec_fmt_bytes)
export(vec_fmt_currency)
export(vec_fmt_date)
export(vec_fmt_datetime)
export(vec_fmt_duration)
export(vec_fmt_engineering)
export(vec_fmt_fraction)
export(vec_fmt_integer)
export(vec_fmt_markdown)
export(vec_fmt_number)
export(vec_fmt_partsper)
export(vec_fmt_percent)
export(vec_fmt_scientific)
export(vec_fmt_time)
Expand Down
81 changes: 52 additions & 29 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +1314,10 @@ fmt_partsper <- function(
#' option to simplify the fraction (where possible) can be taken with `TRUE`
#' (the default). With `FALSE`, denominators in fractions will be fixed to the
#' value provided in `accuracy`.
#' @param layout For HTML output, the `"diagonal"` layout is the default. This
#' will generate fractions that are typeset with raised/lowered numerals and a
#' virgule. The `"inline"` layout places the numerals of the fraction on the
#' baseline and uses a standard slash character.
#' @param layout For HTML output, the `"inline"` layout is the default. This
#' layout places the numerals of the fraction on the baseline and uses a
#' standard slash character. The `"diagonal"` layout will generate fractions
#' that are typeset with raised/lowered numerals and a virgule.
#'
#' @return An object of class `gt_tbl`.
#'
Expand Down Expand Up @@ -1350,7 +1350,8 @@ fmt_partsper <- function(
#' fmt_fraction(
#' columns = starts_with("f_"),
#' accuracy = 10,
#' simplify = FALSE
#' simplify = FALSE,
#' layout = "diagonal"
#' ) %>%
#' sub_missing(missing_text = "") %>%
#' tab_spanner(
Expand Down Expand Up @@ -1399,14 +1400,13 @@ fmt_fraction <- function(
rows = everything(),
accuracy = NULL,
simplify = TRUE,
layout = c("diagonal", "inline"),
layout = c("inline", "diagonal"),
use_seps = TRUE,
pattern = "{x}",
sep_mark = ",",
system = c("intl", "ind"),
locale = NULL
) {

system <- match.arg(system)

# Perform input object validation
Expand Down Expand Up @@ -1602,21 +1602,45 @@ fmt_fraction <- function(

if (context == "html") {

narrow_no_break_space_char <- "\U0202F"
slash_mark_char <- "\U02044"

num_vec <-
paste0("<span class=\"gt_fraction_numerator\">", num_vec, "</span>")
paste0(
"<span style=\"",
"font-size:0.6em;",
"line-height:0.6em;",
"vertical-align:0.45em;",
"\">",
num_vec,
"</span>"
)

denom_vec <-
paste0("<span class=\"gt_fraction_denominator\">", denom_vec, "</span>")
paste0(
"<span style=\"",
"font-size:0.6em;",
"line-height:0.6em;",
"vertical-align:-0.05em;",
"\">",
denom_vec,
"</span>"
)

slash_mark <-
htmltools::tags$span(
class = "gt_slash_mark",
htmltools::HTML("&frasl;")
paste0(
"<span style=\"",
"font-size:0.7em;",
"line-height:0.7em;",
"vertical-align:0.15em;",
"\">",
slash_mark_char,
"</span>"
)

x_str[has_a_fraction] <-
paste0(
gsub(" ", "&#8239;", non_fraction_part),
gsub(" ", narrow_no_break_space_char, non_fraction_part),
num_vec, slash_mark, denom_vec
)

Expand Down Expand Up @@ -2842,7 +2866,7 @@ fmt_duration <- function(
cli::cli_abort(c(
"The value provided for `trim_zero_units` is invalid. Either use:",
"*" = "`TRUE` or `FALSE`, or",
"*" = "A vector with any of the keywords `\"leading\"`, `\"trailing\"`, or `\"internal\"`."
"*" = "A vector with any of the keywords \"leading\", \"trailing\", or \"internal\"."
))
}

Expand Down Expand Up @@ -2871,7 +2895,7 @@ fmt_duration <- function(
)
) {
cli::cli_abort(c(
"The `fmt_duration()` function can only be used on `columns` of certain types:",
"The `fmt_duration()` function can only be used on `columns` of certain types.",
"*" = "Allowed types are `numeric` and `difftime`."
))
}
Expand All @@ -2885,11 +2909,10 @@ fmt_duration <- function(
) &&
is.null(input_units)
) {
stop(
"When there are numeric columns to format, `input_units` must not be `NULL`:\n",
"* Use one of `\"seconds\"`, `\"minutes\"`, `\"hours\"`, `\"days\"`, or `\"weeks\"`",
call. = FALSE
)
cli::cli_abort(c(
"When there are numeric columns to format, `input_units` must not be `NULL`.",
"*" = "Use one of \"seconds\", \"minutes\", \"hours\", \"days\", or \"weeks\"."
))
}

# Initialize `colon_sep_params` list
Expand Down Expand Up @@ -3027,12 +3050,12 @@ fmt_duration <- function(
validate_trim_zero_units <- function(trim_zero_units) {

if (!all(trim_zero_units %in% c("leading", "trailing", "internal"))) {
stop(
"The character vector provided for `trim_zero_units` is invalid:\n",
"* It should only contain any of the keywords `\"leading\"`, `\"trailing\"`,
or ", "`\"internal\"`",
call. = FALSE
)

cli::cli_abort(c(
"The character vector provided for `trim_zero_units` is invalid.",
"*" = "It should only contain any of the keywords \"leading\", \"trailing\",
or ", "\"internal\"."
))
}
}

Expand All @@ -3054,7 +3077,7 @@ validate_duration_input_units <- function(input_units) {
if (!all(input_units %in% time_parts_vec) || length(input_units) != 1) {

cli::cli_abort(c(
"The value of `input_units` for `fmt_duration()` is invalid:",
"The value of `input_units` for `fmt_duration()` is invalid.",
"*" = "Only one of the \"weeks\", \"days\", \"hours\", \"minutes\", or
\"seconds\" time parts should be present."
))
Expand Down Expand Up @@ -3091,9 +3114,9 @@ validate_duration_output_units <- function(output_units) {
if (!all(output_units %in% time_parts_vec)) {

cli::cli_abort(c(
"There are invalid components in the `output_units` input to `fmt_duration()`:",
"There are invalid components in the `output_units` input to `fmt_duration()`.",
"*" = "Only the \"weeks\", \"days\", \"hours\", \"minutes\", and \"seconds\`
time parts should be present"
time parts should be present."
))
}
}
Expand Down
Loading

0 comments on commit 165c6d0

Please sign in to comment.