-
Notifications
You must be signed in to change notification settings - Fork 336
/
build-home-index.R
121 lines (99 loc) · 3.19 KB
/
build-home-index.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
build_home_index <- function(pkg = ".", quiet = TRUE) {
pkg <- as_pkgdown(pkg)
scoped_package_context(pkg$package, pkg$topic_index, pkg$article_index)
scoped_file_context(depth = 0L)
src_path <- path_first_existing(pkg$src_path,
c("index.md", "README.md", "index.Rmd", "README.Rmd")
)
dst_path <- path(pkg$dst_path, "index.html")
data <- data_home(pkg)
if (is.null(src_path)) {
data$index <- linkify(pkg$desc$get("Description")[[1]])
} else {
data$index <- markdown(src_path)
}
render_page(pkg, "home", data, "index.html")
strip_header <- isTRUE(pkg$meta$home$strip_header)
update_html(dst_path, tweak_homepage_html, strip_header = strip_header)
invisible()
}
# Stripped down version of build_article
render_index <- function(pkg = ".", path, data = list(), quiet = TRUE) {
pkg <- as_pkgdown(pkg)
format <- build_rmarkdown_format(pkg, "article",
depth = 0L,
data = data, toc = FALSE
)
render_rmarkdown(
pkg = pkg,
input = path,
output = "index.html",
output_format = format,
quiet = quiet,
copy_images = FALSE
)
}
data_home <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
print_yaml(list(
pagetitle = pkg$meta$home[["title"]] %||%
cran_unquote(pkg$desc$get("Title")[[1]]),
sidebar = data_home_sidebar(pkg),
opengraph = list(description = pkg$meta$home[["description"]] %||%
cran_unquote(pkg$desc$get("Description")[[1]]))
))
}
data_home_sidebar <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
if (!is.null(pkg$meta$home$sidebar))
return(pkg$meta$home$sidebar)
paste0(
data_home_sidebar_links(pkg),
data_home_sidebar_license(pkg),
data_home_sidebar_community(pkg),
data_home_sidebar_citation(pkg),
data_home_sidebar_authors(pkg),
collapse = "\n"
)
}
data_home_sidebar_links <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
repo <- repo_link(pkg$package)
meta <- purrr::pluck(pkg, "meta", "home", "links")
links <- c(
link_url(paste0("Download from ", repo$repo), repo$url),
link_url("Browse source code", pkg$github_url),
if (pkg$desc$has_fields("BugReports"))
link_url("Report a bug", pkg$desc$get("BugReports")[[1]]),
purrr::map_chr(meta, ~ link_url(.$text, .$href))
)
sidebar_section("Links", links)
}
sidebar_section <- function(heading, bullets, class = make_slug(heading)) {
if (length(bullets) == 0)
return(character())
paste0(
"<div class='", class, "'>\n",
"<h2>", heading, "</h2>\n",
"<ul class='list-unstyled'>\n",
paste0("<li>", bullets, "</li>\n", collapse = ""),
"</ul>\n",
"</div>\n"
)
}
repo_link <- memoise(function(pkg) {
if (!has_internet()) {
return(NULL)
}
cran_url <- paste0("https://cloud.r-project.org/package=", pkg)
if (!httr::http_error(cran_url)) {
return(list(repo = "CRAN", url = cran_url))
}
# bioconductor always returns a 200 status, redirecting to /removed-packages/
bioc_url <- paste0("https://www.bioconductor.org/packages/", pkg)
req <- httr::HEAD(bioc_url)
if (!httr::http_error(req) && !grepl("removed-packages", req$url)) {
return(list(repo = "BIOC", url = bioc_url))
}
NULL
})