https://github.com/cran/pkgdown
Raw File
Tip revision: 74b598025322cd572dcd58326f0c5fb465f503ab authored by Hadley Wickham on 23 June 2022, 13:00:02 UTC
version 2.0.5
Tip revision: 74b5980
build-redirects.R
build_redirects <- function(pkg = ".",
                            override = list()) {
  pkg <- section_init(pkg, depth = 1L, override = override)

  redirects <- c(
    article_redirects(pkg),
    pkg$meta$redirects
  )

  if (is.null(redirects)) {
    return(invisible())
  }

  rule("Building redirects")
  if (is.null(pkg$meta$url)) {
    abort(sprintf("%s required to generate redirects", pkgdown_field(pkg, "url")))
  }

  purrr::iwalk(
    redirects,
    build_redirect,
    pkg = pkg
  )
}

build_redirect <- function(entry, index, pkg) {
  if (!is.character(entry) || length(entry) != 2) {
    abort(
      sprintf(
        "Entry %s in %s must be a character vector of length 2.",
        index,
        pkgdown_field(pkg, "redirects")
      )
    )
  }

  new <- entry[2]
  old <- path(pkg$dst_path, entry[1])

  path <- find_template("layout", "redirect", pkg = pkg)
  template <- read_file(path)

  url <- sprintf("%s/%s%s", pkg$meta$url, pkg$prefix, new)
  lines <- whisker::whisker.render(template, list(url = url))
  dir_create(path_dir(old))
  write_lines(lines, old)
}

article_redirects <- function(pkg) {
  if (is.null(pkg$meta$url)) {
    return(NULL)
  }

  is_vig_in_articles <- path_has_parent(pkg$vignettes$name, "articles")
  if (!any(is_vig_in_articles)) {
    return(NULL)
  }

  articles <- pkg$vignettes$file_out[is_vig_in_articles]
  purrr::map(articles, ~ paste0(c("articles/", ""), .x))
}
back to top