Skip to content

Commit

Permalink
Support ANSI sequences in index.md (#2787)
Browse files Browse the repository at this point in the history
Co-authored-by: Maëlle Salmon <[email protected]>
  • Loading branch information
krlmlr and maelle authored Nov 8, 2024
1 parent 3bee460 commit c40ba2c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

# pkgdown 2.1.1

* Added keyboard shortcut, `/`, to focus search bar (#2423)
* Added keyboard shortcut, `/`, to focus search bar (#2423).
* The `BugReports` field can now be an email (@catalamarti, #2275).
* New `clean_cache()` function removes the contents of the cache directory (#2718).
* pkgdown now depends on R >= 4.0.0 (#2714)
* Updated GitHub Actions advice and workflows around Quarto install (@tanho63, #2743)
* pkgdown now depends on R >= 4.0.0 (#2714).
* Updated GitHub Actions advice and workflows around Quarto install (@tanho63, #2743).
* Support ANSI sequences in `index.md` (@krlmlr, #2787).

# pkgdown 2.1.0

Expand Down
4 changes: 2 additions & 2 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ markdown_text <- function(pkg, text, ...) {
markdown_path_html(pkg, md_path, ...)
}

markdown_text_inline <- function(pkg,
markdown_text_inline <- function(pkg,
text,
error_path,
error_call = caller_env()) {
Expand Down Expand Up @@ -62,7 +62,7 @@ markdown_body <- function(pkg, path, strip_header = FALSE) {
markdown_path_html <- function(pkg, path, strip_header = FALSE) {
html_path <- withr::local_tempfile()
convert_markdown_to_html(pkg, path, html_path)
xml <- xml2::read_html(html_path, encoding = "UTF-8")
xml <- read_html_keep_ansi(html_path)
if (!inherits(xml, "xml_node")) {
return(NULL)
}
Expand Down
6 changes: 3 additions & 3 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ check_open_graph <- function(pkg, og, file_path = NULL, call = caller_env()) {
if (is.null(og)) {
return()
}

is_yaml <- is.null(file_path)
base_path <- if (is_yaml) "template.opengraph" else "opengraph"

Expand Down Expand Up @@ -220,7 +220,7 @@ check_open_graph_list <- function(pkg,
}
not <- obj_type_friendly(x)
config_abort(
pkg,
pkg,
"{.field {error_path}} must be a list, not {not}.",
path = file_path,
call = error_call
Expand Down Expand Up @@ -259,7 +259,7 @@ same_contents <- function(path, contents) {

cur_contents <- paste0(read_lines(path), collapse = "\n")
cur_hash <- digest::digest(cur_contents, serialize = FALSE)

identical(new_hash, cur_hash)
}

Expand Down
11 changes: 8 additions & 3 deletions R/tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ tweak_useless_toc <- function(html) {

# Update file on disk -----------------------------------------------------

update_html <- function(path, tweak, ...) {

read_html_keep_ansi <- function(path) {
raw <- read_file(path)
# Following the xml 1.0 spec, libxml2 drops low-bit ASCII characters
# so we convert to \u2029, relying on downlit to convert back in
# token_escape().
raw <- gsub("\033", "\u2029", raw, fixed = TRUE)
html <- xml2::read_html(raw, encoding = "UTF-8")
# Use charToRaw() to always interpret as string,
# even for length 1 vectors
xml2::read_html(charToRaw(raw), encoding = "UTF-8")
}

update_html <- function(path, tweak, ...) {
html <- read_html_keep_ansi(path)
tweak(html, ...)

xml2::write_html(html, path, format = FALSE)
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@
Error in `config_math_rendering_()`:
! In _pkgdown.yml, template.math-rendering must be one of mathml, mathjax, and katex, not math.

# preserves ANSI characters

Code
markdown_text(pkg, sprintf("prefer %s", cli::col_blue("a")))
Output
{html_document}
<html>
[1] <body><p>prefer \u2029[34ma\u2029[39m</p></body>

12 changes: 12 additions & 0 deletions tests/testthat/test-build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@ test_that("allow email in BugReports", {
html <- xml2::read_html(data_home_sidebar(pkg))
expect_snapshot(xpath_xml(html, ".//li/a"))
})

test_that("ANSI are handled", {
withr::local_options(cli.num_colors = 256)
pkg <- local_pkgdown_site()

pkg <- pkg_add_file(pkg, "index.md", sprintf("prefer %s", cli::col_blue("a")))
suppressMessages(build_home_index(pkg))

html <- xml2::read_html(path(pkg$dst_path, "index.html"))
readme_p <- xml2::xml_find_first(html, ".//main[@id='main']/p")
expect_equal(xml2::xml_text(readme_p), "prefer \u2029[34ma\u2029[39m")
})
8 changes: 8 additions & 0 deletions tests/testthat/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ test_that("validates math yaml", {
config_math_rendering_(`math-rendering` = "math")
})
})

test_that("preserves ANSI characters", {
withr::local_options(cli.num_colors = 256)
pkg <- local_pkgdown_site()
expect_snapshot(
markdown_text(pkg, sprintf("prefer %s", cli::col_blue("a")))
)
})

0 comments on commit c40ba2c

Please sign in to comment.