From 1e16b43faef911ca42134455a39ce06129676b30 Mon Sep 17 00:00:00 2001 From: Sebastian Krantz Date: Thu, 12 Oct 2023 20:25:52 +0200 Subject: [PATCH 1/3] Make pkgdown work for collapse. --- R/rd.R | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/R/rd.R b/R/rd.R index 91d03c1c0..9ad402727 100644 --- a/R/rd.R +++ b/R/rd.R @@ -2,18 +2,32 @@ rd_text <- function(x, fragment = TRUE) { con <- textConnection(x) on.exit(close(con), add = TRUE) - set_classes(tools::parse_Rd(con, fragment = fragment, encoding = "UTF-8")) + set_classes(parse_Rd2(con, fragment = fragment, encoding = "UTF-8")) +} + +parse_Rd2 <- function(file, ...) { + substcr <- function(x) gsub("\\cr\\cr", "\\cr", gsub("\\cr \\cr", "\\cr", x, fixed = TRUE), fixed = TRUE) + lines <- read_lines(file) + if(endsWith(file, "collapse-options.Rd")) { + lines <- gsub("\\tab\\tab", "\\tab", gsub("\\tab \\tab", "\\tab", lines, fixed = TRUE), fixed = TRUE) + lines <- gsub("\\tabular{lll}", "\\tabular{ll}", lines, fixed = TRUE) + } + tmp <- tempfile(fileext = ".Rd") + write_lines(substcr(substcr(substcr(substcr(substcr(lines))))), tmp) + res <- tools::parse_Rd(tmp, ...) + file.remove(tmp) + res } rd_file <- function(path, pkg_path = NULL) { if (getRversion() >= "3.4.0") { macros <- tools::loadPkgRdMacros(pkg_path) - set_classes(tools::parse_Rd(path, macros = macros, encoding = "UTF-8")) + set_classes(parse_Rd2(path, macros = macros, encoding = "UTF-8")) } else if (getRversion() >= "3.2.0") { macros <- tools::loadPkgRdMacros(pkg_path, TRUE) - set_classes(tools::parse_Rd(path, macros = macros, encoding = "UTF-8")) + set_classes(parse_Rd2(path, macros = macros, encoding = "UTF-8")) } else { - set_classes(tools::parse_Rd(path, encoding = "UTF-8")) + set_classes(parse_Rd2(path, encoding = "UTF-8")) } } From 738482e7df45644c0baa3ff62bf9f45e2f7f0c2f Mon Sep 17 00:00:00 2001 From: Sebastian Krantz Date: Fri, 13 Oct 2023 00:05:37 +0200 Subject: [PATCH 2/3] Always showing badges + removing download badges. --- R/build-home-index.R | 2 +- R/tweak-homepage.R | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/build-home-index.R b/R/build-home-index.R index 3c39aca2a..4f86fe615 100644 --- a/R/build-home-index.R +++ b/R/build-home-index.R @@ -22,7 +22,7 @@ build_home_index <- function(pkg = ".", quiet = TRUE) { render_page(pkg, "home", data, "index.html", quiet = quiet) strip_header <- isTRUE(pkg$meta$home$strip_header) - hide_badges <- pkg$development$mode == "release" && !pkg$development$in_dev + hide_badges <- FALSE # pkg$development$mode == "release" && !pkg$development$in_dev update_html( dst_path, diff --git a/R/tweak-homepage.R b/R/tweak-homepage.R index b1a77797a..ac177df5c 100644 --- a/R/tweak-homepage.R +++ b/R/tweak-homepage.R @@ -115,7 +115,8 @@ badges_extract <- function(html) { xml2::xml_remove(x) - as.character(badges) + badges <- as.character(badges) + badges[!grepl("download", badges, ignore.case = TRUE)] } badges_extract_text <- function(x) { From 46910a1bc381cafa6421488ffcc61ef75e8d4dcf Mon Sep 17 00:00:00 2001 From: Sebastian Krantz Date: Fri, 13 Oct 2023 17:07:00 +0200 Subject: [PATCH 3/3] Improving function reference index rendering: removing aliases and shorthands for cleaner presentation. --- R/build-reference-index.R | 9 ++++++++- R/rd.R | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/R/build-reference-index.R b/R/build-reference-index.R index d8e4df7e5..aed87fe87 100644 --- a/R/build-reference-index.R +++ b/R/build-reference-index.R @@ -1,6 +1,13 @@ data_reference_index <- function(pkg = ".") { pkg <- as_pkgdown(pkg) + # This removes shorthands from reference index + sh <- getNamespace("collapse")[[".SHORTHANDS"]] + sh <- sh[!endsWith(sh, "<-")] + sh <- c(sh, paste0("`", sh), ".quantile", ".range", "`[", "`$") + pkg$topics$funs <- lapply(pkg$topics$funs, function(y) if(length(y)) + y[rowSums(do.call(cbind, lapply(sh, startsWith, x = y))) <= 0] else y) + meta <- pkg$meta[["reference"]] %||% default_reference_index(pkg) if (length(meta) == 0) { return(list()) @@ -17,7 +24,7 @@ data_reference_index <- function(pkg = ".") { rows <- Filter(function(x) !x$is_internal, rows) print_yaml(list( - pagetitle = tr_("Function reference"), + pagetitle = tr_("Function Reference"), rows = rows, has_icons = has_icons )) diff --git a/R/rd.R b/R/rd.R index 9ad402727..63a332167 100644 --- a/R/rd.R +++ b/R/rd.R @@ -12,6 +12,16 @@ parse_Rd2 <- function(file, ...) { lines <- gsub("\\tab\\tab", "\\tab", gsub("\\tab \\tab", "\\tab", lines, fixed = TRUE), fixed = TRUE) lines <- gsub("\\tabular{lll}", "\\tabular{ll}", lines, fixed = TRUE) } + clpns <- getNamespace("collapse") + nam <- names(clpns) + rm_alias <- c(clpns[[".SHORTHANDS"]], + setdiff(nam[startsWith(nam, ".")], c(".", ".c")), + nam[startsWith(nam, "[")], + nam[startsWith(nam, "$")]) + rm_alias <- paste0("\\alias{", rm_alias) + lines <- lines[!startsWith(lines, "%")] + lines <- lines[!startsWith(lines, "\\alias{A")] + lines <- lines[rowSums(sapply(rm_alias, startsWith, x = lines, USE.NAMES = FALSE)) <= 0] tmp <- tempfile(fileext = ".Rd") write_lines(substcr(substcr(substcr(substcr(substcr(lines))))), tmp) res <- tools::parse_Rd(tmp, ...)