Skip to content

Commit

Permalink
Sync with carpentries/sandpaper (#101)
Browse files Browse the repository at this point in the history
* Allow custom carpentry setting

* Add optional `carpentry_description`

* Add `carpentry_description` to the config template

* Test that `which_carpentry` works correctly

* Update documentation reference

Following updates in carpentries/sandpaper-docs#197

Co-authored-by: Erin Becker <ebecker@carpentries.org>

* Fix for pkgdown 2.1.0 adding anchors to sections

* Revert last commit, add pinned pkgdown version

* Update DESCRIPTION

Revert package pinning as it doesn't seem to be respected in dependency calculations from fresh installs - something I don't really understand with R. This means that sandpaper will continue to build but will have the duplicate anchors for h2 sections, and tests will fail.

---------

Co-authored-by: Erin Becker <ebecker@carpentries.org>
Co-authored-by: Robert Davey <robertdavey@carpentries.org>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent 81c4dfe commit 5413303
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
9 changes: 9 additions & 0 deletions R/utils-xml.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,23 @@ add_anchors <- function(nodes, ids) {
anchor <- paste0(
"<a class='anchor' aria-label='", tranchor, "' href='#", ids, "'></a>"
)

for (i in seq_along(nodes)) {
heading <- nodes[[i]]

if (length(xml2::xml_contents(heading)) == 0) {
# skip empty headings
next
}

# Insert anchor in first element of header
xml2::xml_add_child(heading, xml2::read_xml(anchor[[i]]))

# fix for pkgdown 2.1.0 now adding in anchors for <section> tags
# rename our workbench translated sections <workbench-section>
# this would require a lot more downstream work
# sections <- xml2::xml_parent(heading)
# xml2::xml_set_name(sections, "workbench-section")
}
}

Expand Down
2 changes: 1 addition & 1 deletion R/utils-yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ create_pkgdown_yaml <- function(path) {
branch = siQuote(usr$branch),
contact = siQuote(usr$contact),
# What carpentry are we dealing with?
carpentry_name = siQuote(which_carpentry(usr$carpentry)),
carpentry_name = siQuote(which_carpentry(usr$carpentry, usr$carpentry_description)),
carpentry = siQuote(usr$carpentry),
carpentry_icon = siQuote(which_icon_carpentry(usr$carpentry)),
license = siQuote(usr$license),
Expand Down
11 changes: 8 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,19 @@ create_description <- function(path) {
desc$write(fs::path(path_site(path), "DESCRIPTION"))
}

which_carpentry <- function(carpentry) {
which_carpentry <- function(carpentry, carpentry_description = NULL) {
if (!is.null(carpentry_description)) {
return(carpentry_description)
}
switch(carpentry,
lc = "Library Carpentry",
dc = "Data Carpentry",
swc = "Software Carpentry",
cp = "The Carpentries",
incubator = "Carpentries Incubator",
lab = "Carpentries Lab",
l2d = "l2d"
# Default: match the input
carpentry
)
}

Expand All @@ -207,7 +211,8 @@ which_icon_carpentry <- function(carpentry) {
cp = "carpentries",
incubator = "incubator",
lab = "lab",
l2d = "l2d"
# Default: match the input
carpentry
)
}

Expand Down
6 changes: 6 additions & 0 deletions inst/templates/config-template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
# lc: Library Carpentry
# cp: Carpentries (to use for instructor training for instance)
# incubator: The Carpentries Incubator
# Note that you can also use a custom carpentry type. For more info,
# see the documentation: https://carpentries.github.io/sandpaper-docs/editing.html
carpentry: {{ carpentry }}

# Custom carpentry description
# This will be used as the alt text for the logo
# carpentry_description: "Custom Carpentry"

# Overall title for pages.
title: {{ title }}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ test_that("a sitemap can be generated for urls", {
urls <- c("https://example.com/one", "https://example.com/two")
expect_snapshot(urls_to_sitemap(urls))
})

test_that("which_carpentry_workshop works for default carpentries", {
expect_equal(which_carpentry("swc"), "Software Carpentry")
expect_equal(which_carpentry("dc"), "Data Carpentry")
expect_equal(which_carpentry("lc"), "Library Carpentry")
expect_equal(which_carpentry("cp"), "The Carpentries")
})

test_that("which_carpentry can take a custom description", {
expect_equal(which_carpentry("ice-cream", "Ice Cream Carpentry"), "Ice Cream Carpentry")
expect_equal(which_carpentry("mexican-guitars"), "mexican-guitars")
})

0 comments on commit 5413303

Please sign in to comment.