From 5aaa2c1bd5fb4913407c18f72ffc8670bd18e2d5 Mon Sep 17 00:00:00 2001 From: froggleston Date: Fri, 22 Nov 2024 13:59:35 +0000 Subject: [PATCH 1/2] Add support for disabling sidebar automatic numbering --- R/utils-sidebar.R | 25 +++++++++++++++++++++---- R/utils-varnish.R | 22 ++++++++++++++++++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/R/utils-sidebar.R b/R/utils-sidebar.R index 6a5d93f3..3454a04d 100644 --- a/R/utils-sidebar.R +++ b/R/utils-sidebar.R @@ -144,6 +144,9 @@ create_sidebar_headings <- function(nodes) { #' @param name the name of the current chapter #' @param html the html of the current chapter. defaults to a link that will #' produce a sidebar with no links to headings. +#' @param disable_numbering a boolean indicating if the sidebar should not automatically +#' number the chapters. Defaults to `FALSE`. If `TRUE`, developers should consider +#' adding their own custom numbering to the chapter titles in the frontmatter. #' @return a character vector of HTML divs that can be appended to display the #' sidebar. #' @keywords internal @@ -151,15 +154,29 @@ create_sidebar_headings <- function(nodes) { #' [set_globals()] for where `create_sidebar()` is called and #' [build_html()] for where `update_sidebar()` is called. #' @rdname create_sidebar -create_sidebar <- function(chapters, name = "", html = "") { +create_sidebar <- function( + chapters, + name = "", + html = "", + disable_numbering = FALSE) { res <- character(length(chapters)) + for (i in seq(chapters)) { position <- if (name == chapters[i]) "current" else i info <- get_navbar_info(chapters[i]) - # We use zero index to count the index page (which is removed later) + + numbering_prefix = paste0(i - 1, ". ") + # if numbering is disabled, remove list numbering prefix + if (disable_numbering) { + numbering_prefix = "" + } + + # We use zero index to count the index page + # (which is removed later if automated numbering is enabled) page_link <- paste0( "", - i - 1, ". ", parse_title(info$pagetitle), + numbering_prefix, + parse_title(info$pagetitle), "" ) res[i] <- create_sidebar_item(html, page_link, position) @@ -253,7 +270,7 @@ update_sidebar <- function( #' #' # Add an anchor to the links #' snd$fix_sidebar_href(my_links, scheme = "https", fragment = "anchor") -#' +#' #' # NOTE: this will _always_ return a character vector, even if the input is #' # incorrect #' snd$fix_sidebar_href(list(), server = "example.com") diff --git a/R/utils-varnish.R b/R/utils-varnish.R index be080f34..eaa2f39f 100644 --- a/R/utils-varnish.R +++ b/R/utils-varnish.R @@ -78,13 +78,31 @@ set_globals <- function(path) { # that is different is the name of the index node. idx <- these_resources[["."]] idx <- idx[as_html(idx) == "index.html"] - instructor_sidebar <- create_sidebar(c(idx, these_resources[["episodes"]])) + + # get sidebar numbering disable option from config, if null set FALSE + disable_numbering <- this_metadata$get()[["disable_sidebar_numbering"]] %||% FALSE + + instructor_sidebar <- create_sidebar( + c(idx, these_resources[["episodes"]]), + disable_numbering = disable_numbering + ) # check if we have a title in the index sidebar and replace with # "summary and schedule" if it does not exist. idx_item <- xml2::read_html(instructor_sidebar[[1]]) idx_link <- xml2::xml_find_first(idx_item, ".//a") idx_text <- xml2::xml_contents(idx_link) - no_index_title <- length(idx_text) == 1 && xml2::xml_text(idx_text) == "0. " + href <- xml2::xml_attr(idx_link, "href") + + no_index_title <- ( + length(idx_text) == 1 && + xml2::xml_text(idx_text) == "0. " + ) || + ( + disable_numbering && + length(idx_text) == 0 && + href == "index.html" + ) + if (no_index_title) { xml2::xml_set_text(idx_link, tr_computed("SummaryAndSchedule")) } else { From 067a316335f09fe760071de53e0ab132bae60835 Mon Sep 17 00:00:00 2001 From: froggleston Date: Fri, 22 Nov 2024 14:24:43 +0000 Subject: [PATCH 2/2] Update documentation --- man/create_sidebar.Rd | 7 ++++++- man/known_languages.Rd | 1 + man/sandpaper-package.Rd | 2 ++ man/translations.Rd | 3 ++- man/yaml_list.Rd | 4 +--- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/man/create_sidebar.Rd b/man/create_sidebar.Rd index 98f4e848..f9a21d4b 100644 --- a/man/create_sidebar.Rd +++ b/man/create_sidebar.Rd @@ -12,7 +12,8 @@ create_resources_dropdown(files, type = "learners") create_sidebar( chapters, name = "", - html = "" + html = "", + disable_numbering = FALSE ) update_sidebar( @@ -38,6 +39,10 @@ are included.} \item{html}{the html of the current chapter. defaults to a link that will produce a sidebar with no links to headings.} +\item{disable_numbering}{a boolean indicating if the sidebar should not automatically +number the chapters. Defaults to \code{FALSE}. If \code{TRUE}, developers should consider +adding their own custom numbering to the chapter titles in the frontmatter.} + \item{sidebar}{an object of class "list-store" which has a \code{"sidebar"} element in the stored list. See \code{\link[=set_globals]{set_globals()}}.} diff --git a/man/known_languages.Rd b/man/known_languages.Rd index 79f820c6..c1ff7e3e 100644 --- a/man/known_languages.Rd +++ b/man/known_languages.Rd @@ -21,6 +21,7 @@ details of how to do so in the source code for \code{{sandpaper}}. \subsection{List of Known Languages:}{ \if{html}{\out{
}}\preformatted{#> - en +#> - de #> - es #> - fr #> - ja diff --git a/man/sandpaper-package.Rd b/man/sandpaper-package.Rd index 9c1a31c0..38bda3ce 100644 --- a/man/sandpaper-package.Rd +++ b/man/sandpaper-package.Rd @@ -40,6 +40,8 @@ Other contributors: \item Hugo Gruson \email{hugo.gruson+R@normalesup.org} [contributor] \item Milan Malfait \email{milan.malfait94@gmail.com} (\href{https://orcid.org/0000-0001-9144-3701}{ORCID}) [contributor] \item Yanina Bellini Saibene (\href{https://orcid.org/0000-0002-4522-7466}{ORCID}) [contributor, translator] + \item Martin Raden (\href{https://orcid.org/0000-0002-7926-5911}{ORCID}) [contributor, translator] + \item Matthew Armstrong \email{cs.matthewarmstrong@gmail.com} [contributor] } } diff --git a/man/translations.Rd b/man/translations.Rd index 5458a3b5..4903e990 100644 --- a/man/translations.Rd +++ b/man/translations.Rd @@ -150,7 +150,7 @@ There are 62 translations generated by } -In addition, there are 26 translations +In addition, there are 27 translations that are inserted \emph{before} they get to \code{{varnish}}:\tabular{ll}{ variable \tab string \cr \code{OUTPUT} \tab \code{'OUTPUT'} \cr @@ -165,6 +165,7 @@ that are inserted \emph{before} they get to \code{{varnish}}:\tabular{ll}{ \code{Checklist} \tab \code{'Checklist'} \cr \code{Discussion} \tab \code{'Discussion'} \cr \code{Testimonial} \tab \code{'Testimonial'} \cr + \code{Caution} \tab \code{'Caution'} \cr \code{Keypoints} \tab \code{'Key Points'} \cr \verb{Show me the solution} \tab \code{'Show me the solution'} \cr \verb{Give me a hint} \tab \code{'Give me a hint'} \cr diff --git a/man/yaml_list.Rd b/man/yaml_list.Rd index c6edd157..aea4ad03 100644 --- a/man/yaml_list.Rd +++ b/man/yaml_list.Rd @@ -25,9 +25,7 @@ cat(yaml::as.yaml(hx)) # representation in yaml #> - a #> - b #> - c -}\if{html}{\out{
}} - -\if{html}{\out{
}}\preformatted{cat(whisker::whisker.render("hello: \{\{hello\}\}", hx)) # messed up whisker +cat(whisker::whisker.render("hello: \{\{hello\}\}", hx)) # messed up whisker #> hello: a,b,c }\if{html}{\out{
}}