https://github.com/cran/pkgdown
Revision a4818cca780db29741daa700c63a640567d99327 authored by Hadley Wickham on 03 May 2018, 07:55:54 UTC, committed by cran-robot on 03 May 2018, 07:55:54 UTC
0 parent
Raw File
Tip revision: a4818cca780db29741daa700c63a640567d99327 authored by Hadley Wickham on 03 May 2018, 07:55:54 UTC
version 1.0.0
Tip revision: a4818cc
metadata.R
#' @importFrom memoise memoise
NULL

remote_urls <- function(package) {
  local <- context_get("local_packages")
  if (has_name(local, package)) {
    base_url <- local[[package]]
    list(
      reference = path(base_url, "reference"),
      article = path(base_url, "articles")
    )
  } else {
    remote_metadata(package)$urls
  }
}

remote_package_reference_url <- function(package) {
  remote_urls(package)$reference
}
remote_package_article_url <- function(package) {
  remote_urls(package)$article
}

remote_metadata <- memoise(function(package) {
  path <- find.package(package, quiet = TRUE)
  if (length(path) == 0) {
    return(NULL)
  }

  desc <- read_desc(path)
  urls <- desc$get_urls()

  for (url in urls) {
    url <- paste0(url, "/pkgdown.yml")

    yaml <- tryCatch(fetch_yaml(url), error = function(e) NULL)
    if (is.list(yaml)) {
      if (has_name(yaml, "articles")) {
        yaml$articles <- unlist(yaml$articles)
      }
      return(yaml)
    }
  }

  NULL
})

fetch_yaml <- function(url) {
  resp <- httr::GET(url, httr::timeout(3))
  httr::stop_for_status(resp)

  text <- httr::content(resp, as = "text", encoding = "UTF-8")
  yaml::yaml.load(text)
}
back to top