Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ANSI sequences in index.md #2787

Merged
merged 6 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")))
)
})
Loading