Skip to content

Commit ffa8112

Browse files
hadleyjennybc
andauthored
Use bs4 with dev pkgdown (#1502)
* Use bs4 with dev pkgdown Fixes https://github.com/r- lib/pkgdown/issues/1580 * Tweak news bullet * Now need pkgdown installed * Add missing namespace * Relax test * Tweak news bullet * Delete pkgdown_build_favicons() and has_logo() * usethis does not and should not depend on pkgdown * Reword * Move comment * Use YAML-parsing/writing approach here too * desc has a function for this (adding URLs) * Re-document() * Fix some tests Co-authored-by: Jenny Bryan <[email protected]>
1 parent dae1253 commit ffa8112

File tree

5 files changed

+50
-60
lines changed

5 files changed

+50
-60
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ usethis has a more sophisticated understanding of the default branch and gains s
3434
* `use_release_issue()` also takes bullets from `release_questions()`,
3535
for compatibility with `devtools::release()`.
3636

37+
* `use_pkgdown()` automatically uses bootstrap4 if the pkgdown version supports
38+
it (anticipated for pkgdown 2.0.0).
39+
3740
* `git_vaccinate()`, `edit_git_ignore()`, and `git_sitrep()` are more careful to
3841
consult, reveal, and set the `core.excludesFile` in user's Git configuration
3942
(#1461).

R/logo.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,3 @@ use_logo <- function(img, geometry = "240x278", retina = TRUE) {
5656
ui_code_block("# {pkg} <a href={ui_value(pd_link)}><img src={ui_path(logo_path)} align=\"right\" height=\"{height}\" /></a>")
5757
}
5858
}
59-
60-
has_logo <- function() {
61-
file_exists(proj_path("man", "figures", "logo.png")) ||
62-
file_exists(proj_path("man", "figures", "logo.svg"))
63-
}

R/pkgdown.R

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
#' [pkgdown](https://pkgdown.r-lib.org) makes it easy to turn your package into
55
#' a beautiful website. usethis provides two functions to help you use pkgdown:
66
#'
7-
#' * `use_pkgdown()`: creates a pkgdown config file, adds relevant files or
8-
#' directories to `.Rbuildignore` and `.gitignore`, and builds favicons if
9-
#' your package has a logo.
7+
#' * `use_pkgdown()`: creates a pkgdown config file and adds relevant files or
8+
#' directories to `.Rbuildignore` and `.gitignore`.
109
#'
1110
#' * `use_pkgdown_github_pages()`: implements the GitHub setup needed to
1211
#' automatically publish your pkgdown site to GitHub pages:
@@ -31,28 +30,37 @@ use_pkgdown <- function(config_file = "_pkgdown.yml", destdir = "docs") {
3130
check_is_package("use_pkgdown()")
3231
check_installed("pkgdown")
3332

34-
use_build_ignore(c(config_file, destdir))
35-
use_build_ignore("pkgdown")
33+
use_build_ignore(c(config_file, destdir, "pkgdown"))
3634
use_git_ignore(destdir)
3735

38-
ui_todo("
39-
Record your site's {ui_field('url')} in the pkgdown config file \\
40-
(optional, but recommended)")
36+
config <- pkgdown_config(destdir)
37+
config_path <- proj_path(config_file)
38+
write_over(config_path, yaml::as.yaml(config))
39+
edit_file(config_path)
40+
41+
invisible(TRUE)
42+
}
43+
44+
pkgdown_config <- function(destdir) {
45+
config <- list(
46+
url = NULL
47+
)
4148

42-
if (has_logo()) {
43-
pkgdown_build_favicons(proj_get(), overwrite = TRUE)
49+
if (pkgdown_version() >= "1.9000") {
50+
config$template <- list(bootstrap = 4L)
4451
}
4552

46-
config <- proj_path(config_file)
4753
if (!identical(destdir, "docs")) {
48-
write_over(config, paste("destination:", destdir))
54+
config$destination <- destdir
4955
}
50-
edit_file(config)
5156

52-
invisible(TRUE)
57+
config
5358
}
5459

55-
# tidyverse pkgdown setup ------------------------------------------------------
60+
# wrapping because I need to be able to mock this in tests
61+
pkgdown_version <- function() {
62+
utils::packageVersion("pkgdown")
63+
}
5664

5765
#' @rdname use_pkgdown
5866
#' @export
@@ -75,6 +83,8 @@ use_pkgdown_github_pages <- function() {
7583
}
7684
}
7785

86+
# tidyverse pkgdown setup ------------------------------------------------------
87+
7888
#' @details
7989
#' * `use_tidy_pkgdown_github_pages()` is basically
8090
#' [use_pkgdown_github_pages()], so does full pkgdown set up. Note that there
@@ -94,32 +104,22 @@ use_tidy_pkgdown_github_pages <- function() {
94104
use_pkgdown_url <- function(url, tr = NULL) {
95105
tr <- tr %||% target_repo(github_get = TRUE)
96106

97-
config <- pkgdown_config_path()
98-
config_lines <- read_utf8(config)
99-
url_line <- paste0("url: ", url)
100-
if (!any(grepl(url_line, config_lines))) {
101-
ui_done("
102-
Recording {ui_value(url)} as site's {ui_field('url')} in \\
103-
{ui_path(config)}")
104-
config_lines <- config_lines[!grepl("^url:", config_lines)]
105-
write_utf8(config, c(
106-
url_line,
107-
if (length(config_lines) && nzchar(config_lines[[1]])) "",
108-
config_lines
109-
))
107+
config_path <- pkgdown_config_path()
108+
ui_done("
109+
Recording {ui_value(url)} as site's {ui_field('url')} in \\
110+
{ui_path(config_path)}")
111+
config <- pkgdown_config_meta()
112+
if (has_name(config, "url")) {
113+
config$url <- url
114+
} else {
115+
config <- c(url = url, config)
110116
}
117+
write_utf8(config_path, yaml::as.yaml(config))
111118

112-
urls <- desc::desc_get_urls()
113-
if (!url %in% urls) {
114-
ui_done("Adding {ui_value(url)} to {ui_field('URL')} field in DESCRIPTION")
115-
ui_silence(
116-
use_description_field(
117-
"URL",
118-
glue_collapse(c(url, urls), ", "),
119-
overwrite = TRUE
120-
)
121-
)
122-
}
119+
ui_done("Adding {ui_value(url)} to {ui_field('URL')} field in DESCRIPTION")
120+
desc <- desc::desc(file = proj_get())
121+
desc$add_urls(url)
122+
desc$write()
123123

124124
gh <- gh_tr(tr)
125125
homepage <- gh("GET /repos/{owner}/{repo}")[["homepage"]]
@@ -215,16 +215,11 @@ use_pkgdown_travis <- function() {
215215

216216
tr <- target_repo(github_get = TRUE)
217217

218-
use_build_ignore("docs/")
218+
use_build_ignore(c("docs/", "pkgdown"))
219219
use_git_ignore("docs/")
220220
# TODO: suggest git rm -r --cache docs/
221221
# Can't currently detect if git known files in that directory
222222

223-
if (has_logo()) {
224-
pkgdown_build_favicons(proj_get(), overwrite = TRUE)
225-
use_build_ignore("pkgdown")
226-
}
227-
228223
ui_todo("
229224
Set up deploy keys by running {ui_code('travis::use_travis_deploy()')}")
230225
ui_todo("Insert the following code in {ui_path('.travis.yml')}")
@@ -242,9 +237,3 @@ use_pkgdown_travis <- function() {
242237

243238
invisible()
244239
}
245-
246-
# usethis itself should not depend on pkgdown
247-
# all usage of this wrapper is guarded by `check_installed("pkgdown")`
248-
pkgdown_build_favicons <- function(...) {
249-
get("build_favicons", asNamespace("pkgdown"), mode = "function")(...)
250-
}

man/use_pkgdown.Rd

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-pkgdown.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_that("use_pkgdown() creates and ignores the promised file/dir", {
88
local_interactive(FALSE)
99
with_mock(
1010
check_installed = function(pkg) TRUE,
11+
pkgdown_version = function() "1.9000",
1112
use_pkgdown()
1213
)
1314
expect_true(uses_pkgdown())
@@ -29,9 +30,11 @@ test_that("pkgdown_config_meta() returns a list", {
2930
local_interactive(FALSE)
3031
with_mock(
3132
check_installed = function(pkg) TRUE,
33+
pkgdown_version = function() "1.9000",
3234
use_pkgdown()
3335
)
34-
expect_equal(pkgdown_config_meta(), list())
36+
expect_type(pkgdown_config_meta(), "list")
37+
3538
writeLines(c("home:", " strip_header: true"), pkgdown_config_path())
3639
expect_equal(
3740
pkgdown_config_meta(),
@@ -44,6 +47,7 @@ test_that("pkgdown_url() returns correct data, warns if pedantic", {
4447
local_interactive(FALSE)
4548
with_mock(
4649
check_installed = function(pkg) TRUE,
50+
pkgdown_version = function() "1.9000",
4751
use_pkgdown()
4852
)
4953

0 commit comments

Comments
 (0)