/
build-news.R
260 lines (219 loc) · 6.18 KB
/
build-news.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#' Build news section
#'
#' Your `NEWS.md` is parsed in to sections based on your use of headings.
#'
#' The `NEWS.md` file should be formatted with level one headings (`#`)
#' containing the package name and version number, level two headings (`##`)
#' with topic headings and lists of news bullets. Commonly used level two
#' headings include 'Major changes', 'Bug fixes', or 'Minor changes'.
#'
#' ```
#' # pkgdown 0.1.0.9000
#'
#' ## Major changes
#'
#' - Fresh approach based on the staticdocs package. Site configuration now based
#' on YAML files.
#' ```
#'
#' If the package is available on CRAN, release dates will be added to versions
#' in level-one headings, and "Unreleased" will be added versions that are not on
#' CRAN.
#'
#' Issues and contributors mentioned in news items are automatically linked to
#' github if a `URL` entry linking to github.com is provided in the package
#' `DESCRIPTION`.
#'
#' ```
#' ## Major changes
#'
#' - Lots of bug fixes (@hadley, #100)
#' ```
#'
#' @section YAML config:
#'
#' To automatically link to release announcements, include a `releases`
#' section.
#'
#' ```
#' news:
#' releases:
#' - text: "usethis 1.3.0"
#' href: https://www.tidyverse.org/articles/2018/02/usethis-1-3-0/
#' - text: "usethis 1.0.0 (and 1.1.0)"
#' href: https://www.tidyverse.org/articles/2017/11/usethis-1.0.0/
#' ```
#'
#' Control whether news is present on one page or multiple pages with the
#' `one_page` field. The default is `true`.
#'
#' ```
#' news:
#' - one_page: false
#' ```
#' @seealso [Tidyverse style for News](http://style.tidyverse.org/news.html)
#'
#' @inheritParams build_articles
#' @export
build_news <- function(pkg = ".",
override = list(),
preview = NA) {
pkg <- section_init(pkg, depth = 1L, override = override)
one_page <- purrr::pluck(pkg, "meta", "news", 1, "one_page", .default = TRUE)
if (!has_news(pkg$src_path))
return()
rule("Building news")
dir_create(path(pkg$dst_path, "news"))
if (one_page) {
build_news_single(pkg)
} else {
build_news_multi(pkg)
}
preview_site(pkg, "news", preview = preview)
}
build_news_single <- function(pkg) {
news <- data_news(pkg)
render_page(
pkg,
"news",
list(
contents = purrr::transpose(news),
pagetitle = "Changelog",
source = github_source_links(pkg$github_url, "NEWS.md")
),
path("news", "index.html")
)
}
build_news_multi <- function(pkg) {
news <- data_news(pkg)
major <- factor(news$major_dev, levels = unique(news$major_dev))
news_paged <- tibble::tibble(
version = levels(major),
file_out = paste0("news-", version, ".html"),
contents = news[c("html", "version", "anchor")] %>% split(major)
)
render_news <- function(version, file_out, contents) {
render_page(
pkg,
"news",
list(
version = version,
contents = rev(purrr::transpose(contents)),
pagetitle = paste0("Version ", version)
),
path("news", file_out),
)
}
news_paged %>% purrr::pmap(render_news)
render_page(
pkg,
"news-index",
list(versions = news_paged %>% purrr::transpose(), pagetitle = "News"),
path("news", "index.html")
)
}
globalVariables(".")
data_news <- function(pkg = ".") {
pkg <- as_pkgdown(pkg)
scoped_file_context(depth = 1L)
html <- markdown(path(pkg$src_path, "NEWS.md"))
sections <- xml2::read_html(html) %>%
xml2::xml_find_all("./body/div")
titles <- sections %>%
xml2::xml_find_first(".//h1|h2") %>%
xml2::xml_text(trim = TRUE)
if (any(is.na(titles))) {
stop("Invalid NEWS.md: bad nesting of titles", call. = FALSE)
}
anchors <- sections %>%
xml2::xml_attr("id")
re <- regexec("^([[:alnum:],\\.]+)\\s+((\\d+[.-]\\d+)(?:[.-]\\d+)*)", titles)
pieces <- regmatches(titles, re)
# Only keep sections with unambiguous version
is_version <- purrr::map_int(pieces, length) == 4
pieces <- pieces[is_version]
sections <- sections[is_version]
anchors <- anchors[is_version]
major <- purrr::map_chr(pieces, 4)
version <- purrr::map_chr(pieces, 3)
timeline <- pkg_timeline(pkg$package)
html <- sections %>%
purrr::walk(tweak_code) %>%
purrr::walk2(version, tweak_news_heading, timeline = timeline) %>%
purrr::map_chr(as.character) %>%
purrr::map_chr(add_github_links, pkg = pkg)
news <- tibble::tibble(
version = version,
is_dev = is_dev(version),
major = major,
major_dev = ifelse(is_dev, "unreleased", major),
anchor = anchors,
html = html
)
news
}
navbar_news <- function(pkg) {
releases_meta <- pkg$meta$news$releases
if (!is.null(releases_meta)) {
menu("News",
c(
list(menu_text("Releases")),
releases_meta,
list(
menu_spacer(),
menu_link("Changelog", "news/index.html")
)
)
)
} else if (has_news(pkg$src_path)) {
menu_link("Changelog", "news/index.html")
}
}
has_news <- function(path = ".") {
file_exists(path(path, "NEWS.md"))
}
is_dev <- function(version) {
dev_v <- version %>%
package_version() %>%
purrr::map(unclass) %>%
purrr::map_dbl(c(1, 4), .null = 0)
dev_v > 0
}
pkg_timeline <- function(package) {
if (!has_internet()) {
return(NULL)
}
url <- paste0("http://crandb.r-pkg.org/", package, "/all")
resp <- httr::GET(url)
if (httr::http_error(resp)) {
return(NULL)
}
content <- httr::content(resp)
timeline <- content$timeline
data.frame(
version = names(timeline),
date = as.Date(unlist(timeline)),
stringsAsFactors = FALSE
)
}
rel_date_html <- function(date) {
if (is.na(date))
return("<small> Unreleased</small>")
paste0("<small> ", date, "</small>")
}
tweak_news_heading <- function(x, versions, timeline) {
x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_set_attr("class", "page-header")
if (is.null(timeline))
return(x)
date <- timeline$date[match(versions, timeline$version)]
date_str <- ifelse(is.na(date), "Unreleased", as.character(date))
date_nodes <- paste(" <small>", date_str, "</small>", collapse = "") %>%
xml2::read_html() %>%
xml2::xml_find_all(".//small")
x %>%
xml2::xml_find_all(".//h1") %>%
xml2::xml_add_child(date_nodes, .where = 1)
invisible()
}