Skip to content

Commit

Permalink
refactor typst to use raw blocks. better overflow. no escaping necess…
Browse files Browse the repository at this point in the history
…ary any more
  • Loading branch information
coolbutuseless committed May 2, 2024
1 parent 4ca6424 commit 095e054
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: emphatic
Type: Package
Title: Highlight R Output using Colouring
Version: 0.1.6.9006
Version: 0.1.6.9007
Author: mikefc
Maintainer: mikefc <[email protected]>
Description: User-defined colouring of data.frames and other R output.
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Remove `scale_mode` argument to `hl()`
* Support for output to `typst` in quarto docs
* Updated the list of characters to escape for typst
* Switched to `raw` text blocks for output as they have better overflow
behaviour. Still not perfect, but better!

# emphatic 0.1.6 2024-04-27

Expand Down
33 changes: 17 additions & 16 deletions R/core-typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ col2fill_typst <- function(colours) {

ifelse(
no_colour,
'#[', # do nothing
paste0('#highlight(fill: rgb("', colours, '"))[')
'#[`', # do nothing
paste0('#highlight(fill: rgb("', colours, '"))[`')
)

}
Expand Down Expand Up @@ -47,7 +47,7 @@ col2text_typst <- function(colours) {

}

reset_typst <- "]]"
reset_typst <- "`]]"
underline_on_typst <- "#underline["
underline_off_typst <- "]"

Expand All @@ -60,23 +60,24 @@ underline_off_typst <- "]"


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Escape HTML by replacing special characters
# Escape typst by replacing special characters
# Now that export is using 'raw' blocks, don't need to escape anything!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
escape_typst <- function(x) {
x <- enc2utf8(x)
# x = gsub("([#$%&_{}])", "\\\\\\1", x)
x <- gsub(" ", "\\\\u{00a0}", x, useBytes = TRUE)
x <- gsub("<" , "\\\\<", x, useBytes = TRUE)
x <- gsub("#" , "\\\\#", x, useBytes = TRUE)
x <- gsub("_" , "\\\\_", x, useBytes = TRUE)
x <- gsub("~" , "\\\\~", x, useBytes = TRUE)
x <- gsub("~" , '\\\\"', x, useBytes = TRUE)
x <- gsub("@" , "\\\\@", x, useBytes = TRUE)
x <- gsub("`" , "\\\\`", x, useBytes = TRUE)
x <- gsub("\\*" , "\\\\*", x, useBytes = TRUE)
x <- gsub("\\[" , "\\\\[", x, useBytes = TRUE)
x <- gsub("\\]" , "\\\\]", x, useBytes = TRUE)
x <- gsub("\\$" , "\\\\$", x, useBytes = TRUE)
# x <- gsub(" ", "\\\\u{00a0}", x, useBytes = TRUE)
# x <- gsub("<" , "\\\\<", x, useBytes = TRUE)
# x <- gsub("#" , "\\\\#", x, useBytes = TRUE)
# x <- gsub("_" , "\\\\_", x, useBytes = TRUE)
# x <- gsub("~" , "\\\\~", x, useBytes = TRUE)
# x <- gsub("~" , '\\\\"', x, useBytes = TRUE)
# x <- gsub("@" , "\\\\@", x, useBytes = TRUE)
# x <- gsub("`" , "\\\\`", x, useBytes = TRUE)
# x <- gsub("\\*" , "\\\\*", x, useBytes = TRUE)
# x <- gsub("\\[" , "\\\\[", x, useBytes = TRUE)
# x <- gsub("\\]" , "\\\\]", x, useBytes = TRUE)
# x <- gsub("\\$" , "\\\\$", x, useBytes = TRUE)
x <- gsub("\n" , "\\\\\n", x, useBytes = TRUE)
Encoding(x) <- 'UTF-8'
x
Expand Down
26 changes: 14 additions & 12 deletions R/core.R
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ as_character_inner <- function(m,
this_rownames <- escape_latex(this_rownames)
} else if (mode == 'typst') {
this_rownames <- escape_typst(this_rownames)
this_rownames <- paste0("`", this_rownames, "`")
}
ansi_mat <- cbind(this_rownames, ansi_mat)
col_names <- c(sprintf(fmt, ''), col_names)
Expand All @@ -489,6 +490,7 @@ as_character_inner <- function(m,
rownames(m) <- escape_latex(rownames(m))
} else if (mode == 'typst') {
rownames(m) <- escape_typst(rownames(m))
rownames(m) <- paste0("`", rownames(m), "`")
}
col_names <- c('', col_names)
}
Expand All @@ -500,18 +502,18 @@ as_character_inner <- function(m,
header <- NULL
} else {
header <- paste(col_names, collapse = " ")
if (mode == 'ansi') {
header <- paste0(underline_on_ansi, header, underline_off_ansi)
} else if (mode == 'html') {
header <- escape_html(header)
header <- paste0(underline_on_html, header, underline_off_html)
} else if (mode == 'latex') {
header <- escape_latex(header)
header <- paste0(underline_on_latex, header, underline_off_latex)
} else if (mode == 'typst') {
header <- escape_typst(header)
header <- paste0(underline_on_typst, header, underline_off_typst)
}
if (mode == 'ansi') {
header <- paste0(underline_on_ansi, header, underline_off_ansi)
} else if (mode == 'html') {
header <- escape_html(header)
header <- paste0(underline_on_html, header, underline_off_html)
} else if (mode == 'latex') {
header <- escape_latex(header)
header <- paste0(underline_on_latex, header, underline_off_latex)
} else if (mode == 'typst') {
header <- escape_typst(header)
header <- paste0(underline_on_typst, '`', header, '`', underline_off_typst)
}
}

body <- apply(ansi_mat, 1, paste, collapse = '')
Expand Down
7 changes: 5 additions & 2 deletions R/knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ as_latex <- function(x, ...) {
#' @param ... other arguments passed to \code{as.character.emphatic}
#' @param font name of font. Default: 'Courier New'
#' @param font_size font size in points. default: 10
#' @param line_spacing line spacing in \code{em} units. Default: 0.3
#'
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
as_typst <- function(x, ..., font = 'Courier New', font_size = 10) {
as_typst <- function(x, ..., font_size = 10, font = NA, line_spacing = 0.3) {

res <- as.character(x, ..., mode = 'typst')

res <- paste(
"\n```{=typst}\n",
"#[",
paste0('#set text(font: "', font, '", size: ', font_size, 'pt, hyphenate: false)'),
paste0('#set text(size: ', font_size, 'pt, hyphenate: false)'),
paste0('#set par(leading: ', line_spacing, 'em)'),
ifelse(is.na(font), '', paste0('#show raw: set text(font: "', font, '")')),
res,
"]",
"\n```\n",
Expand Down
6 changes: 4 additions & 2 deletions man/as_typst.Rd

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

0 comments on commit 095e054

Please sign in to comment.