Skip to content

Commit

Permalink
read_html_keep_ansi()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Sep 28, 2024
1 parent 56013d6 commit 1784d8c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/markdown.R
Original file line number Diff line number Diff line change
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
9 changes: 6 additions & 3 deletions R/tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,17 @@ 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")
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

0 comments on commit 1784d8c

Please sign in to comment.