Skip to content

Commit

Permalink
Finding badges (#996)
Browse files Browse the repository at this point in the history
Closes #670
  • Loading branch information
maelle authored and jayhesselberth committed Apr 26, 2019
1 parent b9db3ad commit 04e3f6e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pkgdown 1.3.0.9000 (development version)

* Badges can be extracted from the README paragraph coming after the comment `<!-- badges: start -->`, to build the "dev status" section of the sidebar (#670, @gaborcsardi, @maelle)

* One can override the title of the homepage via a `title` field in the config (#957, @maelle).

* Links to external documentation now point to [rdrr.io](https://rdrr.io) (#998).
Expand Down
10 changes: 8 additions & 2 deletions R/build-home.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@
#'
#' @section Badges:
#' Status badges are displayed in the sidebar under the section "Dev status".
#' This section is automatically populated if the first paragraph of the
#' homepage consists solely of status badges as linked images.
#' This section is automatically populated if there is an identifiable badges
#' paragraph in the homepage source (index.Rmd, index.md, README.Rmd,
#' README.md):
#' * pkgdown first looks for a paragraph starting with `<!-- badges: start -->`
#' and ending with `<!-- badges: end -->` as created by
#' `usethis::use_readme_md()` or `usethis::use_readme_rmd()`;
#' * failing that, pkgdown looks at the first paragraph, and uses it if it
#' only contains images.
#'
#' @inheritParams build_articles
#' @export
Expand Down
10 changes: 8 additions & 2 deletions R/html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,15 @@ tweak_homepage_html <- function(html, strip_header = FALSE) {

# Mutates `html`, removing the badge container
badges_extract <- function(html) {
# First try specially named div; then try first paragraph
# First try specially named div;
x <- xml2::xml_find_first(html, "//div[@id='badges']")

# then try usethis-readme-like paragraph;
if (length(x) == 0) {
x <- xml2::xml_find_all(html, ".//*/comment()[contains(., 'badges: start')]/following-sibling::p[1]")
}

# finally try first paragraph
if (length(x) == 0) {
x <- xml2::xml_find_first(html, "//p")
}
Expand Down Expand Up @@ -276,7 +283,6 @@ badges_extract_text <- function(x) {
xml <- xml2::read_html(x)
badges_extract(xml)
}

# Update file on disk -----------------------------------------------------

update_html <- function(path, tweak, ...) {
Expand Down
12 changes: 10 additions & 2 deletions man/build_home.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,27 @@ test_that("badges can't contain an extra text", {
)
})


test_that("badges can be in special div", {
expect_equal(
badges_extract_text('<p></p><div id="badges"><a href="x"><img src="y"></a></div>'),
'<a href="x"><img src="y"></a>'
)
})

test_that("badges-paragraph a la usethis can be found", {
string <- '
<blockquote>
<p>Connect to thisisatest, from R</p>
</blockquote>
<!-- badges: start -->
<p><a href="https://travis-ci.org/thisisatest/thisisatest"><img src="https://travis-ci.org/thisisatest/thisisatest.svg?branch=master" alt="Linux Build Status"></a> <!-- badges: end --></p>
<div id="introduction" class="section level2">
<h2 class="hasAnchor">
<a href="#introduction" class="anchor"></a>Introduction</h2>
<p>The thingie is a blabla.</p>
<p>The <code>thisisatest</code> package also blabla.</p>'

badges_page <- xml2::read_html(string)
expect_equal(length(badges_extract(badges_page)), 1)
})
5 changes: 3 additions & 2 deletions vignettes/pkgdown.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ In addition to home page content from the README (e.g., installation instruction
Convert your package logo to a favicon with `build_favicon()`


* Badges in the home page source file (e.g. `README.Rmd`) are linked in the
sidebar under "Dev status".
* Badges in the home page source file (index.Rmd, index.md, README.Rmd, README.md) are automatically displayed in the sidebar under the section "Dev status" if there is an identifiable badges paragraph:
* pkgdown first looks for a paragraph starting with `<!-- badges: start -->` and ending with `<!-- badges: end -->` as created by `usethis::use_readme_md()` or `usethis::use_readme_rmd()`;
* failing that, pkgdown looks at the first paragraph, and uses it if it only contains images.

* A link for bug reports is added if the `BugReports` field in
`DESCRIPTION` contains a link. You can use `usethis::use_github_links()`
Expand Down

0 comments on commit 04e3f6e

Please sign in to comment.