diff --git a/DESCRIPTION b/DESCRIPTION index 01aa653..dbe4e71 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 Description: User-defined colouring of data.frames and other R output. diff --git a/NEWS.md b/NEWS.md index 0d80c03..c3f9170 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/core-typst.R b/R/core-typst.R index a17d366..a5baed1 100644 --- a/R/core-typst.R +++ b/R/core-typst.R @@ -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, '"))[`') ) } @@ -47,7 +47,7 @@ col2text_typst <- function(colours) { } -reset_typst <- "]]" +reset_typst <- "`]]" underline_on_typst <- "#underline[" underline_off_typst <- "]" @@ -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 diff --git a/R/core.R b/R/core.R index 2e59665..3ce4d45 100644 --- a/R/core.R +++ b/R/core.R @@ -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) @@ -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) } @@ -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 = '') diff --git a/R/knitr.R b/R/knitr.R index e5e46e5..bb314b0 100644 --- a/R/knitr.R +++ b/R/knitr.R @@ -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", diff --git a/man/as_typst.Rd b/man/as_typst.Rd index cf8cdfe..d0b1edc 100644 --- a/man/as_typst.Rd +++ b/man/as_typst.Rd @@ -4,16 +4,18 @@ \alias{as_typst} \title{Render an emphatic object to typst} \usage{ -as_typst(x, ..., font = "Courier New", font_size = 10) +as_typst(x, ..., font_size = 10, font = NA, line_spacing = 0.3) } \arguments{ \item{x}{emphatic object} \item{...}{other arguments passed to \code{as.character.emphatic}} +\item{font_size}{font size in points. default: 10} + \item{font}{name of font. Default: 'Courier New'} -\item{font_size}{font size in points. default: 10} +\item{line_spacing}{line spacing in \code{em} units. Default: 0.3} } \description{ Render an emphatic object to typst