Skip to content

Commit

Permalink
more typst styles
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Dec 8, 2024
1 parent 478c2fc commit b5aae2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
49 changes: 3 additions & 46 deletions R/style_notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,17 @@ setMethod(
signature = "tinytable_tabularray",
definition = function(x, ...) {
styles <- x@style_notes
x@notes <- lapply(x@notes, style_string_html, styles)
x@notes <- lapply(x@notes, style_string_latex, styles)
return(x)
})


# LaTeX: tabularray
# Typst
setMethod(
f = "style_notes",
signature = "tinytable_typst",
definition = function(x, ...) {
styles <- x@style_notes

if (length(x@notes) == 0) {
return(x)
}

sty <- NULL
if (isTRUE(styles[["italic"]])) {
sty <- c(sty, 'style: "italic"')
}

if (isTRUE(styles[["bold"]])) {
sty <- c(sty, 'weight: "bold"')
}

template <- paste0("text(", paste(sty, collapse = ", "), ", [%s])")
x@notes <- lapply(x@notes, function(k) sprintf(template, k))

x@notes <- lapply(x@notes, style_string_typst, styles)
return(x)
})



style_string_html <- function(n, styles) {
if (isTRUE(styles[["italic"]])) {
n <- sprintf("<i>%s</i>", n)
}
if (isTRUE(styles[["strikeout"]])) {
n <- sprintf("<s>%s</s>", n)
}
if (isTRUE(styles[["underline"]])) {
n <- sprintf("<u>%s</u>", n)
}
if (isTRUE(styles[["bold"]])) {
n <- sprintf("<b>%s</b>", n)
}
if (isTRUE(styles[["monospace"]])) {
n <- sprintf("<code>%s</code>", n)
}
if (!is.null(styles[["color"]])) {
n <- sprintf("<span style='color:%s'>%s</span>", styles[["color"]], n)
}
if (!is.null(styles[["fontsize"]])) {
n <- sprintf("<span style='font-size:%s'>%s</span>", styles[["fontsize"]], n)
}
n
}
32 changes: 31 additions & 1 deletion R/style_string.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,37 @@ style_string_latex <- function(n, styles) {
n <- sprintf("\\textcolor{%s}{%s}", styles[["color"]], n)
}
if (!is.null(styles[["fontsize"]])) {
n <- sprintf("{\\fontsize{%s}{%s}\\selectfont %s}", styles[["fontsize"]], styles[["fontsize"]], n)
n <- sprintf("{\\fontsize{%sem}{%sem}\\selectfont %s}", styles[["fontsize"]], styles[["fontsize"]], n)
}
n
}


style_string_typst <- function(n, styles) {
sty <- NULL
if (isTRUE(styles[["italic"]])) {
sty <- c(sty, 'style: "italic"')
}
if (isTRUE(styles[["bold"]])) {
sty <- c(sty, 'weight: "bold"')
}
if (isTRUE(styles[["strikeout"]])) {
# not sure how to do this
}
if (isTRUE(styles[["underline"]])) {
# not sure how to do this
}
if (!is.null(styles[["fontsize"]])) {
fs <- sprintf("size: %sem", styles[["fontsize"]])
sty <- c(sty, fs)
}
if (!is.null(styles[["color"]])) {
col <- styles[["color"]]
if (grepl("^#", col)) col <- sprintf('rgb("%s")', col)
col <- sprintf("fill: %s", col)
sty <- c(sty, col)
}
template <- paste0("text(", paste(sty, collapse = ", "), ", [%s])")
out <- sprintf(template, n)
return(out)
}

0 comments on commit b5aae2e

Please sign in to comment.